Skip to content

Commit

Permalink
Add test cases & bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ComMouse committed Jul 13, 2015
1 parent 7036c35 commit 794a9af
Show file tree
Hide file tree
Showing 14 changed files with 1,107 additions and 170 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ before_script:
- if [ $PHP_VER = "5" ]; then phpenv config-add myconfig.ini; fi

script:
- phpunit
- phpunit --test-suite=predis
- phpunit --test-suite=predis-noset
- if [ $PHP_VER = "5" ]; then phpunit --test-suite=php-redis; phpunit --test-suite=php-redis-noset;fi
- sh -c "./vendor/bin/phpcs --standard=PSR2 ./src/"

services:
Expand Down
13 changes: 9 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="basic">
<directory>./tests</directory>
<exclude>./tests/RedisTest.php</exclude>
<testsuite name="predis">
<file>./tests/predis/PredisTestSet.php</file>
</testsuite>
<testsuite name="predis-noset">
<file>./tests/predis/PredisTestNoSet.php</file>
</testsuite>
<testsuite name="php-redis">
<file phpVersion="7.0.0" phpVersionOperator="&lt;=">./tests/RedisTest.php</file>
<file phpVersion="7.0.0" phpVersionOperator="&lt;">./tests/phpredis/RedisTestSet.php</file>
</testsuite>
<testsuite name="php-redis-noset">
<file phpVersion="7.0.0" phpVersionOperator="&lt;">./tests/phpredis/RedisTestNoSet.php</file>
</testsuite>
</testsuites>
</phpunit>
4 changes: 3 additions & 1 deletion src/Dy/Cache/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Dy\Cache;

use Dy\Common\SingletonFacade;
use Dy\Redis\ClientInterface as Client;

/**
* Class RedisCache
Expand All @@ -19,8 +20,9 @@
* @package Dy\Cache
* @see RedisRepository
*
* @method static Client client()
* @method static RedisRepository setNamespace(string $namespace, mixed $lazyRecord = null)
* @method static string getNamespace(string $namespace)
* @method static string getNamespace()
* @method static RedisRepository enableMemoryCache()
* @method static RedisRepository disableMemoryCache()
* @method static bool usingMemoryCache()
Expand Down
2 changes: 1 addition & 1 deletion src/Dy/Cache/RedisNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function clearAllKeys()
$this->client->srem($this->getKeyName($this->keySetName), $keys);
}
} else {
$regexp = preg_quote($this->namespace) . '*';
$regexp = preg_quote($this->getPrefix()) . '*';
$cursor = 0;
do {
$result = $this->client->scan($cursor, $regexp);
Expand Down
40 changes: 26 additions & 14 deletions src/Dy/Cache/RedisRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public function __construct(array $config)
$this->namespaceLazyRecord = (bool)$config['namespace']['lazy_record'];
$this->keySetName = $config['namespace']['key_set_name'];
$this->setNamespace($config['namespace']['name']);
$this->enableMemoryCache = $config['memory_cache'];
if ($this->enableMemoryCache) {
$this->memoryCache = new MemoryRepository();
if ($config['memory_cache']) {
$this->enableMemoryCache();
}
}

Expand All @@ -89,6 +88,15 @@ public function __destruct()
$this->clearNamespace();
}

/**
* Get the redis client.
* @return Client
*/
public function client()
{
return $this->client;
}

/**
* Switch to a new namespace.
*
Expand Down Expand Up @@ -126,8 +134,10 @@ public function getNamespace()
public function enableMemoryCache()
{
$this->enableMemoryCache = true;
if ($this->memoryCache === null) {
if ($this->memoryCache !== null) {
$this->memoryCache->clearAll();
} else {
$this->memoryCache = new MemoryRepository();
}
return $this;
}
Expand Down Expand Up @@ -199,11 +209,13 @@ public function get($key, $default = null)

$key = $this->getKeyName($key);
$value = $this->client->get($key);
$value = $value !== null ?
(is_numeric($value) ? $value : unserialize($value)) :
($default instanceof Closure ? $default() : $default);
$this->recordKey($key, $value);
return $value;
if ($value != false) {
$value = is_numeric($value) ? $value : unserialize($value);
$this->recordKey($key, $value);
return $value;
} else {
return $default instanceof Closure ? $default() : $default;
}
}

/**
Expand All @@ -218,7 +230,7 @@ public function has($key)
if ($cachedValue !== null) {
return true;
}
return $this->client->exists($this->getKeyName($key)) === 1;
return $this->client->exists($this->getKeyName($key));
}

/**
Expand Down Expand Up @@ -331,7 +343,7 @@ public function keysByNamespace($namespace)
if ($this->namespace->toString() == $namespace) {
return $this->getAllKeys();
}
$namespaceObj = new RedisNamespace($namespace, $this->client);
$namespaceObj = new RedisNamespace($namespace, $this->client, $this->keySetName);
$keys = $namespaceObj->getAllKeys();
unset($namespaceObj);
return $keys;
Expand All @@ -347,7 +359,7 @@ public function keysByNamespace($namespace)
public function clearAll()
{
$this->namespace->clearAllKeys();
if ($this->memoryCache !== null) {
if ($this->enableMemoryCache) {
$this->memoryCache->clearAll();
}
return $this;
Expand All @@ -368,7 +380,7 @@ public function delByNamespace($namespace)
$this->clearAll();
return $this;
}
$namespaceObj = new RedisNamespace($namespace, $this->client);
$namespaceObj = new RedisNamespace($namespace, $this->client, $this->keySetName);
$namespaceObj->clearAllKeys();
unset($namespaceObj);
return $this;
Expand Down Expand Up @@ -402,7 +414,7 @@ protected function clearNamespace()
unset($this->namespace);
}

if ($this->memoryCache !== null) {
if ($this->enableMemoryCache) {
$this->memoryCache->clearAll();
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Dy/Redis/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function quit();
* Returns if key exists.
*
* @param $key
* @return int
* @return bool
*/
public function exists($key);

Expand All @@ -37,7 +37,6 @@ public function exists($key);
*
* @param string $key
* @param string $value
* @return mixed
*/
public function set($key, $value);

Expand All @@ -48,17 +47,16 @@ public function set($key, $value);
* @param string $key
* @param int $seconds
* @param string $value
* @return int
*/
public function setex($key, $seconds, $value);

/**
* Get the value of key. If the key does not exist the special value
* null is returned. An error is returned if the value stored at key
* false is returned. An error is returned if the value stored at key
* is not a string, because GET only handles string values.
*
* @param string $key
* @return string
* @return string|false
*/
public function get($key);

Expand Down Expand Up @@ -151,7 +149,7 @@ public function scard($key);
*
* @param string $key
* @param string $member
* @return int
* @return bool
*/
public function sismember($key, $member);

Expand Down
18 changes: 8 additions & 10 deletions src/Dy/Redis/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function quit()
/**
* @inheritdoc
* @param string $key
* @return int
* @return bool
*/
public function exists($key)
{
Expand All @@ -64,29 +64,27 @@ public function exists($key)
* @inheritdoc
* @param string $key
* @param string $value
* @return mixed
*/
public function set($key, $value)
{
return $this->redis->set($key, $value);
$this->redis->set($key, $value);
}

/**
* @inheritdoc
* @param string $key
* @param int $seconds
* @param string $value
* @return int
*/
public function setex($key, $seconds, $value)
{
return $this->redis->setex($key, $seconds, $value);
$this->redis->setex($key, $seconds, $value);
}

/**
* @inheritdoc
* @param string $key
* @return string
* @return string|false
*/
public function get($key)
{
Expand Down Expand Up @@ -160,18 +158,18 @@ public function keys($pattern)
* @param int $cursor
* @param string $pattern
* @param int $count
* @return array|bool
* @return array
*/
public function scan($cursor, $pattern = '', $count = 0)
{
$options = array();
$option = array();
if ($pattern != '') {
$option['match'] = $pattern;
}
if ($count != 0) {
$option['count'] = $count;
}
return $this->redis->scan($cursor, $options);
return $this->redis->scan($cursor, $option);
}

/**
Expand Down Expand Up @@ -199,7 +197,7 @@ public function scard($key)
* @inheritdoc
* @param string $key
* @param string $member
* @return int
* @return bool
*/
public function sismember($key, $member)
{
Expand Down
Loading

0 comments on commit 794a9af

Please sign in to comment.