キャッシュ アダプタからデフォルトの有効期間を設定する必要がありますが、奇妙なことが起こっています。:/
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();
}
参照http://symfony.com/doc/current/components/cache/cache_pools.html#filesystem-cache-adapter
初回は、キャッシュされたファイルの内容が次のように表示されます。
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";}
もちろん、このアイテムは 5 秒後に期限切れになりません:/ (手動でキャッシュ ディレクトリをクリアしました)。
一方、$latestNews->expiresAfter(5);
all を使用すると、次のように正常に動作します。
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";}
参照http://symfony.com/doc/current/components/cache/cache_items.html#cache-item-expiration
アイテムが正しく期限切れになってから 5 秒。
私はそれをテストしSymfony\Component\Cache\Adapter\ApcuAdapter
、同じ問題も発生します。
キャッシュ アダプターの既定の有効期間 (コンストラクター パラメーター) はどうなりますか? ここに何かが欠けています:/ ?