Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created March 8, 2012 16:24
Show Gist options
  • Save richthegeek/2001903 to your computer and use it in GitHub Desktop.
Save richthegeek/2001903 to your computer and use it in GitHub Desktop.
Variable microcaching with Laravel
<?php
Route::filter('cache', function($response = NULL) {
$cname = 'response-' . Str::slug(URI::full());
if (!$response) {
return Cache::get($cname);
}
else if ($response->status == 200) {
$ctime = floor(pow(current(sys_getloadavg()) + 1, 5)); # cache for between 1 and 32 minutes
Cache::put($cname, $response, $ctime);
}
});
<?php
Route::get('foo', array('before' => 'cache', 'after' => 'cache', function() {
...
});
@maikeldaloo
Copy link

That's really cool.

@maikeldaloo
Copy link

Just noticed a tiny bug when developing locally on a Windows machine, and possibly if the site is hosted on a Windows machine.

Windows doesn't allow semicolons (:) in file names.
It's better to use a dash or an underscore.

@richthegeek
Copy link
Author

Solution: don't develop on windows ;)

I've update the gist to fix this anyway.

@maikeldaloo
Copy link

Haha, I love my Windows :)
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment