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

Commit

Permalink
Merge pull request #3 from dcb9/addTests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
dcb9 authored Jun 29, 2017
2 parents 3bc1bd0 + bc38576 commit 626e76d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ php:
- '5.5'
- '5.6'
- '7.0'
- '7.1'

services:
- redis-server

before_install: echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
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

install:
- composer global require "fxp/composer-asset-plugin:^1.2.0"
Expand Down
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ It includes a `Cache` and `Session` storage handler in redis.
Requirements
------------

- PHP >=5.4.0
- PHP >= 5.4.0
- Redis >= 2.6.12
- ext-redis >=2.2.7
- ext-redis >= 2.2.7
- Yii2 ~2.0.4

Installation
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"yiisoft/yii2": "~2.0.4",
"ext-redis": ">=2.2.5",
"ext-redis": ">=2.2.7",
"php": ">=5.4.0"
},
"autoload": {
Expand All @@ -20,7 +20,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "*",
"phpunit/phpunit": "<6.0",
"yiisoft/yii2-redis": "^2.0"
}
}
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 626e76d

Please sign in to comment.