lifesign
10/9/2013 - 5:01 PM

routes.php

<?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 );
		}
	}
	
});