Patterns for clearing Zend Cache
Asked Answered
S

2

6

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?

Symonds answered 28/4, 2011 at 21:47 Comment(0)
T
10

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/

Trona answered 28/4, 2011 at 22:7 Comment(2)
Thanks for your time duke. As regilero pointed out below APC doesn't support tags. Which backend would you use in this case to support tags?Symonds
How many cache entries are we talking about? If its 10 or 20 I would just delete each entry. Depending on your needs you could consider saving a serialized associative array into the cache instead of individual entries. This is not a good idea if you have to update/delete individual entries often thoughTrona
H
4

@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.

Helper answered 30/4, 2011 at 22:11 Comment(2)
Which backend supports tags? It seems like most of the backends don't from the Zend ManualSymonds
from framework.zend.com/manual/en/zend.cache.backends.html you can see tags are available for file backend, SQLite backend and partially on ZendPlatform.Helper

© 2022 - 2024 — McMap. All rights reserved.