mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
add microcaching #66
This commit is contained in:
parent
9ab9a00377
commit
9f6ee54cc3
@ -14,6 +14,9 @@ CACHE_USER_EXPIRE=300
|
||||
CACHE_404_EXPIRE=604800
|
||||
CACHE_SEARCH_EXPIRE=432000
|
||||
|
||||
MICROCACHING=false
|
||||
MICROCACHING_EXPIRE=5
|
||||
|
||||
QUEUE_DELAY_PER_JOB=5
|
||||
|
||||
THROTTLE=false
|
||||
|
@ -18,7 +18,7 @@ class ModifyCacheDriver extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Change caching driver. (redis, file)';
|
||||
protected $description = 'Change the cache driver. [redis, file]';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
@ -48,8 +48,8 @@ class ModifyCacheDriver extends Command
|
||||
file_put_contents($path, str_replace(
|
||||
'CACHE_DRIVER='.env('CACHE_DRIVER'), 'CACHE_DRIVER='.$this->argument('driver'), file_get_contents($path)
|
||||
));
|
||||
|
||||
$this->info("CACHE_DRIVER is now set to '{$this->argument('driver')}'");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -42,8 +42,6 @@ class ModifyCacheMethod extends Command
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$path = base_path('.env');
|
||||
|
||||
if (file_exists($path)) {
|
||||
@ -52,6 +50,6 @@ class ModifyCacheMethod extends Command
|
||||
));
|
||||
}
|
||||
|
||||
$this->info("CACHE_METHOD is now set to {$this->argument('method')}");
|
||||
$this->info("CACHE_METHOD is now set to '{$this->argument('method')}'");
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ class EtagMiddleware
|
||||
}
|
||||
|
||||
$fingerprint = HttpHelper::resolveRequestFingerprint($request);
|
||||
|
||||
if (
|
||||
$request->hasHeader('If-None-Match')
|
||||
&& Cache::has($fingerprint)
|
||||
|
@ -143,9 +143,16 @@ class JikanResponseHandler
|
||||
$cacheMutable = json_decode($cache, true);
|
||||
$cacheMutable = $this->cacheMutation($cacheMutable);
|
||||
|
||||
$response = array_merge($meta, $cacheMutable);
|
||||
|
||||
// Allow microcaching if it's enabled and the cache driver is set to file
|
||||
if (env('MICROCACHING', true) && env('CACHE_DRIVER', 'file') === 'file') {
|
||||
MicroCaching::setMicroCache($this->fingerprint, $response);
|
||||
}
|
||||
|
||||
return response()
|
||||
->json(
|
||||
array_merge($meta, $cacheMutable)
|
||||
$response
|
||||
)
|
||||
->setEtag(
|
||||
md5($cache)
|
||||
|
47
app/Http/Middleware/MicroCaching.php
Normal file
47
app/Http/Middleware/MicroCaching.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Http\HttpHelper;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
class MicroCaching
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if ($request->header('auth') === env('APP_KEY')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Microcaching should not work alongside redis caching
|
||||
if (!env('MICROCACHING', false) || env('CACHE_DRIVER', 'file') === 'redis') {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$fingerprint = "microcache:".HttpHelper::resolveRequestFingerprint($request);
|
||||
if (app('redis')->exists($fingerprint)) {
|
||||
return response()
|
||||
->json(
|
||||
json_decode(app('redis')->get($fingerprint), true)
|
||||
);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public static function setMicroCache($fingerprint, $cache) {
|
||||
$fingerprint = "microcache:".$fingerprint;
|
||||
$cache = json_encode($cache);
|
||||
|
||||
app('redis')->set($fingerprint, $cache);
|
||||
app('redis')->expire($fingerprint, env('MICROCACHING_EXPIRE', 5));
|
||||
}
|
||||
}
|
@ -73,7 +73,8 @@ $app->routeMiddleware([
|
||||
'meta' => App\Http\Middleware\Meta::class,
|
||||
'jikan-response' => App\Http\Middleware\JikanResponseHandler::class,
|
||||
'throttle' => App\Http\Middleware\Throttle::class,
|
||||
'etag' => \App\Http\Middleware\EtagMiddleware::class
|
||||
'etag' => \App\Http\Middleware\EtagMiddleware::class,
|
||||
'microcaching' => \App\Http\Middleware\MicroCaching::class
|
||||
]);
|
||||
|
||||
/*
|
||||
@ -122,6 +123,7 @@ $commonMiddleware = [
|
||||
'slave-auth',
|
||||
'meta',
|
||||
'etag',
|
||||
'microcaching',
|
||||
'jikan-response',
|
||||
'throttle'
|
||||
];
|
||||
|
Loading…
x
Reference in New Issue
Block a user