|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
-
-
- namespace yii\caching;
-
- use Yii;
- use yii\helpers\FileHelper;
-
-
- class FileCache extends Cache
- {
-
-
- public $keyPrefix = '';
-
-
- public $cachePath = '@runtime/cache';
-
-
- public $cacheFileSuffix = '.bin';
-
-
- public $directoryLevel = 1;
-
-
- public $gcProbability = 10;
-
-
- public $fileMode;
-
-
- public $dirMode = 0775;
-
-
-
-
- public function init()
- {
- parent::init();
- $this->cachePath = Yii::getAlias($this->cachePath);
- if (!is_dir($this->cachePath)) {
- FileHelper::createDirectory($this->cachePath, $this->dirMode, true);
- }
- }
-
-
-
- public function exists($key)
- {
- $cacheFile = $this->getCacheFile($this->buildKey($key));
-
- return @filemtime($cacheFile) > time();
- }
-
-
-
- protected function getValue($key)
- {
- $cacheFile = $this->getCacheFile($key);
-
- if (@filemtime($cacheFile) > time()) {
- $fp = @fopen($cacheFile, 'r');
- if ($fp !== false) {
- @flock($fp, LOCK_SH);
- $cacheValue = @stream_get_contents($fp);
- @flock($fp, LOCK_UN);
- @fclose($fp);
- return $cacheValue;
- }
- }
-
- return false;
- }
-
-
-
- protected function setValue($key, $value, $duration)
- {
- $this->gc();
- $cacheFile = $this->getCacheFile($key);
- if ($this->directoryLevel > 0) {
- @FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
- }
- if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
- if ($this->fileMode !== null) {
- @chmod($cacheFile, $this->fileMode);
- }
- if ($duration <= 0) {
- $duration = 31536000;
- }
-
- return @touch($cacheFile, $duration + time());
- } else {
- $error = error_get_last();
- Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__);
- return false;
- }
- }
-
-
-
- protected function addValue($key, $value, $duration)
- {
- $cacheFile = $this->getCacheFile($key);
- if (@filemtime($cacheFile) > time()) {
- return false;
- }
-
- return $this->setValue($key, $value, $duration);
- }
-
-
-
- protected function deleteValue($key)
- {
- $cacheFile = $this->getCacheFile($key);
-
- return @unlink($cacheFile);
- }
-
-
-
- protected function getCacheFile($key)
- {
- if ($this->directoryLevel > 0) {
- $base = $this->cachePath;
- for ($i = 0; $i < $this->directoryLevel; ++$i) {
- if (($prefix = substr($key, $i + $i, 2)) !== false) {
- $base .= DIRECTORY_SEPARATOR . $prefix;
- }
- }
-
- return $base . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
- } else {
- return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
- }
- }
-
-
-
- protected function flushValues()
- {
- $this->gc(true, false);
-
- return true;
- }
-
-
-
- public function gc($force = false, $expiredOnly = true)
- {
- if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
- $this->gcRecursive($this->cachePath, $expiredOnly);
- }
- }
-
-
-
- protected function gcRecursive($path, $expiredOnly)
- {
- if (($handle = opendir($path)) !== false) {
- while (($file = readdir($handle)) !== false) {
- if ($file[0] === '.') {
- continue;
- }
- $fullPath = $path . DIRECTORY_SEPARATOR . $file;
- if (is_dir($fullPath)) {
- $this->gcRecursive($fullPath, $expiredOnly);
- if (!$expiredOnly) {
- if (!@rmdir($fullPath)) {
- $error = error_get_last();
- Yii::warning("Unable to remove directory '{$fullPath}': {$error['message']}", __METHOD__);
- }
- }
- } elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) {
- if (!@unlink($fullPath)) {
- $error = error_get_last();
- Yii::warning("Unable to remove file '{$fullPath}': {$error['message']}", __METHOD__);
- }
- }
- }
- closedir($handle);
- }
- }
- }
|