Skip to content

Commit

Permalink
add microcaching #66
Browse files Browse the repository at this point in the history
  • Loading branch information
Irfan committed Nov 18, 2019
1 parent 9ab9a00 commit 9f6ee54
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/Console/Commands/ModifyCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -48,8 +48,8 @@ public function handle()
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')}'");
}
}
}
4 changes: 1 addition & 3 deletions app/Console/Commands/ModifyCacheMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public function handle()
return;
}



$path = base_path('.env');

if (file_exists($path)) {
Expand All @@ -52,6 +50,6 @@ public function handle()
));
}

$this->info("CACHE_METHOD is now set to {$this->argument('method')}");
$this->info("CACHE_METHOD is now set to '{$this->argument('method')}'");
}
}
1 change: 1 addition & 0 deletions app/Http/Middleware/EtagMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function handle($request, Closure $next)
}

$fingerprint = HttpHelper::resolveRequestFingerprint($request);

if (
$request->hasHeader('If-None-Match')
&& Cache::has($fingerprint)
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Middleware/JikanResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,16 @@ public function handle(Request $request, Closure $next)
$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)
Expand Down
47 changes: 47 additions & 0 deletions app/Http/Middleware/MicroCaching.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
4 changes: 3 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
'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
]);

/*
Expand Down Expand Up @@ -122,6 +123,7 @@
'slave-auth',
'meta',
'etag',
'microcaching',
'jikan-response',
'throttle'
];
Expand Down

0 comments on commit 9f6ee54

Please sign in to comment.