Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
Fix some bugs, Add more tests covered Cache.php everwhere
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Jun 29, 2017
1 parent 3bc1bd0 commit 80ad874
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
17 changes: 6 additions & 11 deletions Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function setValue($key, $value, $expire)
if ($expire == 0) {
return (bool)$this->redis->set($key, $value);
} else {
return (bool)$this->redis->setex($key, $expire, $value);
return (bool)$this->redis->setEx($key, $expire, $value);
}
}

Expand All @@ -78,18 +78,13 @@ protected function setValue($key, $value, $expire)
*/
protected function setValues($data, $expire)
{
$args = [];
foreach ($data as $key => $value) {
$args[] = $key;
$args[] = $value;
}
$failedKeys = [];
if ($expire == 0) {
$this->redis->mset($args);
$this->redis->mSet($data);
} else {
$expire = (int)($expire * 1000);
$expire = (int)$expire;
$this->redis->multi();
$this->redis->mset($args);
$this->redis->mSet($data);
$index = [];
foreach ($data as $key => $value) {
$this->redis->expire($key, $expire);
Expand All @@ -114,10 +109,10 @@ protected function setValues($data, $expire)
protected function addValue($key, $value, $expire)
{
if ($expire == 0) {
return (bool)$this->redis->set($key, $value);
return (bool)$this->redis->setNx($key, $value);
}

return (bool)$this->redis->setex($key, $expire, $value);
return (bool)$this->redis->rawCommand('SET', $key, $value, 'EX', $expire, 'NX');
}

/**
Expand Down
79 changes: 79 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,83 @@ public function testMultiByteGetAndSet()
$cache->set($key, $data);
$this->assertTrue($cache->get($key) === $data);
}

public function testFlushValues()
{
$cache = $this->getCacheInstance();
$key = 'data';
$cache->set($key, 'val');
$cache->flush();
$this->assertFalse($cache->get($key));
}

public function testMultiSet()
{
$cache = $this->getCacheInstance();
$items = [
'k1' => 'v1',
'k2' => 'v2',
'k3' => 'v3',
];
$this->assertEquals([], $cache->multiSet($items, 1));
$this->assertEquals('v1', $cache->get('k1'));
$this->assertEquals('v2', $cache->get('k2'));
$this->assertEquals('v3', $cache->get('k3'));
sleep(1);
$this->assertFalse($cache->get('k1'));
$this->assertFalse($cache->get('k2'));
$this->assertFalse($cache->get('k3'));

$this->assertFalse($cache->exists('k1'));
$this->assertFalse($cache->exists('k2'));
$this->assertFalse($cache->exists('k3'));

$cache->multiSet($items);
sleep(2);
$this->assertEquals('v1', $cache->get('k1'));
$this->assertEquals('v2', $cache->get('k2'));
$this->assertEquals('v3', $cache->get('k3'));
}

public function testSetGet()
{
$cache = $this->getCacheInstance();
$cache->set('key', 'val', 1);
$this->assertEquals('val', $cache->get('key'));
sleep(1);
$this->assertFalse($cache->get('key'));
$this->assertFalse($cache->exists('key'));

$cache->set('key', 'val');
$this->assertTrue($cache->delete('key'));
$this->assertFalse($cache->exists('key'));

}

public function testMultiAdd()
{
$cache = $this->getCacheInstance();
$items = [
'k1' => 'v1',
'k2' => 'v2',
'k3' => 'v3',
];
$cache->multiSet($items);
$this->assertEquals(['k2'], $cache->multiAdd([
'k2' => 'vv22',
'k4' => 'vv44',
]));

$this->assertEquals('v2', $cache->get('k2'));
$this->assertEquals('vv44', $cache->get('k4'));

$this->assertEquals(['k1'], $cache->multiAdd([
'k5' => 'vv55',
'k1' => 'v1',
], 1));
$this->assertEquals('vv55', $cache->get('k5'));
sleep(1);
$this->assertFalse($cache->exists('k5'));
$this->assertTrue($cache->exists('k1'));
}
}

0 comments on commit 80ad874

Please sign in to comment.