123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- class sfFileCache extends sfCache
- {
- const READ_DATA = 1;
- const READ_TIMEOUT = 2;
- const READ_LAST_MODIFIED = 4;
- const EXTENSION = '.cache';
-
- public function initialize($options = array())
- {
- parent::initialize($options);
- if (!$this->getOption('cache_dir'))
- {
- throw new sfInitializationException('You must pass a "cache_dir" option to initialize a sfFileCache object.');
- }
- $this->setcache_dir($this->getOption('cache_dir'));
- }
-
- public function get($key, $default = null)
- {
- $file_path = $this->getFilePath($key);
- if (!file_exists($file_path))
- {
- return $default;
- }
- $data = $this->read($file_path, self::READ_DATA);
- if ($data[self::READ_DATA] === null) {
- return $default;
- }
- return $data[self::READ_DATA];
- }
-
- public function has($key)
- {
- $path = $this->getFilePath($key);
- return file_exists($path) && $this->isValid($path);
- }
-
- public function set($key, $data, $lifetime = null)
- {
- if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1)
- {
- $this->clean(sfCache::OLD);
- }
- return $this->write($this->getFilePath($key), $data, time() + $this->getLifetime($lifetime));
- }
-
- public function remove($key)
- {
- return @unlink($this->getFilePath($key));
- }
-
- public function removePattern($pattern)
- {
- if (false !== strpos($pattern, '**'))
- {
- $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
- $regexp = self::patternToRegexp($pattern);
- $paths = array();
- foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
- {
- if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
- {
- $paths[] = $path;
- }
- }
- }
- else
- {
- $paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
- }
- foreach ($paths as $path)
- {
- if (is_dir($path))
- {
- sfToolkit::clearDirectory($path);
- }
- else
- {
- @unlink($path);
- }
- }
- }
-
- public function clean($mode = sfCache::ALL)
- {
- if (!is_dir($this->getOption('cache_dir')))
- {
- return true;
- }
- $result = true;
- foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $file)
- {
- if (sfCache::ALL == $mode || !$this->isValid($file))
- {
- $result = @unlink($file) && $result;
- }
- }
- return $result;
- }
-
- public function getTimeout($key)
- {
- $path = $this->getFilePath($key);
- if (!file_exists($path))
- {
- return 0;
- }
- $data = $this->read($path, self::READ_TIMEOUT);
- return $data[self::READ_TIMEOUT] < time() ? 0 : $data[self::READ_TIMEOUT];
- }
-
- public function getLastModified($key)
- {
- $path = $this->getFilePath($key);
- if (!file_exists($path))
- {
- return 0;
- }
-
- $data = $this->read($path, self::READ_TIMEOUT | self::READ_LAST_MODIFIED);
- if ($data[self::READ_TIMEOUT] < time())
- {
- return 0;
- }
- return $data[self::READ_LAST_MODIFIED];
- }
- protected function isValid($path)
- {
- $data = $this->read($path, self::READ_TIMEOUT);
- return time() < $data[self::READ_TIMEOUT];
- }
-
- protected function getFilePath($key)
- {
- return $this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $key).self::EXTENSION;
- }
-
- protected function read($path, $type = self::READ_DATA)
- {
- if (!$fp = @fopen($path, 'rb'))
- {
- throw new sfCacheException(sprintf('Unable to read cache file "%s".', $path));
- }
- @flock($fp, LOCK_SH);
- $data[self::READ_TIMEOUT] = intval(@stream_get_contents($fp, 12, 0));
- if ($type != self::READ_TIMEOUT && time() < $data[self::READ_TIMEOUT]) {
- if ($type & self::READ_LAST_MODIFIED)
- {
- $data[self::READ_LAST_MODIFIED] = intval(@stream_get_contents($fp, 12, 12));
- }
- if ($type & self::READ_DATA)
- {
- fseek($fp, 0, SEEK_END);
- $length = ftell($fp) - 24;
- fseek($fp, 24);
- $data[self::READ_DATA] = @fread($fp, $length);
- }
- } else {
- $data[self::READ_LAST_MODIFIED] = null;
- $data[self::READ_DATA] = null;
- }
- @flock($fp, LOCK_UN);
- @fclose($fp);
- return $data;
- }
-
- protected function write($path, $data, $timeout)
- {
- $current_umask = umask();
- umask(0000);
- if (!is_dir(dirname($path)))
- {
-
- mkdir(dirname($path), 0777, true);
- }
- $tmpFile = $path . '.' . getmypid();
- if (!$fp = @fopen($tmpFile, 'wb'))
- {
- throw new sfCacheException(sprintf('Unable to write cache file "%s".', $tmpFile));
- }
- @fwrite($fp, str_pad($timeout, 12, 0, STR_PAD_LEFT));
- @fwrite($fp, str_pad(time(), 12, 0, STR_PAD_LEFT));
- @fwrite($fp, $data);
- @fclose($fp);
- chmod($tmpFile, 0666);
- @unlink($path);
- rename($tmpFile, $path);
- umask($current_umask);
- return true;
- }
-
- protected function setcache_dir($cache_dir)
- {
-
- if (DIRECTORY_SEPARATOR == substr($cache_dir, -1))
- {
- $cache_dir = substr($cache_dir, 0, -1);
- }
-
- if (!is_dir($cache_dir))
- {
- $current_umask = umask(0000);
- @mkdir($cache_dir, 0777, true);
- umask($current_umask);
- }
- }
- }
|