Cache_Lite::_cleanDir PHP Method

_cleanDir() public method

Recursive function for cleaning cache file in the given directory
public _cleanDir ( string $dir, string $group = false, string $mode = 'ingroup' ) : boolean
$dir string directory complete path (with a trailing slash)
$group string name of the cache group
$mode string flush cache mode : 'old', 'ingroup', 'notingroup', 'callback_myFunction'
return boolean true if no problem
    function _cleanDir($dir, $group = false, $mode = 'ingroup')
    {
        if ($this->_fileNameProtection) {
            $motif = $group ? 'cache_' . md5($group) . '_' : 'cache_';
        } else {
            $motif = $group ? 'cache_' . $group . '_' : 'cache_';
        }
        if ($this->_memoryCaching) {
            while (list($key, ) = each($this->_memoryCachingArray)) {
                if (strpos($key, $motif, 0)) {
                    unset($this->_memoryCachingArray[$key]);
                    $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
                }
            }
            if ($this->_onlyMemoryCaching) {
                return true;
            }
        }
        if (!($dh = opendir($dir))) {
            return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
        }
        $result = true;
        while ($file = readdir($dh)) {
            if ($file != '.' && $file != '..') {
                if (substr($file, 0, 6) == 'cache_') {
                    $file2 = $dir . $file;
                    if (is_file($file2)) {
                        switch (substr($mode, 0, 9)) {
                            case 'old':
                                // files older than lifeTime get deleted from cache
                                if (!is_null($this->_lifeTime)) {
                                    if (time() - @filemtime($file2) > $this->_lifeTime) {
                                        $result = ($result and $this->_unlink($file2));
                                    }
                                }
                                break;
                            case 'notingroup':
                                if (!strpos($file2, $motif, 0)) {
                                    $result = ($result and $this->_unlink($file2));
                                }
                                break;
                            case 'callback_':
                                $func = substr($mode, 9, strlen($mode) - 9);
                                if ($func($file2, $group)) {
                                    $result = ($result and $this->_unlink($file2));
                                }
                                break;
                            case 'ingroup':
                            default:
                                if (strpos($file2, $motif, 0)) {
                                    $result = ($result and $this->_unlink($file2));
                                }
                                break;
                        }
                    }
                    if (is_dir($file2) and $this->_hashedDirectoryLevel > 0) {
                        $result = ($result and $this->_cleanDir($file2 . '/', $group, $mode));
                    }
                }
            }
        }
        return $result;
    }

Usage Example

コード例 #1
0
 /**
  * Garbage collect expired cache data
  *
  * @return  boolean
  *
  * @since   11.1
  */
 public function gc()
 {
     $result = true;
     static::$CacheLiteInstance->setOption('automaticCleaningFactor', 1);
     static::$CacheLiteInstance->setOption('hashedDirectoryLevel', 1);
     $success1 = static::$CacheLiteInstance->_cleanDir($this->_root . '/', false, 'old');
     if (!($dh = opendir($this->_root . '/'))) {
         return false;
     }
     while ($file = readdir($dh)) {
         if ($file != '.' && $file != '..' && $file != '.svn') {
             $file2 = $this->_root . '/' . $file;
             if (is_dir($file2)) {
                 $result = $result && static::$CacheLiteInstance->_cleanDir($file2 . '/', false, 'old');
             }
         }
     }
     $success = $success1 && $result;
     return $success;
 }