Is the Zend Framework’s Zend_Cache slowing you down?
I recently discovered that one of our websites had become so painfully slow that it was taking several seconds to display a cached file. We are using Zend_Cache, from the Zend Framework, to cache frequently accessed content. On the Zend_Cache backend we are using Zend_Cache_Backend_File to cache thousands of documents. We were storing the cache files in a directory that, in addition to the Zend cache files, had thousands of other temporary files unrelated to Zend_Cache.
It was a poor decision on my part to pile all these files into one directory, and one that went unnoticed until the number of files on the site started to grow. If you happen to find yourself in the same situation, here’s what you can do:
Put the cache files in their own directory, and use a hashed directory structure. Zend_Cache_Backend_File supports this through the hashed_directory_level setting. See the documentation for more information.
Sample code:
<?php
require_once ("Zend/Cache.php");
$fOpt = array (
'lifetime' => 3600,
'automatic_serialization' => true
);
$bOpt = array (
'hashed_directory_level' => 2,
'cache_dir' => '/tmp/'
);
$cache = Zend_Cache :: factory('Core', 'File', $fOpt, $bOpt);
?>
- February 28th





Have you considered putting this somewhere on the user wiki? I think it would fit in here: http://framework.zend.com/wiki/x/P6c
BTW, that’s a pretty misleading title. I was hoping to find some nice dirt on ZF when I first saw it come up in my reader.
,Wil
Thanks Wil, I have done so at:
http://framework.zend.com/wiki/display/ZFUSER/Zend_Cache+Example
I may have messed up the formatting of the code sample that was already present. Very sorry about that
BTW I think the Zend Framework absolutely *rocks*, I certainly hope my post didn’t sound negative towards Zend_Cache. Keep up the great work (please).
I had same problem with my website. I had about 100 thsnd pages cached and it become very slow because all files were in one folder. I couldn’t even delete cacehd files (I am using shared hosting so i do not have enought resources. ) then I tried 2 leveled hashing but then again it takes a lot of space in file system. finally I am using sqlite backend which is doing well.
by the way i managed to delete all those cached files same way I creadet them, one at every request. it took me a week to delete all cached files this way.