Created
October 9, 2013 17:01
-
-
Save lifesign/6904583 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 route filter | |
|-------------------------------------------------------------------------- | |
| | |
| This filter must be called 'before' and 'after' any of the front-end | |
| pages are loaded. | |
| Before the page is loaded, we return a cached version of the page, if | |
| available. And after the page is loaded, we cache it. | |
| | |
| @example: | |
| Route::group(array('before' => 'cache', 'after' => 'cache'), function() | |
| { | |
| Route::controller('home'); | |
| }); | |
| | |
*/ | |
Route::filter('cache', function( $response = null ) | |
{ | |
$uri = URI::full() == '/' ? 'home' : Str::slug( URI::full() ); | |
$cached_filename = "response-$uri"; | |
if ( is_null($response) ) | |
{ | |
return Cache::get( $cached_filename ); | |
} | |
else if ( $response->status == 200 ) | |
{ | |
$cache_time = 30; // 30 minutes | |
if ( $cache_time > 0 ) { | |
Cache::put( $cached_filename , $response , $cache_time ); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment