Efficient caching with NamespaceFileEngine
In my attempt to cache ACL permissions I stumbled upon a limitation of the current FileEngine. It is unable to drop a group of keys, only all keys or just one. Here is an alternative engine (which basically is a partial reimplementation of the current engine) that supports this. This engine is not to replace the original one and should not be used as the App's default engine.
How it works
Basically, this Engine builds for every namespace a folder, so you get a nice nested tree of cache files. The last key of a path will be the name of the cache file. So you can see the separating dot as a DIRECTORY_SEPARATOR or DS in Cake terms. This structure is needed to delete groups of cache files or better said, parent namespaces.The benefit of this is that you can delete a certain part of the cache and not the whole cache. The cache for the other namespaces remains intact, which makes this type of deletion more efficient, since it doesn't have to rebuild the entire cache.
This Engine uses the features of the File and Folder classes to create and delete the cache tree. The way the content of the cache files is saved is the same as the original FileEngine.
Configuration
Before you start, download the file below and place it in:APP/vendors/namespace_file.php
Next, configure the engine in:
APP/config/bootstrap.php
By placing this in bootstrap.php:
Download code
<?php
App::import('Vendor', 'NamespaceFile');
Cache::config('app', array(
'engine' => 'NamespaceFile',
'duration'=> '+1 hour',
'prefix' => 'cake.'
));
?>
Usage
It is easy, just use it as you would with the old Engine, but with dot separated key to separate the namespaces:Download code
<?php
Cache::write('app.pictures.recent', $recentPictures, 'app');
Cache::write('app.pictures.top', $topPictures, 'app');
?>
And to delete all defined pictures keys:
Download code
<?php
Cache::delete('app.pictures', 'app');
?>
Phally
http://www.frankdegraaf.net
Comments
Question
1 Not Reading Prefix
I've configured it the way you said, but I think something's gone wrong. Any help?
Question
2 Getting help
Can you please find me in #cakephp after easter, so I can help you easier? That would be great.
Comment
3 issue in 1.2 RC3
I kept getting error that app/tmp could not be opened as it was a dir.
Anyway, this little hack fixes it:
function __setPath($path) {
$this->__File->Folder->cd($path);
if (is_dir($path)) {
$object = &$this->__File->Folder;
return $object;
}
//$this->__File->path = $path;
$pos = 1+strrpos($path, "/");
$this->__File->Folder->path = substr($path, 0, $pos);
$this->__File->name = substr($path, $pos);
$object = &$this->__File;
return $object;
}
Comment
4 Better idea
Comment
5 Yes, but...
...not 10 days from launch! :-)
Comment
6 Works perfectly
Question
7 Impact on Cake's own methods
How would this affect Cake's own caching, such as view/element caching? Will Cake's methods still work, or is an ugly hack needed for that as well?
I'd love to use your ACL caching component, but if the Namespace Cache engine screws up the element caching than that might be a problem.
Comment
8 None, unless...
When you create one or many cache configurations in bootstrap.php, you will need to tell Cake to use it else it would just use the default.
Comment
9 Great!
Comment
10 error
Comment
11 RE: error
Probably some other error occures before that or maybe a debug() or echo. It isn't likely the Engine has something to do with it since it doesn't output anything.
Comment
12 Download
this is a really great component, but how do I download it? There doesn't seem to be a link :/
Thanks.
Hai
EDIT: Don't worry, I've found it ;). Sorry to bother.
Comment
13 RE: error
it's a whitespace letter after the closing "?>" in the code. Maybe it occurs only in windows and with copy and paste? If you would remove the "?>" the error can't occur.
Simon
Comment
14 Nice Post.