views:

62

answers:

1

I've the following function:

function Cache($key, $value = null, $ttl = 60)
{
    if (isset($value) === true)
    {
        apc_store($key, $value, intval($ttl));
    }

    return apc_fetch($key);
}

And I'm testing it using the following code:

Cache('ktime', time(), 3); // Store

sleep(1);
var_dump(Cache('ktime') . '-' . time()); echo '<hr />'; // Should Fetch
sleep(5);
var_dump(Cache('ktime') . '-' . time()); echo '<hr />'; // Should NOT Fetch
sleep(1);
var_dump(Cache('ktime') . '-' . time()); echo '<hr />'; // Should NOT Fetch
sleep(1);
var_dump(Cache('ktime') . '-' . time()); echo '<hr />'; // Should NOT Fetch

And this is the output:

string(21) "1273966771-1273966772"
string(21) "1273966771-1273966777"
string(21) "1273966771-1273966778"
string(21) "1273966771-1273966779"

Shouldn't it look like this:

string(21) "1273966771-1273966772"
string(11) "-1273966777"
string(11) "-1273966778"
string(11) "-1273966779"

I don't understand, can anyone help me figure out this strange behavior?

+2  A: 

Apparently this is a known issue in APC 3.0.16 (2007-12-26) and later, and will not be fixed.

http://pecl.php.net/bugs/bug.php?id=13331

The ttl works, but "t" is a constant during a given request. So a cached object does not expire until at least the next request.

Bill Karwin
Oh! Thats what the "on the next request" means, I though *request* meant on the next `apc_fetch()` call. Thank you Bill, now I can take advantage of this "bug". =)
Alix Axel