ZF-11684: Zend_Cache_Backend_File large memory usage for cleaning

Description

Class Zend_Cache_Backend_File uses in its _clean() function glob() like this:

 
$glob = @glob($dir . $prefix . '--*');

This means that all results from a cache folder are stored in memory. For a folder that has many cache files this can lead to memory overflow.

A simple fix for this can be to replace the glob mechanism with simple preg_match:


        $handle = opendir($dir);

        if (!$handle) {
            return false;
        }

        $prefixEscaped = str_replace('/', '\/', $prefix);

        while (false !== ($fileName = readdir($handle))) {
            if (!preg_match('/^' . $prefixEscaped . '\-\-/', $fileName)) {
                continue;
            }

            $file = $dir . $fileName;

            // ...
        }

        closedir($handle);

Comments

On ZF2 it's already fixed by using the class GlobIterator instead of the raw glob function. But the GlobIterator isn't available before PHP 5.3.

Using opendir + preg_match could help but it's in as glob in definition.