diff --git a/library/think/Cache.php b/library/think/Cache.php index df2ebcb0..f13f511c 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -85,19 +85,14 @@ class Cache * 读取缓存 * @access public * @param string $name 缓存标识 - * @param string $default 默认值 + * @param mixed $default 默认值 * @return mixed */ - public static function get($name, $default = null) + public static function get($name, $default = false) { self::init(); self::$readTimes++; - $result = self::$handler->get($name); - if (false !== $result) { - return $result; - } else { - return $default; - } + return self::$handler->get($name, $default); } /** diff --git a/library/think/Template.php b/library/think/Template.php index 73d0f47d..d9250971 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -173,7 +173,7 @@ class Template if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 读取渲染缓存 $cacheContent = Cache::get($this->config['cache_id']); - if (!is_null($cacheContent)) { + if (false !== $cacheContent) { echo $cacheContent; return; } diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index 7d4c56e7..19d42005 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -100,13 +100,14 @@ class File * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { $filename = $this->filename($name); if (!is_file($filename)) { - return false; + return $default; } $content = file_get_contents($filename); if (false !== $content) { @@ -114,7 +115,7 @@ class File if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire) { //缓存过期删除缓存文件 $this->unlink($filename); - return false; + return $default; } $content = substr($content, 20, -3); if ($this->options['data_compress'] && function_exists('gzcompress')) { @@ -124,7 +125,7 @@ class File $content = unserialize($content); return $content; } else { - return false; + return $default; } } diff --git a/library/think/cache/driver/Lite.php b/library/think/cache/driver/Lite.php index d5729dc6..ab295672 100644 --- a/library/think/cache/driver/Lite.php +++ b/library/think/cache/driver/Lite.php @@ -67,9 +67,10 @@ class Lite * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { $filename = $this->filename($name); if (is_file($filename)) { @@ -78,11 +79,11 @@ class Lite if ($mtime < $_SERVER['REQUEST_TIME']) { // 清除已经过期的文件 unlink($filename); - return false; + return $default; } return include $filename; } else { - return false; + return $default; } } diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index 93b63de2..73b6b008 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -71,11 +71,13 @@ class Memcache * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { - return $this->handler->get($this->options['prefix'] . $name); + $result = $this->handler->get($this->options['prefix'] . $name); + return false !== $result ? $result : $default; } /** diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 32063246..4140a842 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -76,11 +76,13 @@ class Memcached * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { - return $this->handler->get($this->options['prefix'] . $name); + $result = $this->handler->get($this->options['prefix'] . $name); + return false !== $result ? $result : $default; } /** diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index ec2d3e93..bfe73206 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -68,11 +68,15 @@ class Redis * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { - $value = $this->handler->get($this->options['prefix'] . $name); + $value = $this->handler->get($this->options['prefix'] . $name); + if (is_null($value)) { + return $default; + } $jsonData = json_decode($value, true); // 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson return (null === $jsonData) ? $value : $jsonData; diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index 94645dd5..328686cf 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -213,11 +213,12 @@ class Redisd * 读取缓存 * * @access public - * @param string $name 缓存key - * @param bool $master 指定主从节点,可以从主节点获取结果 + * @param string $name 缓存key + * @param string $default 默认值 + * @param bool $master 指定主从节点,可以从主节点获取结果 * @return mixed */ - public function get($name, $master = false) + public function get($name, $default = false, $master = false) { $this->master($master); @@ -225,14 +226,12 @@ class Redisd $value = $this->handler->get($name); } catch (\RedisException $e) { unset(self::$redis_rw_handler[0]); - $this->master(); - return $this->get($name); + return $this->get($name, $default); } catch (\Exception $e) { Log::record($e->getMessage(), Log::ERROR); } - - return isset($value) ? $value : null; + return isset($value) ? $value : $default; } /** diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index e5e47fe7..561acdfc 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -64,9 +64,10 @@ class Sqlite * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { $name = $this->options['prefix'] . sqlite_escape_string($name); $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1'; @@ -79,7 +80,7 @@ class Sqlite } return unserialize($content); } - return false; + return $default; } /** diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index 937fa344..5556162c 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -56,12 +56,13 @@ class Wincache * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { $name = $this->options['prefix'] . $name; - return wincache_ucache_exists($name) ? wincache_ucache_get($name) : false; + return wincache_ucache_exists($name) ? wincache_ucache_get($name) : $default; } /** diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index 10b0740d..8334404a 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -56,15 +56,13 @@ class Xcache * 读取缓存 * @access public * @param string $name 缓存变量名 + * @param mixed $default 默认值 * @return mixed */ - public function get($name) + public function get($name, $default = false) { $name = $this->options['prefix'] . $name; - if (xcache_isset($name)) { - return xcache_get($name); - } - return false; + return xcache_isset($name) ? xcache_get($name) : $default; } /**