2

I'm trying to implement a cache using Zend Cache. I use the following code to initialize the caches.

$tagCache = Zend_Cache::factory('Core',
                                'File',
                                 array('automatic_serialization' => true),
                                 array('cache_dir' => $cfg['instdir']. 'Cache_dir'));

$cache = Zend_Cache::factory('Capture',
                             'Static',
                              array(),
                              array('index_filename' => 'index',
                                    'public_dir'     => $cfg['instdir'] . 'Cached',
                                    'tag_cache'      => $tagCache));

I use the following code to start caching:

$id = bin2hex($_SERVER["REQUEST_URI"]);
$cache->start($id, array());

The cache files are generated but I can't delete them using the remove() method (the cache doesn't refresh):

$cache->remove($id); // id generated like above from the REQUEST_URI of the page I want to refresh

What am I doing wrong ? Thanks.

Iansen
  • 1,268
  • 1
  • 10
  • 14
  • Are there any permission related errors outputting to your error log? And did you validate that the $id values are exactly the same? (for start() and remove()) – Mike Purcell Feb 19 '12 at 23:16
  • I don't get any errors in my logs. I've checked the ids ... I even created a test file with some hard coded values for the ids. But no luck. I've opened some of the cache files and they seem to be created as they should...but when I want to remove them nothing happens. The cache directories have the necessary read, write permissions. – Iansen Feb 19 '12 at 23:40
  • 1
    Are you wrapping a try/catch block around the remove() call? You may have to dig around their API docs to uncover the problem. – Mike Purcell Feb 20 '12 at 05:00
  • It turns out that you have to pass the path to the cache file to the remove method in order for it to work. Although the Zend Documentation says it takes a cache id.anyway if anyone else has a similar problem .... just add $id = $this->_decodeId($id); to the remove method in the Static.php backend. – Iansen Feb 21 '12 at 15:59

1 Answers1

2

$cache->remove() is a proxy to the backend's remove() method. In this case you are using the Static backend and so we look in there to find out what's happening.

My reading of that method leads me to believe that the $id parameter to remove has to be a filename, so:

$cache->remove('index'); 

will work.

The more usual way to clear a cache is to use the clean() method though. See the manual.

Rob Allen
  • 12,643
  • 1
  • 40
  • 49