123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace think\cache\driver;
- use think\cache\Driver;
- class File extends Driver
- {
- protected $options = [
- 'expire' => 0,
- 'cache_subdir' => true,
- 'prefix' => '',
- 'path' => CACHE_PATH,
- 'data_compress' => false,
- ];
- protected $expire;
-
- public function __construct($options = [])
- {
- if (!empty($options)) {
- $this->options = array_merge($this->options, $options);
- }
- if (substr($this->options['path'], -1) != DS) {
- $this->options['path'] .= DS;
- }
- $this->init();
- }
-
- private function init()
- {
-
- if (!is_dir($this->options['path'])) {
- if (mkdir($this->options['path'], 0755, true)) {
- return true;
- }
- }
- return false;
- }
-
- protected function getCacheKey($name, $auto = false)
- {
- $name = md5($name);
- if ($this->options['cache_subdir']) {
-
- $name = substr($name, 0, 2) . DS . substr($name, 2);
- }
- if ($this->options['prefix']) {
- $name = $this->options['prefix'] . DS . $name;
- }
- $filename = $this->options['path'] . $name . '.php';
- $dir = dirname($filename);
- if ($auto && !is_dir($dir)) {
- mkdir($dir, 0755, true);
- }
- return $filename;
- }
-
- public function has($name)
- {
- return $this->get($name) ? true : false;
- }
-
- public function get($name, $default = false)
- {
- $filename = $this->getCacheKey($name);
- if (!is_file($filename)) {
- return $default;
- }
- $content = file_get_contents($filename);
- $this->expire = null;
- if (false !== $content) {
- $expire = (int) substr($content, 8, 12);
- if (0 != $expire && time() > filemtime($filename) + $expire) {
- return $default;
- }
- $this->expire = $expire;
- $content = substr($content, 32);
- if ($this->options['data_compress'] && function_exists('gzcompress')) {
-
- $content = gzuncompress($content);
- }
- $content = unserialize($content);
- return $content;
- } else {
- return $default;
- }
- }
-
- public function set($name, $value, $expire = null)
- {
- if (is_null($expire)) {
- $expire = $this->options['expire'];
- }
- if ($expire instanceof \DateTime) {
- $expire = $expire->getTimestamp() - time();
- }
- $filename = $this->getCacheKey($name, true);
- if ($this->tag && !is_file($filename)) {
- $first = true;
- }
- $data = serialize($value);
- if ($this->options['data_compress'] && function_exists('gzcompress')) {
-
- $data = gzcompress($data, 3);
- }
- $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
- $result = file_put_contents($filename, $data);
- if ($result) {
- isset($first) && $this->setTagItem($filename);
- clearstatcache();
- return true;
- } else {
- return false;
- }
- }
-
- public function inc($name, $step = 1)
- {
- if ($this->has($name)) {
- $value = $this->get($name) + $step;
- $expire = $this->expire;
- } else {
- $value = $step;
- $expire = 0;
- }
- return $this->set($name, $value, $expire) ? $value : false;
- }
-
- public function dec($name, $step = 1)
- {
- if ($this->has($name)) {
- $value = $this->get($name) - $step;
- $expire = $this->expire;
- } else {
- $value = -$step;
- $expire = 0;
- }
- return $this->set($name, $value, $expire) ? $value : false;
- }
-
- public function rm($name)
- {
- $filename = $this->getCacheKey($name);
- try {
- return $this->unlink($filename);
- } catch (\Exception $e) {
- }
- }
-
- public function clear($tag = null)
- {
- if ($tag) {
-
- $keys = $this->getTagItem($tag);
- foreach ($keys as $key) {
- $this->unlink($key);
- }
- $this->rm('tag_' . md5($tag));
- return true;
- }
- $files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DS : '') . '*');
- foreach ($files as $path) {
- if (is_dir($path)) {
- $matches = glob($path . '/*.php');
- if (is_array($matches)) {
- array_map('unlink', $matches);
- }
- rmdir($path);
- } else {
- unlink($path);
- }
- }
- return true;
- }
-
- private function unlink($path)
- {
- return is_file($path) && unlink($path);
- }
- }
|