I attempted to use the Illuminate\Http\Request::fingerprint method, but it is another functionality documented in Laravel and inexplicably left out of Lumen by Mr. Otwell. Why am I not surprised that the documentation is so shoddy for yet another release of Lumen?
Full details of that error are in this issue: https://github.com/laravel/lumen-framework/issues/348
I created a replacement function that I appended underneath the helpers that Otwell arbitrarily and inexplicably left out of Lumen
section of my bootstrap/app.php
file.
function fingerprint()
{
$r = request();
return hash('sha1', json_encode([
$r->url(),
$r->method(),
$r->ips(),
$r->header(),
$r->all()
]));
}
After some more fiddling/experimentation, I created this and put it in my bootstrap/app.php
file.
// additional helpers
function cache ($key = '', $value = '', $minutes = 1)
{
if (empty($key))
return app('cache');
if (empty($value))
return cache()->has($key) ? unserialize(Serpent\Serpent::decrypt(cache()->get($key))) : '';
cache()->put($key, Serpent\Serpent::encrypt(serialize($value)), $minutes);
}
function flash($key='', $value='')
{
if (empty($key)) return '';
$f = fingerprint();
if (empty($value)) {
if (cache()->has($f.':'.$key)) {
return cache($f.':'.$key);
} else
return '';
}
if (!cache()->has($f))
$c = [];
else
$c = explode(';', cache($f));
if (!in_array($key, $c)) {
$c[] = $key;
cache($f, implode(';', $c));
}
cache($f.':'.$key, $value);
return true;
}
Note that the Serpent
library is a custom class for handling encryption.
All I have to do now is create a Middleware that cleans out the cache at the end of each request.
The following Middleware works for this.
class CleanRequestCacheMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$f = fingerprint();
if ($c = cache($f)) {
$a = explode(';', $c);
for ($i=0, $il=count($a); $i<$il; ++$i) {
cache()->forget($f.':'.$a[$i]);
}
}
return $response;
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community