I need configure default lifetime from cache Adapter, but something weird has been happening, the follows don't works!? :/
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
// in seconds; applied to cache items that don't define their own lifetime
// 0 means to store the cache items indefinitely (i.e. until the files are deleted)
$cache = new FilesystemAdapter('my_namespace', 5); // <-- default lifetime 5 seconds
$latestNews = $cache->getItem('latest_news');
if (!$latestNews->isHit()) {
$news = ['title' => '...', 'createdAt' => (new \DateTime())->format('Y-m-d H:i:s')];
$cache->save($latestNews->set($news));
} else {
$news = $latestNews->get();
}
Reference http://symfony.com/doc/current/components/cache/cache_pools.html#filesystem-cache-adapter
The first time, the cached file content shows:
2147483647 <-- 2038-01-18 22:14:07 :/ ?
latest_news
a:2:{s:5:"title";s:3:"...";s:9:"createdAt";s:19:"2016-10-07 09:16:50";}
and of course this item don't expire after 5 seconds :/ (I've cleared the cache directory manually).
On the other hand, if we use $latestNews->expiresAfter(5);
all works fine:
1475849350 <-- 2016-10-07 10:09:10 \o/ OK
latest_news
a:2:{s:5:"title";s:3:"...";s:9:"createdAt";s:19:"2016-10-07 10:09:05";}
Reference http://symfony.com/doc/current/components/cache/cache_items.html#cache-item-expiration
5 seconds after the item expired correctly.
I tested that with Symfony\Component\Cache\Adapter\ApcuAdapter
and occurs the same problem too.
What happens with default lifetime (constructor parameter) in cache adapters ? I missing something here :/ ?
Is an old issue [Cache] Fix default lifetime being ignored that affect framework version prior to the 3.1
Upgrading the Symfony framework should fix it.