Created
March 8, 2012 16:24
-
-
Save richthegeek/2001903 to your computer and use it in GitHub Desktop.
Variable microcaching with Laravel
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 | |
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); | |
} | |
}); |
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 | |
Route::get('foo', array('before' => 'cache', 'after' => 'cache', function() { | |
... | |
}); |
maikeldaloo: not quite, it caches the full response based on how busy the server is at the time.
However, factoring in page build time isn't such a bad idea!
That's really cool.
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.
Solution: don't develop on windows ;)
I've update the gist to fix this anyway.
Haha, I love my Windows :)
Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, this snippet caches the full page/reponse based on how long it takes to load.
Is that right?