Created
December 26, 2014 08:15
-
-
Save niksmac/6c7ff6e33dca9948d56e to your computer and use it in GitHub Desktop.
Drupal Gotcha : Cache_get() Returns Expired Items
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
| cache_get() returns $cache objects even if the cached item is stale (expired) | |
| function custom_fn_get_data() { | |
| // Return the cached data | |
| $cache = cache_get('custom:fn_get_data'); | |
| if (!$cache) { | |
| // Some expensive processing to build the data. | |
| $data = complicated_recursion_and_loops_on_lots_of_data(); | |
| // Cache the data and rebuild it every hour | |
| $expire = time() + (60 * 60); | |
| cache_set('custom:fn_get_data', $data, 'cache', $expire); | |
| } | |
| else { | |
| $data = $cache->data; | |
| } | |
| return $data; | |
| } | |
| You can make it better by this way | |
| if (!$cache or time() > $cache->expire) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment