Last active
December 30, 2015 21:38
-
-
Save carbontwelve/7888347 to your computer and use it in GitHub Desktop.
From http://markvaneijk.com/caching-routes-using-filters-in-laravel-4 and amended to add configuration.
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
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Cache Filters | |
|-------------------------------------------------------------------------- | |
| | |
| This filter can be attached to a route to easily add a simple caching | |
| layer. | |
| | |
| Use as a before and after filter. When fired after a request the cache | |
| will be updated if required, and when fired before a request the cache | |
| will be loaded if required. | |
| | |
| From: http://markvaneijk.com/caching-routes-using-filters-in-laravel-4 | |
| | |
| Usage: | |
| Route::get('/', array | |
| ( | |
| 'before' => 'cache', | |
| 'after' => 'cache', | |
| 'uses' => 'HomeController@show' | |
| ) | |
| ); | |
*/ | |
Route::filter('cache', function($route, $request, $response = null) | |
{ | |
if ( Config::get('cache.page_caching', false) === true ) | |
{ | |
$key = 'route-'.Str::slug(Request::url()); | |
if(is_null($response) && Cache::has($key)) | |
{ | |
return Cache::get($key); | |
} | |
elseif(!is_null($response) && !Cache::has($key)) | |
{ | |
Cache::put($key, $response->getContent(), 30); | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment