Skip to content

Commit

Permalink
Squashed 'src/redis/' changes from d0f9102..61469f2
Browse files Browse the repository at this point in the history
61469f2 fix #82
6faa845 Fix Travis Ci (#190)
db8883f 修改timeout为float,增加协程Redis timeout配置 (#188)
395dd1d Fix php7.0 travis ci failed (#167)
1f84117 完善Redis代码提醒 (#165)
bed4d1b 修改 Redis::ZRangeByScore 与 Redis::zRevRangeByScore 入参有误的BUG (#163 (comment))

git-subtree-dir: src/redis
git-subtree-split: 61469f25a5e48f11188de9da480e91822f7ff3de
  • Loading branch information
swoft-bot committed Oct 6, 2018
1 parent 03fca08 commit 0521932
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 45 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"autoload-dev": {
"psr-4": {
"SwoftTest\\Redis\\": "test/Cases"
"SwoftTest\\Redis\\": "test/Cases",
"SwoftTest\\Redis\\Testing\\": "test/Testing"
}
},
"repositories": [
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function parseUri(string $uri): array
*
* @return Redis | \Redis
*/
abstract protected function getConnectRedis(string $host, int $port, int $timeout);
abstract protected function getConnectRedis(string $host, int $port, float $timeout);

/**
* @param string $method
Expand Down
25 changes: 3 additions & 22 deletions src/Operator/ZSets/ZSetRangeByScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ public function getId()
*/
protected function prepareOptions($options)
{
$opts = array_change_key_case($options, CASE_UPPER);
$finalizedOpts = array();

if (isset($opts['LIMIT']) && is_array($opts['LIMIT'])) {
$limit = array_change_key_case($opts['LIMIT'], CASE_UPPER);

$finalizedOpts[] = 'LIMIT';
$finalizedOpts[] = isset($limit['OFFSET']) ? $limit['OFFSET'] : $limit[0];
$finalizedOpts[] = isset($limit['COUNT']) ? $limit['COUNT'] : $limit[1];
}

return array_merge($finalizedOpts, parent::prepareOptions($options));
return [$options];
}

/**
Expand All @@ -39,16 +28,8 @@ protected function prepareOptions($options)
protected function withScores()
{
$arguments = $this->getArguments();

for ($i = 3; $i < count($arguments); ++$i) {
switch (strtoupper($arguments[$i])) {
case 'WITHSCORES':
return true;

case 'LIMIT':
$i += 2;
break;
}
if (isset($arguments[3]) && is_array($arguments[3]) && array_key_exists('withscores', $arguments[3])) {
return true;
}

return false;
Expand Down
9 changes: 9 additions & 0 deletions src/Profile/RedisProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ abstract class RedisProfile implements ProfileInterface
*/
protected $processor;

/**
* Setter prefix
* @param string $prefix
*/
public function setPrefix(string $prefix)
{
$this->processor->setPrefix($prefix);
}

/**
* Init
*/
Expand Down
74 changes: 74 additions & 0 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,80 @@
* Psr 16 implement by Redis
* @method string getLastError()
* @method bool clearLastError()
* key and string
* @method int append($key, $value)
* @method int decr($key)
* @method int decrBy($key, $value)
* @method string getRange($key, $start, $end)
* @method int incr($key)
* @method int incrBy($key, $value)
* @method float incrByFloat($key, $increment)
* @method int strlen($key)
* @method bool mset( array $array )
* hash
* @method int hSet($key, $hashKey, $value)
* @method bool hSetNx($key, $hashKey, $value)
* @method string hGet($key, $hashKey)
* @method int hLen($key)
* @method int hDel($key, $hashKey1, $hashKey2 = null, $hashKeyN = null)
* @method array hKeys($key)
* @method array hVals($key)
* @method array hGetAll($key)
* @method bool hExists($key, $hashKey)
* @method bool hIncrBy($key, $hashKey, $value)
* @method bool hIncrByFloat($key, $field, $increment)
* @method bool hMset($key, $hashKeys)
* list
* @method array brPop(array $keys, $timeout)
* @method array blPop(array $keys, $timeout)
* @method int lLen($key)
* @method int lPush($key, $value1, $value2 = null, $valueN = null)
* @method string lPop($key)
* @method array lRange($key, $start, $end)
* @method int lRem($key, $value, $count)
* @method bool lSet($key, $index, $value)
* @method int rPush($key, $value1, $value2 = null, $valueN = null)
* @method string rPop($key)
* set
* @method int sAdd($key, $value1, $value2 = null, $valueN = null)
* @method array|bool scan(&$iterator, $pattern = null, $count = 0)
* @method int sCard($key)
* @method array sDiff($key1, $key2, $keyN = null)
* @method array sInter($key1, $key2, $keyN = null)
* @method int sInterStore($dstKey, $key1, $key2, $keyN = null)
* @method int sDiffStore($dstKey, $key1, $key2, $keyN = null)
* @method array sMembers($key)
* @method bool sMove($srcKey, $dstKey, $member)
* @method bool sPop($key)
* @method string|array sRandMember($key, $count = null)
* @method int sRem($key, $member1, $member2 = null, $memberN = null)
* @method array sUnion($key1, $key2, $keyN = null)
* @method int sUnionStore($dstKey, $key1, $key2, $keyN = null)
* sort
* @method int zAdd($key, $score1, $value1, $score2 = null, $value2 = null, $scoreN = null, $valueN = null)
* @method array zRange($key, $start, $end, $withscores = null)
* @method int zRem($key, $member1, $member2 = null, $memberN = null)
* @method array zRevRange($key, $start, $end, $withscore = null)
* @method array zRangeByScore($key, $start, $end, array $options = array())
* @method array zRangeByLex($key, $min, $max, $offset = null, $limit = null)
* @method int zCount($key, $start, $end)
* @method int zRemRangeByScore($key, $start, $end)
* @method int zRemRangeByRank($key, $start, $end)
* @method int zCard($key)
* @method float zScore($key, $member)
* @method int zRank($key, $member)
* @method float zIncrBy($key, $value, $member)
* @method int zUnion($Output, $ZSetKeys, array $Weights = null, $aggregateFunction = 'SUM')
* @method int zInter($Output, $ZSetKeys, array $Weights = null, $aggregateFunction = 'SUM')
* pub/sub
* @method int publish($channel, $message)
* @method string|array psubscribe($patterns, $callback)
* @method string|array subscribe($channels, $callback)
* @method array|int pubsub($keyword, $argument)
* script
* @method mixed eval($script, $args = array(), $numKeys = 0)
* @method mixed evalSha($scriptSha, $args = array(), $numKeys = 0)
* @method mixed script($command, $script)
*/
class Redis implements CacheInterface
{
Expand Down
11 changes: 4 additions & 7 deletions src/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function __call($method, $arguments)
{
/* @var RedisCommandProvider $commandProvider */
$commandProvider = App::getBean(RedisCommandProvider::class);
$commandProvider->setPrefix($this->pool->getPoolConfig()->getPrefix());
$command = $commandProvider->createCommand($method, $arguments);
$arguments = $command->getArguments();
$method = $command->getId();
Expand All @@ -79,22 +80,18 @@ public function __call($method, $arguments)


/**
* @param string $host
* @param int $port
* @param int $timeout
*
* @return CoRedis
* @throws RedisException
*/
protected function getConnectRedis(string $host, int $port, int $timeout): CoRedis
protected function getConnectRedis(string $host, int $port, float $timeout): CoRedis
{
/* @var RedisPoolConfig $poolConfig */
$poolConfig = $this->pool->getPoolConfig();
$serialize = $poolConfig->getSerialize();
$serialize = ((int)$serialize == 0) ? false : true;
$redis = new CoRedis();
$redis = new CoRedis(['timeout' => $timeout]);
$result = $redis->connect($host, $port, $serialize);
if ($result == false) {
if ($result === false) {
$error = sprintf('Redis connection failure host=%s port=%d', $host, $port);
throw new RedisException($error);
}
Expand Down
8 changes: 2 additions & 6 deletions src/SyncRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,14 @@ public function setDefer($defer = true)
}

/**
* @param string $host
* @param int $port
* @param int $timeout
*
* @return \Redis
* @throws RedisException
*/
protected function getConnectRedis(string $host, int $port, int $timeout): \Redis
protected function getConnectRedis(string $host, int $port, float $timeout): \Redis
{
$redis = new \Redis();
$result = $redis->connect($host, $port, $timeout);
if ($result == false) {
if ($result === false) {
$error = sprintf('Redis connection failure host=%s port=%d', $host, $port);
throw new RedisException($error);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Cases/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class AbstractTestCase extends TestCase
/**
* @var \Swoft\Redis\Redis
*/
protected $redis;
public $redis;

public function __construct($name = null, array $data = [], $dataName = '')
{
Expand Down
28 changes: 28 additions & 0 deletions test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace SwoftTest\Redis;

use Swoft\App;
use Swoft\Redis\Exception\RedisException;
use SwoftTest\Redis\Pool\RedisEnvPoolConfig;
use SwoftTest\Redis\Pool\RedisPptPoolConfig;
use SwoftTest\Redis\Testing\Clients\TimeoutRedis;
use SwoftTest\Redis\Testing\Pool\TimeoutPool;

/**
* PoolTest
Expand Down Expand Up @@ -45,4 +48,29 @@ public function testRedisPoolEnv()
$this->assertEquals($pConfig->getMaxIdleTime(), 60);
$this->assertEquals($pConfig->getTimeout(), 3);
}

public function testRedisTimeout()
{
try {
$redis = bean(TimeoutRedis::class);
$redis->exists('not_connected');
} catch (\Exception $ex) {
$this->assertInstanceOf(\RedisException::class, $ex);
$this->assertTrue(strpos($ex->getMessage(), 'timed out') > 0);
}


go(function () {
$redis = bean(TimeoutRedis::class);
$btime = microtime(true);
try {
$redis->exists('not_connected');
} catch (\Exception $ex) {
$this->assertInstanceOf(RedisException::class, $ex);
$this->assertEquals('Redis connection failure host=echo.swoft.org port=6379', $ex->getMessage());
$this->assertTrue(microtime(true) - $btime < 2);
}
});

}
}
2 changes: 1 addition & 1 deletion test/Cases/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testEvalArray()
$this->assertTrue(is_array($result));

foreach ($result as $index => $value) {
$this->assertEquals($expected[ $index ], $value);
$this->assertEquals($expected[$index], $value);
}
});
}
Expand Down
36 changes: 35 additions & 1 deletion test/Cases/ZsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testZadd()
$keys = $this->redis->zRange($key, 0, -1);
$this->assertCount(5, $keys);

$data = [
$data = [
'key4',
'key2',
'key3',
Expand All @@ -55,6 +55,40 @@ public function testZadd()

$rangeKeys = $this->redis->zRange($key, 1.2, 3.2, 'xxx');
$this->assertEquals($data2, $rangeKeys);

/** @var \Redis $redis */
$redis = $this->redis;
$rangeKeys = $redis->zRangeByScore($key, 1, 2, [
'limit' => [1, 1]
]);
$this->assertEquals(['key4'], $rangeKeys);

$rangeKeys = $redis->zRangeByScore($key, 1, 2, [
'withscores' => true,
'limit' => [1, 1]
]);
$this->assertEquals(['key4' => 1.2], $rangeKeys);

$rangeKeys = $redis->zRangeByScore($key, 1.2, 3.2, [
'withscores' => true
]);
$this->assertEquals($data2, $rangeKeys);

$rangeKeys = $redis->zRevRangeByScore($key, 2, 1, [
'limit' => [0, 1]
]);
$this->assertEquals(['key2'], $rangeKeys);

$rangeKeys = $redis->zRevRangeByScore($key, 2, 1, [
'limit' => [0, 1],
'withscores' => true
]);
$this->assertEquals(['key2' => 1.3], $rangeKeys);

$rangeKeys = $redis->zRevRangeByScore($key, 3.2, 1.2, [
'withscores' => true
]);
$this->assertEquals(['key3' => 3.2, 'key2' => 1.3, 'key4' => 1.2], $rangeKeys);
}

public function testZaddByCo()
Expand Down
19 changes: 19 additions & 0 deletions test/Testing/Clients/TimeoutRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace SwoftTest\Redis\Testing\Clients;

use Swoft\Redis\Redis;
use SwoftTest\Redis\Testing\Pool\TimeoutPool;
use Swoft\Bean\Annotation\Bean;

/**
* Class TimeoutRedis
* @Bean
* @package SwoftTest\Redis\Testing\Clients
*/
class TimeoutRedis extends Redis
{
/**
* @var string
*/
protected $poolName = TimeoutPool::class;
}
27 changes: 27 additions & 0 deletions test/Testing/Pool/Config/TimeoutPoolConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace SwoftTest\Redis\Testing\Pool\Config;

use Swoft\Redis\Pool\Config\RedisPoolConfig;
use Swoft\Bean\Annotation\Bean;

/**
* Class TimeoutPoolConfig
* @Bean
* @package SwoftTest\Redis\Testing\Pool\Config
*/
class TimeoutPoolConfig extends RedisPoolConfig
{
/**
* the time of connect timeout
* @var int
*/
protected $timeout = 1;

/**
* the addresses of connection
* @var array
*/
protected $uri = [
'echo.swoft.org:6379'
];
}
24 changes: 24 additions & 0 deletions test/Testing/Pool/TimeoutPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace SwoftTest\Redis\Testing\Pool;

use Swoft\Redis\Pool\RedisPool;
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Inject;
use Swoft\Bean\Annotation\Pool;
use SwoftTest\Redis\Testing\Pool\Config\TimeoutPoolConfig;

/**
* Class TimeoutPool
* @Pool
* @package SwoftTest\Redis\Testing\Pool
*/
class TimeoutPool extends RedisPool
{
/**
* Config
*
* @Inject()
* @var TimeoutPoolConfig
*/
protected $poolConfig;
}
Loading

0 comments on commit 0521932

Please sign in to comment.