清除 Zend Cache 的模式

发布于 2024-11-04 15:15:26 字数 951 浏览 2 评论 0原文

我开始使用 Zend Cache(APC 后端),并且在返回缓存值而不是每次都访问数据库方面一切都很好。但是,我的问题是:

$cache_key = 'getrebates_'.$operator_code;

if(PP_Model_CacheService::exists($cache_key)) {
    $cached_values = PP_Model_CacheService::load($cache_key);
} else {
   //hits the db    
   $cached_values = $this->getAll($operator_code);
   PP_Model_CacheService::save($cached_values, $cache_key);
}
return $cached_values;

每个运营商都有自己的回扣,每个运营商之间的回扣有所不同,现在如果我更改数据库并需要清除所有运营商的回扣,我该怎么做?

我可以使用 $Cache->clean(),但这会清除其他缓存(不仅仅是每个运营商的回扣缓存)。如果我循环遍历所有运算符:

foreach($operator_codes AS $operator_code) {
   $cache_key = 'getrebates_'.$operator_code;
   $cache->delete($cache_key)
}

这似乎需要为缓存做很多工作。有没有办法只清除一部分缓存。

//Something like:
$section_key = 'getrebates';
$Cache[$section_key][$operator_code];
$Cache->clearSection($section_key);

APC 缓存是否有任何数组结构,或者都是基于缓存键/值?

I started using Zend Cache (APC backend) and all is well in terms of returning cached values instead of hitting the Database each time. However, heres my problem:

$cache_key = 'getrebates_'.$operator_code;

if(PP_Model_CacheService::exists($cache_key)) {
    $cached_values = PP_Model_CacheService::load($cache_key);
} else {
   //hits the db    
   $cached_values = $this->getAll($operator_code);
   PP_Model_CacheService::save($cached_values, $cache_key);
}
return $cached_values;

Each operator has their own rebates which vary between operators, now if I change the database and need to clear the rebates for all the operators, how would I do this?

I can use $Cache->clean(), but that will clear the other caches (not just the rebate cache for each operator). If I loop through all operators:

foreach($operator_codes AS $operator_code) {
   $cache_key = 'getrebates_'.$operator_code;
   $cache->delete($cache_key)
}

That seems like alot of work for the cache. Is there a way to clear just a section of Cache.

//Something like:
$section_key = 'getrebates';
$Cache[$section_key][$operator_code];
$Cache->clearSection($section_key);

Is there any array structure to the APC cache or is it all cache key/value based?

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

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

发布评论

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

评论(2

半﹌身腐败 2024-11-11 15:15:26

您可以将标签应用于存储在缓存中的值。这样您就可以轻松删除具有特定标签的所有缓存条目。

$cache->save($huge_data, 'myUniqueID', array('tagA', 'tagB'));

// clear all cache entries with tag tagA or tagC
$cache->clean(
  Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  array('tagA', 'tagC')
);

请参阅此页面: http://framework.zend.com/manual/ en/zend.cache.theory.html 以及有关 Zend_Cache_Core clean 方法的详细信息的 API: http://framework.zend.com/apidoc/1.11/

You can apply tags to values stored in the cache. That way you can easily delete all cache entries which have a certain tag.

$cache->save($huge_data, 'myUniqueID', array('tagA', 'tagB'));

// clear all cache entries with tag tagA or tagC
$cache->clean(
  Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  array('tagA', 'tagC')
);

Refer to this page: http://framework.zend.com/manual/en/zend.cache.theory.html and the API for details about the clean method of Zend_Cache_Core: http://framework.zend.com/apidoc/1.11/

醉酒的小男人 2024-11-11 15:15:26

@theduke 是对的,标记是正确的方法,除了 APC,因为 Zend_Cache_Backend_Apc 不支持标记。来自文档

小心:使用这个后端,“标签”
暂时不支持

从您最后的评论来看,您似乎正在使用 APC 作为后端。因此,要么扩展此类并添加标签行为(通过在标签标识符中添加特殊语法?通过在其他地方处理标签与缓存条目映射?在长期缓存条目中?),或者决定使用另一个缓存后端。

@theduke is right, tagging is the right way to do it, except for APC, as Zend_Cache_Backend_Apc does not support tagging. From the doc:

Be careful : with this backend, "tags"
are not supported for the moment

And from your last comment it sems you are using APC as a backend. So either you extend this class and add the Tag behavior (by adding a special syntax in tag identifier? by handling the tag vs cache entry mapping somewhere else?, in a long-term cache entry?), or you decide to use another cache backend.

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