Skip to main content
6 of 6
Rollback to Revision 4 - Edit approval overridden by post owner or moderator
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How to check if result from cache_get() is expired?

cache_get always returns an object and I'm a bit confused by its values. There's probably a short generic function that can handle this for once and for all.

$cache = cache_get('key');
if ($cache) { 
  // doesn't work, cache_get always returns an object
}

if (!$cache || ($cache->created < time() - $cache->expire)) {
  // cache is expired? I'm not sure
}

Just checking for the data member doesn't cut it.

I had seen the issue reported on d.o (in 2009) but that doesn't really help me fix it on any live site. So I'm looking for a function that will still make cache work for me without resorting to storing the cache myself in the database.

the
  • 1.7k
  • 1
  • 18
  • 37