mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
implement microcaching with database
This commit is contained in:
parent
5a5dcfb36b
commit
fbd7519ce9
@ -180,7 +180,7 @@ class Handler extends ExceptionHandler
|
||||
*/
|
||||
private function set404Cache(Request $request, BadResponseException $e)
|
||||
{
|
||||
if (!env('CACHING')) {
|
||||
if (!env('CACHING') || env('MICROCACHING')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace App\Http\Middleware;
|
||||
use App\Http\HttpHelper;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Jikan\Exception\BadResponseException;
|
||||
|
||||
class MicroCaching
|
||||
{
|
||||
@ -17,35 +18,38 @@ class MicroCaching
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
|
||||
if ($request->header('auth') === env('APP_KEY')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if (!env('CACHING')) {
|
||||
if (
|
||||
!env('CACHING')
|
||||
|| !env('MICROCACHING')
|
||||
|| env('CACHE_DRIVER') !== 'redis'
|
||||
)
|
||||
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 (Cache::has($fingerprint)) {
|
||||
|
||||
// if cache exists, return cache
|
||||
if (app('redis')->exists($fingerprint)) {
|
||||
return response()
|
||||
->json(
|
||||
json_decode(Cache::get($fingerprint), true)
|
||||
\json_decode(app('redis')->get($fingerprint), true)
|
||||
);
|
||||
}
|
||||
|
||||
// set cache
|
||||
app('redis')->set(
|
||||
$fingerprint,
|
||||
json_encode(
|
||||
$next($request)->getData()
|
||||
)
|
||||
);
|
||||
|
||||
app('redis')->expire($fingerprint, env('MICROCACHING_EXPIRE', 5));
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public static function setMicroCache($fingerprint, $cache) {
|
||||
$fingerprint = "microcache:".$fingerprint;
|
||||
$cache = json_encode($cache);
|
||||
|
||||
Cache::add($fingerprint, $cache, env('MICROCACHING_EXPIRE', 5));
|
||||
}
|
||||
}
|
||||
|
@ -91,39 +91,31 @@ $app->routeMiddleware([
|
||||
|
|
||||
*/
|
||||
|
||||
if (env('CACHING')) {
|
||||
$app->configure('cache');
|
||||
$app->register(Illuminate\Redis\RedisServiceProvider::class);
|
||||
}
|
||||
|
||||
$app->configure('database');
|
||||
$app->configure('queue');
|
||||
$app->configure('controller-to-table-mapping');
|
||||
$app->configure('controller');
|
||||
|
||||
$app->register(\SwaggerLume\ServiceProvider::class);
|
||||
|
||||
if (env('CACHING')) {
|
||||
$app->configure('cache');
|
||||
$app->register(Illuminate\Redis\RedisServiceProvider::class);
|
||||
}
|
||||
|
||||
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
|
||||
$app->register(\App\Providers\SourceHeartbeatProvider::class);
|
||||
|
||||
$guzzleClient = new \GuzzleHttp\Client([
|
||||
'timeout' => env('SOURCE_TIMEOUT', 5),
|
||||
'connect_timeout' => env('SOURCE_CONNECT_TIMEOUT', 5)
|
||||
]);
|
||||
$app->instance('GuzzleClient', $guzzleClient);
|
||||
|
||||
$app->instance('GuzzleClient', $guzzleClient);
|
||||
$jikan = new \Jikan\MyAnimeList\MalClient(app('GuzzleClient'));
|
||||
$app->instance('JikanParser', $jikan);
|
||||
|
||||
$app->instance('SerializerV4', SerializerFactory::createV4());
|
||||
|
||||
$app->register(\App\Providers\SourceHeartbeatProvider::class);
|
||||
|
||||
|
||||
/**
|
||||
* Load Blacklist into Redis
|
||||
*/
|
||||
//\App\HttpV3\Middleware\Blacklist::loadList(); causing issues on high load todo: add it as a one time init
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Load The Application Routes
|
||||
@ -142,8 +134,8 @@ $commonMiddleware = [
|
||||
// 'database-resolver',
|
||||
// 'cache-resolver',
|
||||
// 'throttle'
|
||||
'microcaching',
|
||||
'source-health-monitor'
|
||||
'source-health-monitor',
|
||||
'microcaching'
|
||||
];
|
||||
|
||||
|
||||
@ -174,7 +166,13 @@ $app->router->group(
|
||||
'github_url' => 'https://github.com/jikan-me/jikan-rest',
|
||||
'parser_github_url' => 'https://github.com/jikan-me/jikan',
|
||||
'production_api_url' => 'https://api.jikan.moe/v4/',
|
||||
'status_url' => 'https://status.jikan.moe'
|
||||
'status_url' => 'https://status.jikan.moe',
|
||||
'myanimelist_heartbeat' => [
|
||||
'status' => \App\Providers\SourceHeartbeatProvider::getHeartbeatStatus(),
|
||||
'score' => \App\Providers\SourceHeartbeatProvider::getHeartbeatScore(),
|
||||
'down' => \App\Providers\SourceHeartbeatProvider::isFailoverEnabled(),
|
||||
'last_downtime' => \App\Providers\SourceHeartbeatProvider::getLastDowntime()
|
||||
]
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user