-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Irfan
committed
Nov 18, 2019
1 parent
9ab9a00
commit 9f6ee54
Showing
7 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters