Skip to content

Instantly share code, notes, and snippets.

@olssonm
Created September 2, 2014 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olssonm/ec6faaf067d3a9b47f14 to your computer and use it in GitHub Desktop.
Save olssonm/ec6faaf067d3a9b47f14 to your computer and use it in GitHub Desktop.
Laravel 4.2 route caching
<?php
/**
* For caching routes
*/
Route::filter('cache', function($route, $request, $response = null) {
$cacheTagKey = Config::get('values.cacheKeys.tags_routes');
// Do not cache if the devcookie is set
if(Util::testDevCookie() == false) {
$key = Str::slug(Request::url());
if(is_null($response) && Cache::tags($cacheTagKey)->has($key)) {
return Cache::tags($cacheTagKey)->get($key);
} elseif(!is_null($response) && !Cache::tags($cacheTagKey)->has($key)) {
// Cache for 2 hours
Cache::tags($cacheTagKey)->put($key, $response->getContent(), 60*2);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment