ZF-9148: Zend_Cache_Backend_Static doesn't work properly
Description
I'm trying to use Zend_Cache_Backend_Static like this:
protected function _initCache()
{
$manager = new Zend_Cache_Manager();
$manager->setCacheTemplate('page', array(
'frontend' => array(
'name' => 'Capture',
'options' => array(
'ignore_user_abort' => true,
'automatic_serialization' => true
),
),
'backend' => array(
'name' => 'Static',
'options' => array(
'public_dir' => 'cache',
),
),
));
$manager->getCache('page')->start('nothing', array());
}
Firstly, why i have to specify {{$id}} and {{$tag}} in {{start()}}? Why not just {{$manager->getCache('page')->start()}}?
Then, in Zend_Cache_Backend_Static:
if (is_null($id) || strlen($id) == 0) {
$id = $this->_detectId();
} else {
$id = $this->_decodeId($id);
}
But {{$id}} couldn't ever be {{null}} or {{""}}. It's impossible to call {{start('', array())}}, because {{Zend_Cache_Core::validateIdOrTag}} will throw exception {{"Invalid id or tag '$string' : must use only [a-zA-Z0-9]"}}. So, {{$id = $this->_detectId();}} will never be executed, and {{$id = $this->_decodeId($id);}} will produce wrong filename.
Then, in ``` {{$pathName}} become {{"bla-bla/public/cache."}}, and name of file to be created become {{"bla-bla/public/cache./index.html"}}.
So, is it's my mistake or it's really BUG?
Comments
Posted by Ramon Henrique Ornelas (ramon) on 2010-02-11T09:05:03.000+0000
Reassigned for Padraic
Posted by Pádraic Brady (padraic) on 2010-02-11T09:31:54.000+0000
It's important to remember that the static cache doesn't work like the other caches. It's designed to be a transparent backend system triggered from a controller action, and as such, it's entire operation is dictated by the Zend_Controller_Action_Helper_Cache class. About the only thing you should change is the location of the public_dir (you're using a non-path above - use the absolute path).
To offer a few points of difference:
IDs are the request URI, however the ID passed to the backend must first be encoded as hexadecimal characters (i.e. bin2hex()) to bypass the Zend_Cache_Core ID restrictions. The request URI may be empty (root URI) and detectId() simply adds a check to ensure this isn't an error passed along by mistake.
Tags are not stored to the static cache, since the static cache holds files to be served OUTSIDE of PHP. The tags are stored to an "inner" cache instead. The inner cache is managed by the backend, and so tags need to be passed at that point.
Data is never serialized unless it also requires a custom extension name outside of the default option. So setting automatic_serialization, as above, should not be done.
The cache is intended to be managed via the Cache action manager which allows you to setup which actions in a controller to cache, with what extension, and with which tags. It also offers methods for removing/purging the cache using the request URI or tags (the tag cleaning is similar to other backends). Use outside of the action helper should use the helper itself as a reference point for manual management. Manually, you would also need to register a helper/plugin to trigger the Capture frontend.
Posted by Pádraic Brady (padraic) on 2010-02-11T09:38:44.000+0000
Here's an example from a current project:
.htaccess (includes extra settings for local XML feeds):
application.ini:
IndexController (Cached):
Cache Purging (Here, after a feed is polled (updates index/pages with new content)):
This works perfectly fine here.
Posted by Nikita Afanasenko (nikitug) on 2010-02-11T09:46:24.000+0000
Padraic, thanks a lot!
Posted by Pádraic Brady (padraic) on 2010-03-16T05:22:35.000+0000
Closing - report was the result of a misundertanding of how the static cache operated. Nothing to fix ;).