默认的文件缓存不能按前缀批量清理,小加改造分享一下,希望下个版本能整合进去。S('%', null, array('key' => true, 'prefix' =>'pre_');
./ThinkPHP/Library/Think/Cache/Driver/File.class.php
/**
* 清空目录
* @param string $dir 目录路径
* @param boolean $stay 是否保留目录
* @param string $pattern 文件筛选规则
* @return boolean
*/
protected function clearDir($dir, $stay = true, $pattern = null) {
if (is_dir($dir)) {
$dir = rtrim($dir, '\\/') . DIRECTORY_SEPARATOR;
$list = array_merge(glob($dir . '*', GLOB_ONLYDIR), glob($dir . "{$pattern}*.*", GLOB_NOSORT));
foreach ($list as $file) {
$this->clearDir($file, !is_null($pattern), $pattern);
}
$stay || rmdir($dir);
} elseif (is_file($dir)) {
return unlink($dir);
}
return true;
}
使用方法:
S('%', null, array('key' => true, 'prefix' =>'pre_');