标签:eve request 代码 ror requests etc ica mina 元素
项目背景:最初是想给接口加缓存,但是不想每个接口添加缓存代码,就写了个统一的缓存系统。
技术方案:
前置知识:
下面是代码实现:
1,首先在app/Providers/EventServiceProvider.php 里添加对应的event和listener。
use Illuminate\Foundation\Http\Events\RequestHandled;
*
*
*
protected $listen = [
RequestHandled::class => [
\App\Listeners\CacheApiResult::class
]
];
然后在项目的根目录执行 php artisan event:generate
这个命令用来生成event文件和listener文件;event用的是框架自带的RequestHandled事件,所以不会有改动,这个命令只会在app/Listeners文件夹下添加一个CacheApiResult.php文件。 文件内容如下
<?php
namespace App\Listeners;
use Cache;
use Illuminate\Foundation\Http\Events\RequestHandled;
class CacheApiResult
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param RequestHandled $event
* @return void
*/
public function handle(RequestHandled $event)
{
//$event->response->isCached这个属性是在后面的中间件里面加的,对象本身是没有这个属性的。
if($event->request->isMethod(‘GET‘) && !isset($event->response->isCached) ){
//这里的key生成规则是我自己定义的,可以按需更改。
$uri = $event->request->getUri();
$params = $event->request->all();
$keyStr = $uri.‘::‘;
foreach($params as $key => $val){
$keyStr = $keyStr.$key.‘:‘.$val.‘::‘;
}
$key = md5($keyStr);
$data = $event->response->getContent();
Cache::set($key, $data, 1);
//这里缓存一分钟,目前看来分钟好像是最小粒度了,以后需要再改进;可以按需改成其他缓存
}
}
}
然后需要添加一个中间件 添加app/Http/Middleware/ApiCache.php文件
<?php
namespace App\Http\Middleware;
use Closure;
use Cache;
class ApiCache
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($request->isMethod(‘GET‘)){
$uri = $request->getUri();
$params = $request->all();
$keyStr = $uri.‘::‘;
foreach($params as $key => $val){
$keyStr = $keyStr.$key.‘:‘.$val.‘::‘;
}
$key = md5($keyStr);
$data = Cache::get($key);
if($data){
$data = json_decode($data,true);
$response = response()->json($data);
$response->isCached = true;//添加这个属性是为了避免监听器重复写入缓存
return $response;
}
}
return $next($request);
}
}
然后要去注册这个中间件,让每个请求都经过它。
在app/Http/Kernel.php文件的$routeMiddleware中添加一个元素 ‘apiCache‘ => \App\Http\Middleware\ApiCache::class,
protected $routeMiddleware = [
‘auth‘ => \App\Http\Middleware\Authenticate::class,
‘auth.basic‘ => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
‘bindings‘ => \Illuminate\Routing\Middleware\SubstituteBindings::class,
‘cache.headers‘ => \Illuminate\Http\Middleware\SetCacheHeaders::class,
‘can‘ => \Illuminate\Auth\Middleware\Authorize::class,
‘guest‘ => \App\Http\Middleware\RedirectIfAuthenticated::class,
‘change‘ => \App\Http\Middleware\ChangeJsonToForm::class,
‘signed‘ => \Illuminate\Routing\Middleware\ValidateSignature::class,
‘throttle‘ => \Illuminate\Routing\Middleware\ThrottleRequests::class,
‘verified‘ => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
‘apiCache‘ => \App\Http\Middleware\ApiCache::class,
];
这样就完成了这个缓存系统。其中的缓存我用的是redis,这个可以按需配置,不了解可以看一下lavarel的缓存知识。
这只是一个初级版,以后有需要可以升级一下。
原创地址:https://blog.csdn.net/u010965027/article/details/87934651
标签:eve request 代码 ror requests etc ica mina 元素
原文地址:https://www.cnblogs.com/aipiaoborensheng/p/12787850.html