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

Commit 626e76d

Browse files
author
Bob Chengbin
authored
Merge pull request #3 from dcb9/addTests
Add tests
2 parents 3bc1bd0 + bc38576 commit 626e76d

File tree

5 files changed

+91
-16
lines changed

5 files changed

+91
-16
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ php:
55
- '5.5'
66
- '5.6'
77
- '7.0'
8+
- '7.1'
89

910
services:
1011
- redis-server
1112

12-
before_install: echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
13+
before_install: v=$(phpenv version-name); if [ ${v:0:1} -lt 7 ]; then pecl install redis; else echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
1314

1415
install:
1516
- composer global require "fxp/composer-asset-plugin:^1.2.0"

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
/**

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ It includes a `Cache` and `Session` storage handler in redis.
1818
Requirements
1919
------------
2020

21-
- PHP >=5.4.0
21+
- PHP >= 5.4.0
2222
- Redis >= 2.6.12
23-
- ext-redis >=2.2.7
23+
- ext-redis >= 2.2.7
2424
- Yii2 ~2.0.4
2525

2626
Installation

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"yiisoft/yii2": "~2.0.4",
14-
"ext-redis": ">=2.2.5",
14+
"ext-redis": ">=2.2.7",
1515
"php": ">=5.4.0"
1616
},
1717
"autoload": {
@@ -20,7 +20,7 @@
2020
}
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "*",
23+
"phpunit/phpunit": "<6.0",
2424
"yiisoft/yii2-redis": "^2.0"
2525
}
2626
}

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)