mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Cache类的get方法增加默认值参数
This commit is contained in:
@@ -85,19 +85,14 @@ class Cache
|
|||||||
* 读取缓存
|
* 读取缓存
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name 缓存标识
|
* @param string $name 缓存标识
|
||||||
* @param string $default 默认值
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function get($name, $default = null)
|
public static function get($name, $default = false)
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
self::$readTimes++;
|
self::$readTimes++;
|
||||||
$result = self::$handler->get($name);
|
return self::$handler->get($name, $default);
|
||||||
if (false !== $result) {
|
|
||||||
return $result;
|
|
||||||
} else {
|
|
||||||
return $default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ class Template
|
|||||||
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
|
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
|
||||||
// 读取渲染缓存
|
// 读取渲染缓存
|
||||||
$cacheContent = Cache::get($this->config['cache_id']);
|
$cacheContent = Cache::get($this->config['cache_id']);
|
||||||
if (!is_null($cacheContent)) {
|
if (false !== $cacheContent) {
|
||||||
echo $cacheContent;
|
echo $cacheContent;
|
||||||
return;
|
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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $default = false)
|
||||||
{
|
{
|
||||||
$filename = $this->filename($name);
|
$filename = $this->filename($name);
|
||||||
if (!is_file($filename)) {
|
if (!is_file($filename)) {
|
||||||
return false;
|
return $default;
|
||||||
}
|
}
|
||||||
$content = file_get_contents($filename);
|
$content = file_get_contents($filename);
|
||||||
if (false !== $content) {
|
if (false !== $content) {
|
||||||
@@ -114,7 +115,7 @@ class File
|
|||||||
if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire) {
|
if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire) {
|
||||||
//缓存过期删除缓存文件
|
//缓存过期删除缓存文件
|
||||||
$this->unlink($filename);
|
$this->unlink($filename);
|
||||||
return false;
|
return $default;
|
||||||
}
|
}
|
||||||
$content = substr($content, 20, -3);
|
$content = substr($content, 20, -3);
|
||||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||||
@@ -124,7 +125,7 @@ class File
|
|||||||
$content = unserialize($content);
|
$content = unserialize($content);
|
||||||
return $content;
|
return $content;
|
||||||
} else {
|
} 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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $default = false)
|
||||||
{
|
{
|
||||||
$filename = $this->filename($name);
|
$filename = $this->filename($name);
|
||||||
if (is_file($filename)) {
|
if (is_file($filename)) {
|
||||||
@@ -78,11 +79,11 @@ class Lite
|
|||||||
if ($mtime < $_SERVER['REQUEST_TIME']) {
|
if ($mtime < $_SERVER['REQUEST_TIME']) {
|
||||||
// 清除已经过期的文件
|
// 清除已经过期的文件
|
||||||
unlink($filename);
|
unlink($filename);
|
||||||
return false;
|
return $default;
|
||||||
}
|
}
|
||||||
return include $filename;
|
return include $filename;
|
||||||
} else {
|
} 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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @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);
|
$jsonData = json_decode($value, true);
|
||||||
// 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
|
// 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
|
||||||
return (null === $jsonData) ? $value : $jsonData;
|
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
|
* @access public
|
||||||
* @param string $name 缓存key
|
* @param string $name 缓存key
|
||||||
* @param bool $master 指定主从节点,可以从主节点获取结果
|
* @param string $default 默认值
|
||||||
|
* @param bool $master 指定主从节点,可以从主节点获取结果
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name, $master = false)
|
public function get($name, $default = false, $master = false)
|
||||||
{
|
{
|
||||||
$this->master($master);
|
$this->master($master);
|
||||||
|
|
||||||
@@ -225,14 +226,12 @@ class Redisd
|
|||||||
$value = $this->handler->get($name);
|
$value = $this->handler->get($name);
|
||||||
} catch (\RedisException $e) {
|
} catch (\RedisException $e) {
|
||||||
unset(self::$redis_rw_handler[0]);
|
unset(self::$redis_rw_handler[0]);
|
||||||
|
|
||||||
$this->master();
|
$this->master();
|
||||||
return $this->get($name);
|
return $this->get($name, $default);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::record($e->getMessage(), Log::ERROR);
|
Log::record($e->getMessage(), Log::ERROR);
|
||||||
}
|
}
|
||||||
|
return isset($value) ? $value : $default;
|
||||||
return isset($value) ? $value : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
5
library/think/cache/driver/Sqlite.php
vendored
5
library/think/cache/driver/Sqlite.php
vendored
@@ -64,9 +64,10 @@ class Sqlite
|
|||||||
* 读取缓存
|
* 读取缓存
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $default = false)
|
||||||
{
|
{
|
||||||
$name = $this->options['prefix'] . sqlite_escape_string($name);
|
$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';
|
$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 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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $default = false)
|
||||||
{
|
{
|
||||||
$name = $this->options['prefix'] . $name;
|
$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
|
* @access public
|
||||||
* @param string $name 缓存变量名
|
* @param string $name 缓存变量名
|
||||||
|
* @param mixed $default 默认值
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get($name)
|
public function get($name, $default = false)
|
||||||
{
|
{
|
||||||
$name = $this->options['prefix'] . $name;
|
$name = $this->options['prefix'] . $name;
|
||||||
if (xcache_isset($name)) {
|
return xcache_isset($name) ? xcache_get($name) : $default;
|
||||||
return xcache_get($name);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user