文件缓存前缀清理

jerry thinkphp 2015年11月19日 收藏
默认的文件缓存不能按前缀批量清理,小加改造分享一下,希望下个版本能整合进去。S('%', null, array('key' => true, 'prefix' =>'pre_');

./ThinkPHP/Library/Think/Cache/Driver/File.class.php
  1.     /**
  2.      * 清空目录
  3.      * @param string $dir 目录路径
  4.      * @param boolean $stay 是否保留目录
  5.      * @param string $pattern 文件筛选规则
  6.      * @return boolean
  7.      */
  8.     protected function clearDir($dir, $stay = true, $pattern = null) {
  9.         if (is_dir($dir)) {
  10.             $dir = rtrim($dir, '\\/') . DIRECTORY_SEPARATOR;
  11.             $list = array_merge(glob($dir . '*', GLOB_ONLYDIR), glob($dir . "{$pattern}*.*", GLOB_NOSORT));
  12.             foreach ($list as $file) {
  13.                 $this->clearDir($file, !is_null($pattern), $pattern);
  14.             }
  15.             $stay || rmdir($dir);
  16.         } elseif (is_file($dir)) {
  17.             return unlink($dir);
  18.         }
  19.         return true;
  20.     }
使用方法:
  1. S('%', null, array('key' => true, 'prefix' =>'pre_');