Efficient caching with NamespaceFileEngine

By Frank de Graaf (Frank)
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

Page 2: The Engine

Comments 971

CakePHP Team Comments Author Comments
 

Question

1 Not Reading Prefix

For some reason, I keep getting files saved as 'cake_ACOALIAS_model__user'. They overwrite each other, too. So I'll be logged in as an admin, and it will create 'cake_posts_model__user'. Then I log out, and it creates 'cake_posts_model__user', but with the guest permissions.

I've configured it the way you said, but I think something's gone wrong. Any help?
Posted Apr 11, 2009 by Paddy Foran
 

Question

2 Getting help

For some reason, I keep getting files saved as 'cake_ACOALIAS_model__user'. They overwrite each other, too. So I'll be logged in as an admin, and it will create 'cake_posts_model__user'. Then I log out, and it creates 'cake_posts_model__user', but with the guest permissions.

I've configured it the way you said, but I think something's gone wrong. Any help?
I have a few question about your problem. It doesn't seem it is using this engine at all. If it would, you would see it creates folders and files instead of one file only. What i see in your description seems like cake's own FileEngine.

Can you please find me in #cakephp after easter, so I can help you easier? That would be great.
Posted Apr 12, 2009 by Frank de Graaf
 

Comment

3 issue in 1.2 RC3

The File object created in __setPath did not work. I guess it does in the final version of 1.2?
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;
    }
Posted Apr 22, 2009 by Jan Hansen
 

Comment

4 Better idea

A core upgrade might have been a better idea... ;)
Posted Apr 22, 2009 by Frank de Graaf
 

Comment

5 Yes, but...

A core upgrade might have been a better idea... ;)
...not 10 days from launch! :-)
Posted Apr 22, 2009 by Jan Hansen
 

Comment

6 Works perfectly

Thank you for this good piece of code, Frank!
Posted May 15, 2009 by Yuriy
 

Question

7 Impact on Cake's own methods

Hey Frank,

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.
Posted Jun 11, 2009 by Ben Pesso
 

Comment

8 None, unless...

This method has no impact on any other caching method, unless you tell Cake to use this by default, which you should not do.

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.
Posted Jun 11, 2009 by Frank de Graaf
 

Comment

9 Great!

This method has no impact on any other caching method, unless you tell Cake to use this by default, which you should not do.

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.
Thanks for clarifying that. I guess I should learn more about how Cake handles caching... :)
Posted Jun 11, 2009 by Ben Pesso
 

Comment

10 error

I'm getting a Cannot modify header information error when trying to use this. I think I installed the namespace engine successfully and followed the instructions here exactly but something is off. Any ideas?
Posted Jun 11, 2009 by Ken Frey
 

Comment

11 RE: error

I'm getting a Cannot modify header information error when trying to use this. I think I installed the namespace engine successfully and followed the instructions here exactly but something is off. Any ideas?
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.
Posted Jun 11, 2009 by Frank de Graaf
 

Comment

12 Download

Hi,

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.
Posted Jul 7, 2009 by Mac Duy Hai
 

Comment

13 RE: error

Hi Frank,
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

I'm getting a Cannot modify header information error when trying to use this. I think I installed the namespace engine successfully and followed the instructions here exactly but something is off. Any ideas?
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.
Posted Sep 8, 2009 by Simon Brüchner