Drupal cache_get - a gotcha
Background
Noticed that cache_get did not seem to obey the cache lifetime, returning 'expired' cache dataOverview
After over-abusing the drupal cache tables, to store any and every bit of custom cached data, using the default 'cache' table, I decided to roll my ownI set up a new cache table - easy enough to do, just copy the structure of the rest of the cache tables. But it didn't - as I erroneously assumed - return zilch if the cache had expired, it simply returned the expired entry
Solution/Conclusion
Turns out the cache_get does not check the expiration date before returning the data. If you have regular caching on your site, it will return the cached data to you and leave you to work out if it is stale or not. Only on a clear cache all, will the data get cleared down.
as it goes, checking is fairly trivial...
as it goes, checking is fairly trivial...
Cache set
// cache data for an hour cache_set($cid,$yourdataToCache,'cache_custom_cache_table',time() + (60*60));
Cache get
// check cached data if(($cachedCopy = cache_get($cid,'cache_custom_cache')) && !empty($cachedCopy->data) && (time() < $cachedCopy->expire )) { ...reuse cached data } else { ..rebuild cache cache_set($cid,$availableOnOtherSites,'cache_custom_cache',time() + (60*1)); }