我想将一些XML存储在Zend文件系统缓存中,并在30分钟后过期。如何设置缓存持续时间/到期时间?我使用Zend缓存作为一个组件,而不是在完整的ZF2应用程序的上下文中。
$cache = ZendCacheStorageFactory::factory(array(
'adapter' => array(
'name' => 'filesystem',
'ttl' => 60, // kept short during testing
'options' => array('cache_dir' => __DIR__.'/cache'),
),
'plugins' => array(
// Don't throw exceptions on cache errors
'exception_handler' => array(
'throw_exceptions' => false
),
)
));
$key = 'spektrix-events';
$events = new SimpleXMLELement($cache->getItem($key, $success));
if (!$success) {
$response = $client->setMethod('GET')->send();
$events = new SimpleXMLElement($response->getContent());
$cache->setItem('spektrix-events', $events->asXML());
}
var_dump($cache->getMetadata($key)); // the mtime on the file stays the same as does timestamp from ls -al in a terminal.
如何设置过期时间,然后检查缓存是否已过期?上面的代码似乎不会在60秒后使缓存过期(.dat文件的时间戳不会更改)
您是否尝试在适配器选项中设置选项ttl
?
'adapter' => array(
'name' => 'filesystem',
'options' => array(
'cache_dir' => __DIR__.'/cache',
'ttl' => 3600,
),
),
ZF文档甚至有一些不错的快速启动示例,其中介绍了TTL。
更新:
我已经测试了下一个脚本,TTL正在正常工作。你在其他地方有问题。
$cache = ZendCacheStorageFactory::factory(array(
'adapter' => array(
'name' => 'filesystem',
'options' => array('ttl' => 5),
),
));
$cache->setItem('a', 'b');
for ($i = 1; $i <= 7; $i++) {
sleep(1);
echo "var_dump on {$i}th second ... ";
var_dump($cache->getItem('a'));
}
输出为:
var_dump on 1th second ... string(1) "b"
var_dump on 2th second ... string(1) "b"
var_dump on 3th second ... string(1) "b"
var_dump on 4th second ... string(1) "b"
var_dump on 5th second ... NULL
var_dump on 6th second ... NULL
var_dump on 7th second ... NULL