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

Commit 80ad874

Browse files
committed
Fix some bugs, Add more tests covered Cache.php everwhere
1 parent 3bc1bd0 commit 80ad874

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

Cache.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function setValue($key, $value, $expire)
6969
if ($expire == 0) {
7070
return (bool)$this->redis->set($key, $value);
7171
} else {
72-
return (bool)$this->redis->setex($key, $expire, $value);
72+
return (bool)$this->redis->setEx($key, $expire, $value);
7373
}
7474
}
7575

@@ -78,18 +78,13 @@ protected function setValue($key, $value, $expire)
7878
*/
7979
protected function setValues($data, $expire)
8080
{
81-
$args = [];
82-
foreach ($data as $key => $value) {
83-
$args[] = $key;
84-
$args[] = $value;
85-
}
8681
$failedKeys = [];
8782
if ($expire == 0) {
88-
$this->redis->mset($args);
83+
$this->redis->mSet($data);
8984
} else {
90-
$expire = (int)($expire * 1000);
85+
$expire = (int)$expire;
9186
$this->redis->multi();
92-
$this->redis->mset($args);
87+
$this->redis->mSet($data);
9388
$index = [];
9489
foreach ($data as $key => $value) {
9590
$this->redis->expire($key, $expire);
@@ -114,10 +109,10 @@ protected function setValues($data, $expire)
114109
protected function addValue($key, $value, $expire)
115110
{
116111
if ($expire == 0) {
117-
return (bool)$this->redis->set($key, $value);
112+
return (bool)$this->redis->setNx($key, $value);
118113
}
119114

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

123118
/**

tests/CacheTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,83 @@ public function testMultiByteGetAndSet()
7575
$cache->set($key, $data);
7676
$this->assertTrue($cache->get($key) === $data);
7777
}
78+
79+
public function testFlushValues()
80+
{
81+
$cache = $this->getCacheInstance();
82+
$key = 'data';
83+
$cache->set($key, 'val');
84+
$cache->flush();
85+
$this->assertFalse($cache->get($key));
86+
}
87+
88+
public function testMultiSet()
89+
{
90+
$cache = $this->getCacheInstance();
91+
$items = [
92+
'k1' => 'v1',
93+
'k2' => 'v2',
94+
'k3' => 'v3',
95+
];
96+
$this->assertEquals([], $cache->multiSet($items, 1));
97+
$this->assertEquals('v1', $cache->get('k1'));
98+
$this->assertEquals('v2', $cache->get('k2'));
99+
$this->assertEquals('v3', $cache->get('k3'));
100+
sleep(1);
101+
$this->assertFalse($cache->get('k1'));
102+
$this->assertFalse($cache->get('k2'));
103+
$this->assertFalse($cache->get('k3'));
104+
105+
$this->assertFalse($cache->exists('k1'));
106+
$this->assertFalse($cache->exists('k2'));
107+
$this->assertFalse($cache->exists('k3'));
108+
109+
$cache->multiSet($items);
110+
sleep(2);
111+
$this->assertEquals('v1', $cache->get('k1'));
112+
$this->assertEquals('v2', $cache->get('k2'));
113+
$this->assertEquals('v3', $cache->get('k3'));
114+
}
115+
116+
public function testSetGet()
117+
{
118+
$cache = $this->getCacheInstance();
119+
$cache->set('key', 'val', 1);
120+
$this->assertEquals('val', $cache->get('key'));
121+
sleep(1);
122+
$this->assertFalse($cache->get('key'));
123+
$this->assertFalse($cache->exists('key'));
124+
125+
$cache->set('key', 'val');
126+
$this->assertTrue($cache->delete('key'));
127+
$this->assertFalse($cache->exists('key'));
128+
129+
}
130+
131+
public function testMultiAdd()
132+
{
133+
$cache = $this->getCacheInstance();
134+
$items = [
135+
'k1' => 'v1',
136+
'k2' => 'v2',
137+
'k3' => 'v3',
138+
];
139+
$cache->multiSet($items);
140+
$this->assertEquals(['k2'], $cache->multiAdd([
141+
'k2' => 'vv22',
142+
'k4' => 'vv44',
143+
]));
144+
145+
$this->assertEquals('v2', $cache->get('k2'));
146+
$this->assertEquals('vv44', $cache->get('k4'));
147+
148+
$this->assertEquals(['k1'], $cache->multiAdd([
149+
'k5' => 'vv55',
150+
'k1' => 'v1',
151+
], 1));
152+
$this->assertEquals('vv55', $cache->get('k5'));
153+
sleep(1);
154+
$this->assertFalse($cache->exists('k5'));
155+
$this->assertTrue($cache->exists('k1'));
156+
}
78157
}

0 commit comments

Comments
 (0)