diff --git a/library/think/Cache.php b/library/think/Cache.php index dcf3dd05..df2ebcb0 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -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; + } } /** diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index 29cd3773..7d4c56e7 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -11,8 +11,6 @@ namespace think\cache\driver; -use think\Cache; - /** * 文件类型缓存类 * @author liu21st @@ -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 diff --git a/library/think/cache/driver/Lite.php b/library/think/cache/driver/Lite.php index 0a3a838b..d5729dc6 100644 --- a/library/think/cache/driver/Lite.php +++ b/library/think/cache/driver/Lite.php @@ -11,8 +11,6 @@ namespace think\cache\driver; -use think\Cache; - /** * 文件类型缓存类 * @author liu21st @@ -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 diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index 8ca55f07..93b63de2 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -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 diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 50082a1e..32063246 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -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 diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 25febbf0..ec2d3e93 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -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 diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index e3bc80e7..94645dd5 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -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; + } + /** * 读取缓存 * diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index e4689d61..e5e47fe7 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -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 diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index bc1b35de..937fa344 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -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 diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index bb6f277e..10b0740d 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -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 diff --git a/tests/thinkphp/library/think/cache/driver/cacheTestCase.php b/tests/thinkphp/library/think/cache/driver/cacheTestCase.php index c7bd407c..5097461b 100644 --- a/tests/thinkphp/library/think/cache/driver/cacheTestCase.php +++ b/tests/thinkphp/library/think/cache/driver/cacheTestCase.php @@ -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)); } /**