PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

APC: get a key's expiration time

Note: This article was originally published at Planet PHP on 9 November 2010.
Planet PHP

It's always surprising to me, but APC is still the best kept secret.

APC offers a bunch of very useful features - foremost a realpath cache and an opcode cache. However, my favorite is neither: it's being able to cache data in shared memory. How so? Simple: use apc_store() and apc_fetch() to persist data between requests.

The other day, I wanted use a key's expiration date to send the appropriate headers (Expires and Last-Modified) to the client, but it didn't seem like APC supports this out of the box yet.

Here is more or less a small hack until there's a native call:

/** * Return a key's expiration time. * * @param string $key The name of the key. * * @return mixed Returns false when no keys are cached or when the key * does not exist. Returns int 0 when the key never expires * (ttl = 0) or an integer (unix timestamp) otherwise. */ function apc\_expire($key) { $cache = apc\_cache\_info('user'); if (empty($cache['cache\_list'])) { return false; } foreach ($cache['cache\_list'] as $entry) { if ($entry['info'] != $key) { continue; } if ($entry['ttl'] == 0) { return 0; } $expire = $entry['creation_time']+$entry['ttl']; return $expire; } return false; }