Zend_Cache缓存文件消失

发布于 2024-10-30 16:16:34 字数 795 浏览 0 评论 0原文

我有一个 zend 缓存,它存储数据库中的数组。缓存可以被读取和读取更新好了。但实际的缓存文件似乎在一天左右后就消失了。我认为添加 automatic_cleaning_factor = 0 可以解决这个问题,但事实似乎并非如此。

$frontendOptions = array(
    'caching' => true,
    'cache_id_prefix' => 'mysite_blah',
    'lifetime' => 14400, # 4 hours
    'automatic_serialization' => true,
    'automatic_cleaning_factor' => 0,
);
$backendOptions = array(
    'cache_dir' => "{$_SERVER['DOCUMENT_ROOT']}/../../cache/zend_cache/"
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

if(!$result = $cache->load('test_blah'))
{
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
}
else
{
     $result = $cache->load('test_blah');
}

使用此缓存的页面不是很受欢迎,不确定这是否与它有关......有什么想法吗?

I have a zend cache which stores an array from the db. The cache can be read & updated fine. But the actual cache file seems to disappear after a day or so. I thought that adding automatic_cleaning_factor = 0 would solve this, but that doesn't seem to be the case.

$frontendOptions = array(
    'caching' => true,
    'cache_id_prefix' => 'mysite_blah',
    'lifetime' => 14400, # 4 hours
    'automatic_serialization' => true,
    'automatic_cleaning_factor' => 0,
);
$backendOptions = array(
    'cache_dir' => "{$_SERVER['DOCUMENT_ROOT']}/../../cache/zend_cache/"
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

if(!$result = $cache->load('test_blah'))
{
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
}
else
{
     $result = $cache->load('test_blah');
}

The page which uses this cache isn't very popular, not sure if that has anything to do with it..... Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-11-06 16:16:34

我无法解释为什么您的文件消失了,我认为您的示例中缺少某些内容,可能会删除缓存文件。不过,我可以提供一些可能对您有所帮助的注释...

我认为您误解了 automatic_cleaning_factor 参数。这不会阻止您的缓存过期。这只会禁止缓存前端自动清理在您调用 save() 方法时可能已过期的任何缓存。

当您 load() 缓存时,它仍然会进行有效性测试,如果无效,它将被忽略,并且将查询您的数据库以获取新数据。您可以将 true 作为 load() 的第二个参数传递,以忽略有效性测试。

但是您可能应该将生命周期设置为很长的时间,而不是覆盖缓存验证,因为它的存在是有原因的。

此外,此代码还可以简化:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
} else {
     $result = $cache->load('test_blah');
}

至:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...   
    $result = $my_array_from_db;
    $cache->save($result, 'test_blah');
}

不需要 else,因为缓存中的 $result 已在 if() 语句中分配。

I cannot explain why your file is vanishing, I think something is missing from your sample that is probably removing the cache file. I can however have a few notes that might help you out...

I think you're misinterpreting the automatic_cleaning_factor parameter. This does not disable your cache from expiring. This only disables the cache frontend from automatically cleaning any cache that may have expired when you call the save() method.

When you load() your cache it still gets tested for validity, and if invalid, it gets ignored and your database will be queried for fresh data. You can pass true as a second parameter on the load() to ignore the validity test.

However you should probably just set your lifetime to a very long time instead of overriding cache validation, because it's there for a reason.

Also, this code can be simplified:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
} else {
     $result = $cache->load('test_blah');
}

To:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...   
    $result = $my_array_from_db;
    $cache->save($result, 'test_blah');
}

The else is not needed because $result from your cache is assigned in the if() statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文