Skip to content

Commit

Permalink
fix(Cache): 缓存无数据返回值由 false 改为 null
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 缓存无数据返回值由 `false` 改为 `null`
  • Loading branch information
twinh committed Feb 5, 2022
1 parent 2897cd1 commit b46ad12
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 25 deletions.
5 changes: 4 additions & 1 deletion lib/Apc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/BaseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Bicache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/DbCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion lib/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/MongoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/NearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/CacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/DbCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public function testExpire()
//sleep(2);

$result = $cache->get($key);
$this->assertFalse($result);
$this->assertNull($result);
}
}
8 changes: 4 additions & 4 deletions tests/unit/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);

Expand All @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public function testAddWithExpire()
// sleep for 1.1s
usleep(1100000);

$this->assertFalse($cache->get(__METHOD__));
$this->assertNull($cache->get(__METHOD__));
}
}
8 changes: 4 additions & 4 deletions tests/unit/TagCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'));
Expand All @@ -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();
Expand Down

0 comments on commit b46ad12

Please sign in to comment.