diff --git a/lib/Apc.php b/lib/Apc.php index fe7ba56f1..579d9c2fc 100644 --- a/lib/Apc.php +++ b/lib/Apc.php @@ -21,7 +21,10 @@ class Apc extends BaseCache */ public function get($key, $expire = null, $fn = null) { - $result = apc_fetch($this->namespace . $key); + $result = apc_fetch($this->namespace . $key, $success); + if (!$success) { + $result = null; + } return $this->processGetResult($key, $result, $expire, $fn); } diff --git a/lib/ArrayCache.php b/lib/ArrayCache.php index ee22b794f..27b9ca3d0 100644 --- a/lib/ArrayCache.php +++ b/lib/ArrayCache.php @@ -30,7 +30,7 @@ public function get($key, $expire = null, $fn = null) { $oriKey = $key; $key = $this->namespace . $key; - $result = array_key_exists($key, $this->data) ? $this->data[$key] : false; + $result = array_key_exists($key, $this->data) ? $this->data[$key] : null; return $this->processGetResult($oriKey, $result, $expire, $fn); } diff --git a/lib/BaseCache.php b/lib/BaseCache.php index 7364e658e..cf818ca8f 100644 --- a/lib/BaseCache.php +++ b/lib/BaseCache.php @@ -233,7 +233,7 @@ abstract public function clear(); */ protected function processGetResult($key, $result, $expire, $fn) { - if (false === $result && ($expire || $fn)) { + if (null === $result && ($expire || $fn)) { // Avoid using null as expire second, for null will be convert to 0 // which means that store the cache forever, and make it hart to debug. if (!is_numeric($expire) && $fn) { diff --git a/lib/Bicache.php b/lib/Bicache.php index 4d4244975..860810b3a 100644 --- a/lib/Bicache.php +++ b/lib/Bicache.php @@ -39,7 +39,7 @@ class Bicache extends BaseCache public function get($key, $expire = null, $fn = null) { $result = $this->master->get($key); - if (false === $result) { + if (null === $result) { $result = $this->slave->get($key); } return $this->processGetResult($key, $result, $expire, $fn); diff --git a/lib/DbCache.php b/lib/DbCache.php index ea09e4e76..0fb0d213d 100644 --- a/lib/DbCache.php +++ b/lib/DbCache.php @@ -98,7 +98,7 @@ public function get($key, $expire = null, $fn = null) $result = $this->db->select($this->table, $this->namespace . $key); $result = unserialize($result['value']); } else { - $result = false; + $result = null; } return $this->processGetResult($key, $result, $expire, $fn); } diff --git a/lib/FileCache.php b/lib/FileCache.php index dce366f51..726ae474c 100644 --- a/lib/FileCache.php +++ b/lib/FileCache.php @@ -67,14 +67,14 @@ public function __construct(array $options = []) public function get($key, $expire = null, $fn = null) { if (!is_file($file = $this->getFile($key))) { - $result = false; + $result = null; } else { $content = $this->getContent($file); if ($content && is_array($content) && time() < $content[0]) { $result = $content[1]; } else { $this->remove($key); - $result = false; + $result = null; } } return $this->processGetResult($key, $result, $expire, $fn); diff --git a/lib/Memcache.php b/lib/Memcache.php index c27fc3215..db4d9788c 100644 --- a/lib/Memcache.php +++ b/lib/Memcache.php @@ -80,7 +80,11 @@ public function __invoke($key = null, $value = null, $expire = 0) */ public function get($key, $expire = null, $fn = null) { - $result = $this->object->get($this->namespace . $key); + $flags = false; + $result = $this->object->get($this->namespace . $key, $flags); + if ($flags === false) { + $result = null; + } return $this->processGetResult($key, $result, $expire, $fn); } diff --git a/lib/Memcached.php b/lib/Memcached.php index 3aa9023f3..0d59e646a 100644 --- a/lib/Memcached.php +++ b/lib/Memcached.php @@ -92,6 +92,9 @@ public function __invoke($key = null, $value = null, $expire = 0) public function get($key, $expire = null, $fn = null) { $result = $this->object->get($this->namespace . $key); + if ($this->object->getResultCode() !== \Memcached::RES_SUCCESS) { + $result = null; + } return $this->processGetResult($key, $result, $expire, $fn); } diff --git a/lib/MongoCache.php b/lib/MongoCache.php index 223e7e90a..e62b4e60e 100644 --- a/lib/MongoCache.php +++ b/lib/MongoCache.php @@ -76,7 +76,7 @@ public function get($key, $expire = null, $fn = null) { $result = $this->object->findOne(['_id' => $this->namespace . $key], ['value', 'expire']); if (null === $result || $result['expire'] < time()) { - $result = false; + $result = null; } else { $result = unserialize($result['value']); } diff --git a/lib/NearCache.php b/lib/NearCache.php index f02058b73..fce21a95f 100644 --- a/lib/NearCache.php +++ b/lib/NearCache.php @@ -26,9 +26,9 @@ class NearCache extends BaseCache public function get($key, $expire = null, $fn = null) { $result = $this->front->get($key); - if (false === $result) { + if (null === $result) { $result = $this->back->get($key); - if (false !== $result) { + if (null !== $result) { $this->front->set($key, $result, $expire); } } diff --git a/lib/Redis.php b/lib/Redis.php index 490998e21..122bbe9da 100644 --- a/lib/Redis.php +++ b/lib/Redis.php @@ -136,7 +136,11 @@ public function connect() */ public function get($key, $expire = null, $fn = null) { - $result = $this->unserialize($this->object->get($this->namespace . $key)); + $result = $this->object->get($this->namespace . $key); + if (false === $result) { + $result = null; + } + $result = $this->unserialize($result); return $this->processGetResult($key, $result, $expire, $fn); } @@ -225,7 +229,7 @@ public function add($key, $value, $expire = 0) */ public function replace($key, $value, $expire = 0) { - if (false === $this->get($key)) { + if (false === $this->object->get($this->namespace . $key)) { return false; } return $this->set($key, $this->serialize($value), $expire); diff --git a/tests/unit/CacheTestCase.php b/tests/unit/CacheTestCase.php index f6d903b40..1687586e8 100644 --- a/tests/unit/CacheTestCase.php +++ b/tests/unit/CacheTestCase.php @@ -25,7 +25,7 @@ public function testGetterAndSetter($value, $key) $cache = $this->object; $cache->remove($key); - $this->assertFalse($cache->get($key)); + $this->assertNull($cache->get($key)); $this->assertFalse($cache->replace($key, $value)); $this->assertTrue($cache->add($key, $value)); @@ -92,7 +92,7 @@ public function testClear() $this->assertEquals($key, $cache->get($key)); $cache->clear(); - $this->assertFalse($cache->get($key)); + $this->assertNull($cache->get($key)); } public function testInvoker() diff --git a/tests/unit/DbCacheTest.php b/tests/unit/DbCacheTest.php index 8b7820838..d559f24e1 100644 --- a/tests/unit/DbCacheTest.php +++ b/tests/unit/DbCacheTest.php @@ -43,6 +43,6 @@ public function testExpire() //sleep(2); $result = $cache->get($key); - $this->assertFalse($result); + $this->assertNull($result); } } diff --git a/tests/unit/FileCacheTest.php b/tests/unit/FileCacheTest.php index 9ca68f29c..23b9ef6c2 100644 --- a/tests/unit/FileCacheTest.php +++ b/tests/unit/FileCacheTest.php @@ -70,11 +70,11 @@ public function testGet() $wei->remove('test'); - $this->assertFalse($wei->get('test'), 'cache has been removed'); + $this->assertNull($wei->get('test'), 'cache has been removed'); $wei->set('test', __METHOD__, -1); - $this->assertFalse($wei->get('test'), 'cache is expired'); + $this->assertNull($wei->get('test'), 'cache is expired'); } public function testSet() @@ -100,7 +100,7 @@ public function testReplace() $cache->replace('test', __CLASS__); - $this->assertFalse($cache->get('test'), 'cache not found'); + $this->assertNull($cache->get('test'), 'cache not found'); $cache->set(__METHOD__, true, -1); @@ -115,7 +115,7 @@ public function testRemove() $wei->remove('test'); - $this->assertFalse($wei->get('test')); + $this->assertNull($wei->get('test')); $this->assertFalse($wei->remove('test'), 'cache not found'); } diff --git a/tests/unit/RedisTest.php b/tests/unit/RedisTest.php index 0a60b8141..ca9436ac7 100644 --- a/tests/unit/RedisTest.php +++ b/tests/unit/RedisTest.php @@ -55,6 +55,6 @@ public function testAddWithExpire() // sleep for 1.1s usleep(1100000); - $this->assertFalse($cache->get(__METHOD__)); + $this->assertNull($cache->get(__METHOD__)); } } diff --git a/tests/unit/TagCacheTest.php b/tests/unit/TagCacheTest.php index 9a129eaef..a59801c45 100644 --- a/tests/unit/TagCacheTest.php +++ b/tests/unit/TagCacheTest.php @@ -41,7 +41,7 @@ public function testTwoTags() $this->assertEquals('The content of first post', $cache->get('1')); - $this->assertFalse($postCache->get('1')); + $this->assertNull($postCache->get('1')); $puCache->clear(); $postCache->clear(); @@ -56,7 +56,7 @@ public function testClear() $postCache->set('1', 'This is the first post'); $postCache->clear(); - $this->assertFalse($postCache->get('1')); + $this->assertNull($postCache->get('1')); // Clear tag caches have no effect with other caches $this->assertEquals('The content of first post', $cache->get('1')); @@ -80,10 +80,10 @@ public function testClearTag() $userCache->clear(); - $this->assertFalse($userCache->get('1')); + $this->assertNull($userCache->get('1')); $this->assertEquals('This is the first post, from admin', $puCache->get('1-1')); - $this->assertFalse($puCache->reload()->get('1-1')); + $this->assertNull($puCache->reload()->get('1-1')); $this->assertEquals('This is the first post', $postCache->get('1')); $puCache->clear();