mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
Cache类的get方法增加默认值参数
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
9
library/think/cache/driver/File.php
vendored
9
library/think/cache/driver/File.php
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
7
library/think/cache/driver/Lite.php
vendored
7
library/think/cache/driver/Lite.php
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
6
library/think/cache/driver/Memcache.php
vendored
6
library/think/cache/driver/Memcache.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
6
library/think/cache/driver/Memcached.php
vendored
6
library/think/cache/driver/Memcached.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
8
library/think/cache/driver/Redis.php
vendored
8
library/think/cache/driver/Redis.php
vendored
@@ -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<xiaobo.sun@qq.com>
|
||||
return (null === $jsonData) ? $value : $jsonData;
|
||||
|
||||
13
library/think/cache/driver/Redisd.php
vendored
13
library/think/cache/driver/Redisd.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
5
library/think/cache/driver/Sqlite.php
vendored
5
library/think/cache/driver/Sqlite.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
5
library/think/cache/driver/Wincache.php
vendored
5
library/think/cache/driver/Wincache.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
8
library/think/cache/driver/Xcache.php
vendored
8
library/think/cache/driver/Xcache.php
vendored
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user