Why key_1
is not cleared when execution time is greater than TTL
?
$ttl = 3;
$key = 'key_1';
if (apc_exists($key)) {
echo "Data exists!
";
if (apc_delete($key))
echo "Data deleted.
";
else
die("Unable to delete data
");
} else {
echo "Data not present.
";
}
apc_add($key, 'some-value', $ttl);
$start = microtime(true);
echo "TTL: ".$ttl."sec
";
while (1) {
sleep(1);
$exec_time = round(microtime(true) - $start, 1);
$data = apc_fetch($key);
if (!empty($data)) {
echo 'key_1: ['.$data.']: '
.$exec_time."sec"
.(($exec_time > $ttl) ? "\t<<< Data still exists!
" : "
");
} else {
die("Data was cleared!
");
}
}
I read the docs a little more and I found the answer. Under the section for the TTL parameter:
Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored variable will be expunged from the cache (on the next request). If no ttl is supplied (or if the ttl is 0), the value will persist until it is removed from the cache manually, or otherwise fails to exist in the cache (clear, restart, etc.).
The key part is the first set of brackets; (on the next request)