mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
Cache类增加has方法 get方法支持默认值
This commit is contained in:
@@ -69,16 +69,35 @@ class Cache
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @return mixed
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public static function get($name)
|
||||
public static function has($name)
|
||||
{
|
||||
self::init();
|
||||
self::$readTimes++;
|
||||
return self::$handler->get($name);
|
||||
return self::$handler->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @param string $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($name, $default = null)
|
||||
{
|
||||
self::init();
|
||||
self::$readTimes++;
|
||||
$result = self::$handler->get($name);
|
||||
if (false !== $result) {
|
||||
return $result;
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
14
library/think/cache/driver/File.php
vendored
14
library/think/cache/driver/File.php
vendored
@@ -11,8 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
|
||||
/**
|
||||
* 文件类型缓存类
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
@@ -86,6 +84,18 @@ class File
|
||||
return $this->options['path'] . $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
14
library/think/cache/driver/Lite.php
vendored
14
library/think/cache/driver/Lite.php
vendored
@@ -11,8 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
|
||||
/**
|
||||
* 文件类型缓存类
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
@@ -53,6 +51,18 @@ class Lite
|
||||
return $this->options['path'] . $this->options['prefix'] . md5($name) . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return mixed
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$filename = $this->filename($name);
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
13
library/think/cache/driver/Memcache.php
vendored
13
library/think/cache/driver/Memcache.php
vendored
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
|
||||
class Memcache
|
||||
@@ -56,6 +55,18 @@ class Memcache
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->get($name) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
14
library/think/cache/driver/Memcached.php
vendored
14
library/think/cache/driver/Memcached.php
vendored
@@ -11,8 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
|
||||
class Memcached
|
||||
{
|
||||
protected $handler;
|
||||
@@ -62,6 +60,18 @@ class Memcached
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return $this->handler->get($name) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
14
library/think/cache/driver/Redis.php
vendored
14
library/think/cache/driver/Redis.php
vendored
@@ -11,9 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
|
||||
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
|
||||
@@ -56,6 +53,17 @@ class Redis
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->handler->get($this->options['prefix'] . $name) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
12
library/think/cache/driver/Redisd.php
vendored
12
library/think/cache/driver/Redisd.php
vendored
@@ -12,7 +12,6 @@
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\App;
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
use think\Log;
|
||||
|
||||
@@ -199,6 +198,17 @@ class Redisd
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->get($name) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
*
|
||||
|
||||
15
library/think/cache/driver/Sqlite.php
vendored
15
library/think/cache/driver/Sqlite.php
vendored
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
@@ -47,6 +46,20 @@ class Sqlite
|
||||
$this->handler = $func($this->options['db']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($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';
|
||||
$result = sqlite_query($this->handler, $sql);
|
||||
return sqlite_num_rows($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
13
library/think/cache/driver/Wincache.php
vendored
13
library/think/cache/driver/Wincache.php
vendored
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
@@ -41,6 +40,18 @@ class Wincache
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return wincache_ucache_exists($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
13
library/think/cache/driver/Xcache.php
vendored
13
library/think/cache/driver/Xcache.php
vendored
@@ -11,7 +11,6 @@
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Cache;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
@@ -41,6 +40,18 @@ class Xcache
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return xcache_isset($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
|
||||
@@ -75,7 +75,7 @@ abstract class cacheTestCase extends \PHPUnit_Framework_TestCase
|
||||
$array = $cache->get('array_test');
|
||||
$this->assertArrayHasKey('array_test', $array);
|
||||
$this->assertEquals('array_test', $array['array_test']);
|
||||
|
||||
|
||||
$result = $cache->set('no_expire', 1, 0);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
@@ -88,9 +88,9 @@ abstract class cacheTestCase extends \PHPUnit_Framework_TestCase
|
||||
public function testExists()
|
||||
{
|
||||
$cache = $this->prepare();
|
||||
$this->assertNotEmpty($cache->get('string_test'));
|
||||
$this->assertNotEmpty($cache->get('number_test'));
|
||||
$this->assertFalse($cache->get('not_exists'));
|
||||
$this->assertNotEmpty($cache->has('string_test'));
|
||||
$this->assertNotEmpty($cache->has('number_test'));
|
||||
$this->assertFalse($cache->has('not_exists'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +101,7 @@ abstract class cacheTestCase extends \PHPUnit_Framework_TestCase
|
||||
public function testGetNonExistent()
|
||||
{
|
||||
$cache = $this->getCacheInstance();
|
||||
$this->assertFalse($cache->get('non_existent_key'));
|
||||
$this->assertFalse($cache->get('non_existent_key', false));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user