Efficient caching with NamespaceFileEngineIn 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.http://bakery.cakephp.org/Possible solutionhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea20a-19ac-4b2f-887d-495782f0cb67#comment4caea20a-19ac-4b2f-887d-495782f0cb67http://book.cakephp.org/view/1561/Migrating-from-CakePHP-1-2-to-1-3 According to that link, Folder::ls() is deprecated in 1.3, which is what I'm using, and read() should be used instead. I'll make the change to my code and see what happens. Fri, 10 Sep 2010 14:44:08 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea20a-19ac-4b2f-887d-495782f0cb67#comment4caea20a-19ac-4b2f-887d-495782f0cb67Intermittent missing methodhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea20a-82b0-4f6d-bbda-42cd82f0cb67#comment4caea20a-82b0-4f6d-bbda-42cd82f0cb67EDIT: I should say that this is not with Rolf's modified version, but rather Phally's. Haven't tried changing to Rolf's. I'm unsure if this is a problem with my developing on WAMP locally or if something else is going on. I get intermittent fatal errors for an undefined method ls() in the clear() method. A page refresh pushed things on through, but then it would happen again at some seemingly random point. I dropped this code in to help work around it: [code] if (method_exists($deletable, 'ls')) { $contents = $deletable->ls(); } else { debug_print_backtrace(); } [/code] That gave me the following backtrace (a few times): [code]#0 NamespaceFileEngine->clear(1) called at [C:\wamp\www\artemis\app\vendors\namespace_file.php:110] #1 NamespaceFileEngine->gc() called at [C:\wamp\www\artemis\cakephp\cake\libs\cache.php:153] #2 Cache->_buildEngine(ns_permissions) called at [C:\wamp\www\artemis\cakephp\cake\libs\cache.php:126] #3 Cache->config(ns_permissions, Array ([engine] => NamespaceFile,[duration] => +1 year,[probability] => 100,[path] => C:\wamp\www\artemis\app\tmp\cache\cached_permissions\,[prefix] => AclPerms.)) called at [C:\wamp\www\artemis\app\config\bootstrap.php:97] #4 include(C:\wamp\www\artemis\app\config\bootstrap.php) called at [C:\wamp\www\artemis\cakephp\cake\libs\configure.php:432] #5 Configure->__loadBootstrap(1) called at [C:\wamp\www\artemis\cakephp\cake\libs\configure.php:52] #6 Configure::getInstance() called at [C:\wamp\www\artemis\cakephp\cake\bootstrap.php:38] #7 include(C:\wamp\www\artemis\cakephp\cake\bootstrap.php) called at [C:\wamp\www\artemis\app\webroot\index.php:76] [/code] After that happens, all of the cached items are gone and need to be rebuilt. Any ideas what's happening? Again, it's intermittent. I might not see it for a few days or I might see more than one in a day.Fri, 10 Sep 2010 13:55:27 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea20a-82b0-4f6d-bbda-42cd82f0cb67#comment4caea20a-82b0-4f6d-bbda-42cd82f0cb67Githubhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea209-ee48-4c38-817d-4cee82f0cb67#comment4caea209-ee48-4c38-817d-4cee82f0cb67Right, maybe I should add it to my Github account. This was from the time I didn't use GitHub actually, hence it is not there. Will be easier to contribute too. Good job Rolf in refactoring it!Mon, 06 Sep 2010 12:26:38 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea209-ee48-4c38-817d-4cee82f0cb67#comment4caea209-ee48-4c38-817d-4cee82f0cb67Extending FileEngine insteadhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea209-a480-402f-a1f1-4ad782f0cb67#comment4caea209-a480-402f-a1f1-4ad782f0cb67Frank- very helpful class! I modified to to extend FileEngine instead, which results in a more compact class. Seems to work just great with Cake 1.2.7. More details here: [url]http://blog.echothis.com/2010/09/04/a-simpler-namespacefile-engine-for-cakephp-1-2/[/url]Sat, 04 Sep 2010 17:14:53 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea209-a480-402f-a1f1-4ad782f0cb67#comment4caea209-a480-402f-a1f1-4ad782f0cb67Post to githubhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea205-0230-414b-b287-40b882f0cb67#comment4caea205-0230-414b-b287-40b882f0cb67It would be nice if this was on github so we could clone/submodule from there.Wed, 25 Aug 2010 09:23:06 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea205-0230-414b-b287-40b882f0cb67#comment4caea205-0230-414b-b287-40b882f0cb67RE:Downloadhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea1ed-ab78-46ca-8d8a-4ece82f0cb67#comment4caea1ed-ab78-46ca-8d8a-4ece82f0cb67Hello! where did u find the component... I didn't get it yet, can u help me plz??? [quote]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.[/quote]Thu, 13 May 2010 03:51:12 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea1ed-ab78-46ca-8d8a-4ece82f0cb67#comment4caea1ed-ab78-46ca-8d8a-4ece82f0cb67RE: errorhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea1a3-82ec-4986-b65c-420c82f0cb67#comment4caea1a3-82ec-4986-b65c-420c82f0cb67Hi 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 [quote][quote]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?[/quote] 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.[/quote]Tue, 08 Sep 2009 08:04:05 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea1a3-82ec-4986-b65c-420c82f0cb67#comment4caea1a3-82ec-4986-b65c-420c82f0cb67Downloadhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea18d-2170-4475-ae97-4cd282f0cb67#comment4caea18d-2170-4475-ae97-4cd282f0cb67Hi, 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.Tue, 07 Jul 2009 08:24:03 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea18d-2170-4475-ae97-4cd282f0cb67#comment4caea18d-2170-4475-ae97-4cd282f0cb67RE: errorhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-4468-4b7d-a38f-47e882f0cb67#comment4caea184-4468-4b7d-a38f-47e882f0cb67[quote]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?[/quote] 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.Thu, 11 Jun 2009 07:16:38 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-4468-4b7d-a38f-47e882f0cb67#comment4caea184-4468-4b7d-a38f-47e882f0cb67errorhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-78d4-419e-be9a-49f082f0cb67#comment4caea184-78d4-419e-be9a-49f082f0cb67I'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?Thu, 11 Jun 2009 06:46:58 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-78d4-419e-be9a-49f082f0cb67#comment4caea184-78d4-419e-be9a-49f082f0cb67Great!http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-7334-406f-b8d1-453582f0cb67#comment4caea184-7334-406f-b8d1-453582f0cb67[quote]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.[/quote] Thanks for clarifying that. I guess I should learn more about how Cake handles caching... :)Thu, 11 Jun 2009 03:06:27 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-7334-406f-b8d1-453582f0cb67#comment4caea184-7334-406f-b8d1-453582f0cb67None, unless...http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-aa10-4096-8b4e-471682f0cb67#comment4caea184-aa10-4096-8b4e-471682f0cb67This 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.Thu, 11 Jun 2009 02:58:20 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-aa10-4096-8b4e-471682f0cb67#comment4caea184-aa10-4096-8b4e-471682f0cb67Impact on Cake's own methodshttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-0668-42ab-83cd-4ec582f0cb67#comment4caea184-0668-42ab-83cd-4ec582f0cb67Hey 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.Thu, 11 Jun 2009 02:50:26 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea184-0668-42ab-83cd-4ec582f0cb67#comment4caea184-0668-42ab-83cd-4ec582f0cb67Works perfectlyhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea179-51a0-4d49-b652-4ddc82f0cb67#comment4caea179-51a0-4d49-b652-4ddc82f0cb67Thank you for this good piece of code, Frank! Fri, 15 May 2009 09:37:41 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea179-51a0-4d49-b652-4ddc82f0cb67#comment4caea179-51a0-4d49-b652-4ddc82f0cb67Yes, but...http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-3e60-408c-bb4a-4e0f82f0cb67#comment4caea171-3e60-408c-bb4a-4e0f82f0cb67[quote]A core upgrade might have been a better idea... ;)[/quote] ...not 10 days from launch! :-)Wed, 22 Apr 2009 14:15:35 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-3e60-408c-bb4a-4e0f82f0cb67#comment4caea171-3e60-408c-bb4a-4e0f82f0cb67Better ideahttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-b9c0-41d3-9e1b-4dff82f0cb67#comment4caea171-b9c0-41d3-9e1b-4dff82f0cb67A core upgrade might have been a better idea... ;)Wed, 22 Apr 2009 09:21:07 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-b9c0-41d3-9e1b-4dff82f0cb67#comment4caea171-b9c0-41d3-9e1b-4dff82f0cb67issue in 1.2 RC3http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-0b68-4bd2-a20e-471d82f0cb67#comment4caea171-0b68-4bd2-a20e-471d82f0cb67The 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: [code] 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; } [/code]Wed, 22 Apr 2009 08:43:12 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea171-0b68-4bd2-a20e-471d82f0cb67#comment4caea171-0b68-4bd2-a20e-471d82f0cb67Getting helphttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea16e-a1f0-4f08-8415-4d4782f0cb67#comment4caea16e-a1f0-4f08-8415-4d4782f0cb67[quote]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?[/quote] 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.Sun, 12 Apr 2009 05:26:52 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea16e-a1f0-4f08-8415-4d4782f0cb67#comment4caea16e-a1f0-4f08-8415-4d4782f0cb67Not Reading Prefixhttp://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea16d-0228-4ae1-8ef3-44ac82f0cb67#comment4caea16d-0228-4ae1-8ef3-44ac82f0cb67For 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?Sat, 11 Apr 2009 21:29:46 +0000http://bakery.cakephp.org/articles/Frank/2009/03/27/efficient_caching_with_namespacefileengine/comment:4caea16d-0228-4ae1-8ef3-44ac82f0cb67#comment4caea16d-0228-4ae1-8ef3-44ac82f0cb67