mirror of
https://github.com/predis/predis.git
synced 2026-08-17 22:43:43 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ec42796cb2 | |||
| c75bdd9509 | |||
| 12c3e611a0 | |||
| 0c9b822095 | |||
| 030d9740bd | |||
| 0bbbe064b5 | |||
| 87439ae631 | |||
| d8c227e074 | |||
| 7997eab57c | |||
| 2bc61ea0bc | |||
| d667bcb6bb | |||
| e3dd311dd3 | |||
| 34f4d5584b | |||
| 39619d0c54 | |||
| 07a998ebdf | |||
| b73b9682c0 | |||
| 355d6b6cf4 | |||
| fe2316a655 | |||
| 7b2cd4abd0 | |||
| 35fd6ca509 | |||
| dea03a6aa9 | |||
| fb5f878e21 | |||
| e3ee595768 | |||
| cc16311950 | |||
| f32cd19800 |
@@ -1,3 +1,14 @@
|
||||
v0.6.3 (2011-01-01)
|
||||
* New commands available in the Redis v2.2 profile (dev):
|
||||
- Strings: SETRANGE, GETRANGE, SETBIT, GETBIT
|
||||
- Lists : BRPOPLPUSH
|
||||
|
||||
* The abstraction for MULTI/EXEC transactions has been dramatically improved
|
||||
by providing support for check-and-set (CAS) operations when using Redis >=
|
||||
2.2. Aborted transactions can also be optionally replayed in automatic up
|
||||
to a user-defined number of times, after which a Predis\AbortedMultiExec
|
||||
exception is thrown.
|
||||
|
||||
v0.6.2 (2010-11-28)
|
||||
* Minor internal improvements and clean ups.
|
||||
|
||||
|
||||
+3
-2
@@ -20,6 +20,7 @@ to be implemented soon in Predis.
|
||||
- Full support for Redis 2.0. Different versions of Redis are supported via server profiles.
|
||||
- Client-side sharding (support for consistent hashing and custom distribution strategies).
|
||||
- Command pipelining on single and multiple connections (transparent).
|
||||
- Abstraction for Redis transactions (>= 2.0) with support for CAS operations (>= 2.2).
|
||||
- Lazy connections (connections to Redis instances are only established just in time).
|
||||
- Flexible system to define and register your own set of commands to a client instance.
|
||||
|
||||
@@ -60,10 +61,10 @@ Furthermore, a pipeline can be initialized on a cluster of redis instances in th
|
||||
same exact way they are created on single connection. Sharding is still transparent
|
||||
to the user:
|
||||
|
||||
$redis = Predis\Client::create(
|
||||
$redis = new Predis\Client(array(
|
||||
array('host' => '10.0.0.1', 'port' => 6379),
|
||||
array('host' => '10.0.0.2', 'port' => 6379)
|
||||
);
|
||||
));
|
||||
|
||||
$replies = $redis->pipeline(function($pipe) {
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
require_once 'SharedConfigurations.php';
|
||||
|
||||
/*
|
||||
This is an implementation of an atomic client-side ZPOP using the support for
|
||||
check-and-set (CAS) operations with MULTI/EXEC transactions, as described in
|
||||
"WATCH explained" from http://redis.io/topics/transactions
|
||||
|
||||
First, populate your database with a tiny sample data set:
|
||||
|
||||
./redis-cli
|
||||
SELECT 15
|
||||
ZADD zset 1 a
|
||||
ZADD zset 2 b
|
||||
ZADD zset 3 c
|
||||
*/
|
||||
|
||||
function zpop($client, $zsetKey) {
|
||||
$element = null;
|
||||
$options = array(
|
||||
'cas' => true, // Initialize with support for CAS operations
|
||||
'watch' => $zsetKey, // Key that needs to be WATCHed to detect changes
|
||||
'retry' => 3, // Number of retries on aborted transactions, after
|
||||
// which the client bails out with an exception.
|
||||
);
|
||||
|
||||
$txReply = $client->multiExec($options, function($tx)
|
||||
use ($zsetKey, &$element) {
|
||||
@list($element) = $tx->zrange($zsetKey, 0, 0);
|
||||
if (isset($element)) {
|
||||
$tx->multi(); // With CAS, MULTI *must* be explicitly invoked.
|
||||
$tx->zrem($zsetKey, $element);
|
||||
}
|
||||
});
|
||||
return $element;
|
||||
}
|
||||
|
||||
$redis = new Predis\Client($single_server, 'dev');
|
||||
$zpopped = zpop($redis, 'zset');
|
||||
echo isset($zpopped) ? "ZPOPed $zpopped" : "Nothing to ZPOP!", "\n";
|
||||
?>
|
||||
+136
-83
@@ -805,18 +805,15 @@ class CommandPipeline {
|
||||
}
|
||||
|
||||
class MultiExecBlock {
|
||||
private $_initialized, $_discarded, $_insideBlock;
|
||||
private $_initialized, $_discarded, $_insideBlock, $_checkAndSet;
|
||||
private $_redisClient, $_options, $_commands;
|
||||
private $_supportsWatch;
|
||||
|
||||
public function __construct(Client $redisClient, Array $options = null) {
|
||||
$this->checkCapabilities($redisClient);
|
||||
$this->_initialized = false;
|
||||
$this->_discarded = false;
|
||||
$this->_insideBlock = false;
|
||||
$this->_options = $options ?: array();
|
||||
$this->_redisClient = $redisClient;
|
||||
$this->_options = $options ?: array();
|
||||
$this->_commands = array();
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
private function checkCapabilities(Client $redisClient) {
|
||||
@@ -842,55 +839,66 @@ class MultiExecBlock {
|
||||
}
|
||||
}
|
||||
|
||||
private function initialize() {
|
||||
if ($this->_initialized === false) {
|
||||
if (isset($this->_options['watch'])) {
|
||||
$this->watch($this->_options['watch']);
|
||||
}
|
||||
$this->_redisClient->multi();
|
||||
$this->_initialized = true;
|
||||
$this->_discarded = false;
|
||||
}
|
||||
private function reset() {
|
||||
$this->_initialized = false;
|
||||
$this->_discarded = false;
|
||||
$this->_checkAndSet = false;
|
||||
$this->_insideBlock = false;
|
||||
$this->_commands = array();
|
||||
}
|
||||
|
||||
private function setInsideBlock($value) {
|
||||
$this->_insideBlock = $value;
|
||||
private function initialize() {
|
||||
if ($this->_initialized === true) {
|
||||
return;
|
||||
}
|
||||
$options = &$this->_options;
|
||||
$this->_checkAndSet = isset($options['cas']) && $options['cas'];
|
||||
if (isset($options['watch'])) {
|
||||
$this->watch($options['watch']);
|
||||
}
|
||||
if (!$this->_checkAndSet || ($this->_discarded && $this->_checkAndSet)) {
|
||||
$this->_redisClient->multi();
|
||||
if ($this->_discarded) {
|
||||
$this->_checkAndSet = false;
|
||||
}
|
||||
}
|
||||
$this->_initialized = true;
|
||||
$this->_discarded = false;
|
||||
}
|
||||
|
||||
public function __call($method, $arguments) {
|
||||
$this->initialize();
|
||||
$command = $this->_redisClient->createCommand($method, $arguments);
|
||||
$response = $this->_redisClient->executeCommand($command);
|
||||
if (isset($response->queued)) {
|
||||
$this->_commands[] = $command;
|
||||
return $this;
|
||||
$client = $this->_redisClient;
|
||||
if ($this->_checkAndSet) {
|
||||
return call_user_func_array(array($client, $method), $arguments);
|
||||
}
|
||||
else {
|
||||
$this->malformedServerResponse('The server did not respond with a QUEUED status reply');
|
||||
$command = $client->createCommand($method, $arguments);
|
||||
$response = $client->executeCommand($command);
|
||||
if (!isset($response->queued)) {
|
||||
$this->malformedServerResponse(
|
||||
'The server did not respond with a QUEUED status reply'
|
||||
);
|
||||
}
|
||||
$this->_commands[] = $command;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function watch($keys) {
|
||||
$this->isWatchSupported();
|
||||
if ($this->_initialized === true) {
|
||||
throw new \Predis\ClientException('WATCH inside MULTI is not allowed');
|
||||
if ($this->_initialized && !$this->_checkAndSet) {
|
||||
throw new ClientException('WATCH inside MULTI is not allowed');
|
||||
}
|
||||
|
||||
$reply = null;
|
||||
if (is_array($keys)) {
|
||||
$reply = array();
|
||||
foreach ($keys as $key) {
|
||||
$reply = $this->_redisClient->watch($keys);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$reply = $this->_redisClient->watch($keys);
|
||||
}
|
||||
return $reply;
|
||||
return $this->_redisClient->watch($keys);
|
||||
}
|
||||
|
||||
public function multi() {
|
||||
if ($this->_initialized && $this->_checkAndSet) {
|
||||
$this->_checkAndSet = false;
|
||||
$this->_redisClient->multi();
|
||||
return $this;
|
||||
}
|
||||
$this->initialize();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unwatch() {
|
||||
@@ -901,9 +909,8 @@ class MultiExecBlock {
|
||||
|
||||
public function discard() {
|
||||
$this->_redisClient->discard();
|
||||
$this->_commands = array();
|
||||
$this->_initialized = false;
|
||||
$this->_discarded = true;
|
||||
$this->reset();
|
||||
$this->_discarded = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -911,60 +918,89 @@ class MultiExecBlock {
|
||||
return $this->execute();
|
||||
}
|
||||
|
||||
public function execute($block = null) {
|
||||
private function checkBeforeExecution($block) {
|
||||
if ($this->_insideBlock === true) {
|
||||
throw new \Predis\ClientException(
|
||||
"Cannot invoke 'execute' or 'exec' inside an active client transaction block"
|
||||
);
|
||||
}
|
||||
|
||||
if ($block && !is_callable($block)) {
|
||||
throw new \InvalidArgumentException('Argument passed must be a callable object');
|
||||
if ($block) {
|
||||
if (!is_callable($block)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Argument passed must be a callable object'
|
||||
);
|
||||
}
|
||||
if (count($this->_commands) > 0) {
|
||||
throw new ClientException(
|
||||
'Cannot execute a transaction block after using fluent interface'
|
||||
);
|
||||
}
|
||||
}
|
||||
if (isset($this->_options['retry']) && !isset($block)) {
|
||||
$this->discard();
|
||||
throw new \InvalidArgumentException(
|
||||
'Automatic retries can be used only when a transaction block is provided'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$blockException = null;
|
||||
$returnValues = array();
|
||||
public function execute($block = null) {
|
||||
$this->checkBeforeExecution($block);
|
||||
|
||||
if ($block !== null) {
|
||||
$this->setInsideBlock(true);
|
||||
try {
|
||||
$block($this);
|
||||
}
|
||||
catch (CommunicationException $exception) {
|
||||
$blockException = $exception;
|
||||
}
|
||||
catch (ServerException $exception) {
|
||||
$blockException = $exception;
|
||||
}
|
||||
catch (\Exception $exception) {
|
||||
$blockException = $exception;
|
||||
if ($this->_initialized === true) {
|
||||
$this->discard();
|
||||
$reply = null;
|
||||
$returnValues = array();
|
||||
$attemptsLeft = isset($this->_options['retry']) ? (int)$this->_options['retry'] : 0;
|
||||
do {
|
||||
$blockException = null;
|
||||
if ($block !== null) {
|
||||
$this->_insideBlock = true;
|
||||
try {
|
||||
$block($this);
|
||||
}
|
||||
catch (CommunicationException $exception) {
|
||||
$blockException = $exception;
|
||||
}
|
||||
catch (ServerException $exception) {
|
||||
$blockException = $exception;
|
||||
}
|
||||
catch (\Exception $exception) {
|
||||
$blockException = $exception;
|
||||
if ($this->_initialized === true) {
|
||||
$this->discard();
|
||||
}
|
||||
}
|
||||
$this->_insideBlock = false;
|
||||
if ($blockException !== null) {
|
||||
throw $blockException;
|
||||
}
|
||||
}
|
||||
$this->setInsideBlock(false);
|
||||
if ($blockException !== null) {
|
||||
throw $blockException;
|
||||
|
||||
if ($this->_initialized === false || count($this->_commands) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->_initialized === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reply = $this->_redisClient->exec();
|
||||
if ($reply === null) {
|
||||
throw new AbortedMultiExec('The current transaction has been aborted by the server');
|
||||
}
|
||||
$reply = $this->_redisClient->exec();
|
||||
if ($reply === null) {
|
||||
if ($attemptsLeft === 0) {
|
||||
throw new AbortedMultiExec(
|
||||
'The current transaction has been aborted by the server'
|
||||
);
|
||||
}
|
||||
$this->reset();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
} while ($attemptsLeft-- > 0);
|
||||
|
||||
$execReply = $reply instanceof \Iterator ? iterator_to_array($reply) : $reply;
|
||||
$commands = &$this->_commands;
|
||||
$sizeofReplies = count($execReply);
|
||||
|
||||
$commands = &$this->_commands;
|
||||
if ($sizeofReplies !== count($commands)) {
|
||||
$this->malformedServerResponse('Unexpected number of responses for a MultiExecBlock');
|
||||
$this->malformedServerResponse(
|
||||
'Unexpected number of responses for a MultiExecBlock'
|
||||
);
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $sizeofReplies; $i++) {
|
||||
$returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
|
||||
? iterator_to_array($execReply[$i])
|
||||
@@ -977,10 +1013,10 @@ class MultiExecBlock {
|
||||
}
|
||||
|
||||
private function malformedServerResponse($message) {
|
||||
// NOTE: a MULTI/EXEC block cannot be initialized on a clustered
|
||||
// connection, which means that Predis\Client::getConnection
|
||||
// will always return an instance of Predis\Connection.
|
||||
Shared\Utils::onCommunicationException(new MalformedServerResponse(
|
||||
// Since a MULTI/EXEC block cannot be initialized over a clustered
|
||||
// connection, we can safely assume that Predis\Client::getConnection()
|
||||
// will always return an instance of Predis\Connection.
|
||||
Utils::onCommunicationException(new MalformedServerResponse(
|
||||
$this->_redisClient->getConnection(), $message
|
||||
));
|
||||
}
|
||||
@@ -1823,6 +1859,10 @@ class RedisServer_vNext extends RedisServer_v2_0 {
|
||||
|
||||
/* commands operating on string values */
|
||||
'strlen' => '\Predis\Commands\Strlen',
|
||||
'setrange' => '\Predis\Commands\SetRange',
|
||||
'getrange' => '\Predis\Commands\Substr',
|
||||
'setbit' => '\Predis\Commands\SetBit',
|
||||
'getbit' => '\Predis\Commands\GetBit',
|
||||
|
||||
/* commands operating on the key space */
|
||||
'persist' => '\Predis\Commands\Persist',
|
||||
@@ -1831,6 +1871,7 @@ class RedisServer_vNext extends RedisServer_v2_0 {
|
||||
'rpushx' => '\Predis\Commands\ListPushTailX',
|
||||
'lpushx' => '\Predis\Commands\ListPushHeadX',
|
||||
'linsert' => '\Predis\Commands\ListInsert',
|
||||
'brpoplpush' => '\Predis\Commands\ListPopLastPushHeadBlocking',
|
||||
|
||||
/* commands operating on sorted sets */
|
||||
'zrevrangebyscore' => '\Predis\Commands\ZSetReverseRangeByScore',
|
||||
@@ -2357,10 +2398,22 @@ class Append extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'APPEND'; }
|
||||
}
|
||||
|
||||
class SetRange extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'SETRANGE'; }
|
||||
}
|
||||
|
||||
class Substr extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'SUBSTR'; }
|
||||
}
|
||||
|
||||
class SetBit extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'SETBIT'; }
|
||||
}
|
||||
|
||||
class GetBit extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'GETBIT'; }
|
||||
}
|
||||
|
||||
class Strlen extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'STRLEN'; }
|
||||
}
|
||||
@@ -2464,8 +2517,8 @@ class ListPopLastPushHead extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'RPOPLPUSH'; }
|
||||
}
|
||||
|
||||
class ListPopLastPushHeadBulk extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'RPOPLPUSH'; }
|
||||
class ListPopLastPushHeadBlocking extends \Predis\MultiBulkCommand {
|
||||
public function getCommandId() { return 'BRPOPLPUSH'; }
|
||||
}
|
||||
|
||||
class ListPopFirst extends \Predis\MultiBulkCommand {
|
||||
|
||||
@@ -524,12 +524,29 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('bar', $replies[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Predis\ClientException
|
||||
*/
|
||||
function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
|
||||
$emptyBlock = function($tx) { };
|
||||
$tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
|
||||
}
|
||||
|
||||
function testMultiExecBlock_EmptyCallableBlock() {
|
||||
$client = RC::getConnection();
|
||||
$client->flushdb();
|
||||
|
||||
$replies = $client->multiExec(function($multi) { });
|
||||
$this->assertEquals(0, count($replies));
|
||||
|
||||
$options = array('cas' => true);
|
||||
$replies = $client->multiExec($options, function($multi) { });
|
||||
$this->assertEquals(0, count($replies));
|
||||
|
||||
$options = array('cas' => true);
|
||||
$replies = $client->multiExec($options, function($multi) {
|
||||
$multi->multi();
|
||||
});
|
||||
$this->assertEquals(0, count($replies));
|
||||
}
|
||||
|
||||
@@ -609,5 +626,128 @@ class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->assertEquals('client2', $client1->get('sentinel'));
|
||||
}
|
||||
|
||||
function testMultiExecBlock_CheckAndSet() {
|
||||
$client = RC::getConnection();
|
||||
$client->flushdb();
|
||||
$client->set('foo', 'bar');
|
||||
|
||||
$options = array('watch' => 'foo', 'cas' => true);
|
||||
$replies = $client->multiExec($options, function($tx) {
|
||||
$tx->watch('foobar');
|
||||
$foo = $tx->get('foo');
|
||||
$tx->multi();
|
||||
$tx->set('foobar', $foo);
|
||||
$tx->mget('foo', 'foobar');
|
||||
});
|
||||
$this->assertType('array', $replies);
|
||||
$this->assertEquals(array(true, array('bar', 'bar')), $replies);
|
||||
|
||||
$tx = $client->multiExec($options);
|
||||
$tx->watch('foobar');
|
||||
$foo = $tx->get('foo');
|
||||
$replies = $tx->multi()
|
||||
->set('foobar', $foo)
|
||||
->mget('foo', 'foobar')
|
||||
->execute();
|
||||
$this->assertType('array', $replies);
|
||||
$this->assertEquals(array(true, array('bar', 'bar')), $replies);
|
||||
}
|
||||
|
||||
function testMultiExecBlock_RetryOnServerAbort() {
|
||||
$client1 = RC::getConnection();
|
||||
$client2 = RC::getConnection(true);
|
||||
$client1->flushdb();
|
||||
|
||||
$retry = 3;
|
||||
$attempts = 0;
|
||||
RC::testForAbortedMultiExecException($this, function()
|
||||
use($client1, $client2, $retry, &$attempts) {
|
||||
|
||||
$options = array('watch' => 'sentinel', 'retry' => $retry);
|
||||
$client1->multiExec($options, function($tx)
|
||||
use ($client2, &$attempts) {
|
||||
|
||||
$attempts++;
|
||||
$tx->set('sentinel', 'client1');
|
||||
$tx->get('sentinel');
|
||||
$client2->set('sentinel', 'client2');
|
||||
});
|
||||
});
|
||||
$this->assertEquals('client2', $client1->get('sentinel'));
|
||||
$this->assertEquals($retry + 1, $attempts);
|
||||
|
||||
$retry = 3;
|
||||
$attempts = 0;
|
||||
RC::testForAbortedMultiExecException($this, function()
|
||||
use($client1, $client2, $retry, &$attempts) {
|
||||
|
||||
$options = array(
|
||||
'watch' => 'sentinel',
|
||||
'cas' => true,
|
||||
'retry' => $retry
|
||||
);
|
||||
$client1->multiExec($options, function($tx)
|
||||
use ($client2, &$attempts) {
|
||||
|
||||
$attempts++;
|
||||
$tx->incr('attempts');
|
||||
$tx->multi();
|
||||
$tx->set('sentinel', 'client1');
|
||||
$tx->get('sentinel');
|
||||
$client2->set('sentinel', 'client2');
|
||||
});
|
||||
});
|
||||
$this->assertEquals('client2', $client1->get('sentinel'));
|
||||
$this->assertEquals($retry + 1, $attempts);
|
||||
$this->assertEquals($attempts, $client1->get('attempts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
function testMultiExecBlock_RetryNotAvailableWithoutBlock() {
|
||||
$options = array('watch' => 'foo', 'retry' => 1);
|
||||
$tx = RC::getConnection()->multiExec($options);
|
||||
$tx->multi()->get('foo')->exec();
|
||||
}
|
||||
|
||||
function testMultiExecBlock_CheckAndSet_Discard() {
|
||||
$client = RC::getConnection();
|
||||
$client->flushdb();
|
||||
|
||||
$client->set('foo', 'bar');
|
||||
$options = array('watch' => 'foo', 'cas' => true);
|
||||
$replies = $client->multiExec($options, function($tx) {
|
||||
$tx->watch('foobar');
|
||||
$foo = $tx->get('foo');
|
||||
$tx->multi();
|
||||
$tx->set('foobar', $foo);
|
||||
$tx->discard();
|
||||
$tx->mget('foo', 'foobar');
|
||||
});
|
||||
$this->assertType('array', $replies);
|
||||
$this->assertEquals(array(array('bar', null)), $replies);
|
||||
|
||||
$hijack = true;
|
||||
$client->set('foo', 'bar');
|
||||
$client2 = RC::getConnection(true);
|
||||
$options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
|
||||
$replies = $client->multiExec($options, function($tx)
|
||||
use ($client2, &$hijack) {
|
||||
|
||||
$foo = $tx->get('foo');
|
||||
$tx->multi();
|
||||
$tx->set('foobar', $foo);
|
||||
$tx->discard();
|
||||
if ($hijack) {
|
||||
$hijack = false;
|
||||
$client2->set('foo', 'hijacked!');
|
||||
}
|
||||
$tx->mget('foo', 'foobar');
|
||||
});
|
||||
$this->assertType('array', $replies);
|
||||
$this->assertEquals(array(array('hijacked!', null)), $replies);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -20,11 +20,14 @@ class RC {
|
||||
const EXCEPTION_WRONG_TYPE = 'Operation against a key holding the wrong kind of value';
|
||||
const EXCEPTION_NO_SUCH_KEY = 'no such key';
|
||||
const EXCEPTION_OUT_OF_RANGE = 'index out of range';
|
||||
const EXCEPTION_OFFSET_RANGE = 'offset is out of range';
|
||||
const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
|
||||
const EXCEPTION_VALUE_NOT_INT = 'value is not an integer';
|
||||
const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI';
|
||||
const EXCEPTION_SETEX_TTL = 'invalid expire time in SETEX';
|
||||
const EXCEPTION_HASH_VALNOTINT = 'hash value is not an integer';
|
||||
const EXCEPTION_BIT_VALUE = 'bit is not an integer or out of range';
|
||||
const EXCEPTION_BIT_OFFSET = 'bit offset is not an integer or out of range';
|
||||
|
||||
private static $_connection;
|
||||
|
||||
|
||||
+105
-2
@@ -223,6 +223,28 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
|
||||
});
|
||||
}
|
||||
|
||||
function testSetRange() {
|
||||
$this->assertEquals(6, $this->redis->setrange('var', 0, 'foobar'));
|
||||
$this->assertEquals('foobar', $this->redis->get('var'));
|
||||
$this->assertEquals(6, $this->redis->setrange('var', 3, 'foo'));
|
||||
$this->assertEquals('foofoo', $this->redis->get('var'));
|
||||
$this->assertEquals(16, $this->redis->setrange('var', 10, 'barbar'));
|
||||
$this->assertEquals("foofoo\x00\x00\x00\x00barbar", $this->redis->get('var'));
|
||||
|
||||
$this->assertEquals(4, $this->redis->setrange('binary', 0, pack('l', -2147483648)));
|
||||
list($unpacked) = array_values(unpack('l', $this->redis->get('binary')));
|
||||
$this->assertEquals(-2147483648, $unpacked);
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_OFFSET_RANGE, function($test) {
|
||||
$test->redis->setrange('var', -1, 'bogus');
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
|
||||
$test->redis->rpush('metavars', 'foo');
|
||||
$test->redis->setrange('metavars', 0, 'hoge');
|
||||
});
|
||||
}
|
||||
|
||||
function testSubstr() {
|
||||
$this->redis->set('var', 'foobar');
|
||||
$this->assertEquals('foo', $this->redis->substr('var', 0, 2));
|
||||
@@ -252,6 +274,61 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
|
||||
});
|
||||
}
|
||||
|
||||
function testSetBit() {
|
||||
$this->assertEquals(0, $this->redis->setbit('binary', 31, 1));
|
||||
$this->assertEquals(0, $this->redis->setbit('binary', 0, 1));
|
||||
$this->assertEquals(4, $this->redis->strlen('binary'));
|
||||
$this->assertEquals("\x80\x00\00\x01", $this->redis->get('binary'));
|
||||
|
||||
$this->assertEquals(1, $this->redis->setbit('binary', 0, 0));
|
||||
$this->assertEquals(0, $this->redis->setbit('binary', 0, 0));
|
||||
$this->assertEquals("\x00\x00\00\x01", $this->redis->get('binary'));
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
|
||||
$test->redis->setbit('binary', -1, 1);
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
|
||||
$test->redis->setbit('binary', 'invalid', 1);
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
|
||||
$test->redis->setbit('binary', 15, 255);
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
|
||||
$test->redis->setbit('binary', 15, 'invalid');
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
|
||||
$test->redis->rpush('metavars', 'foo');
|
||||
$test->redis->setbit('metavars', 0, 1);
|
||||
});
|
||||
}
|
||||
|
||||
function testGetBit() {
|
||||
$this->redis->set('binary', "\x80\x00\00\x01");
|
||||
|
||||
$this->assertEquals(1, $this->redis->getbit('binary', 0));
|
||||
$this->assertEquals(0, $this->redis->getbit('binary', 15));
|
||||
$this->assertEquals(1, $this->redis->getbit('binary', 31));
|
||||
$this->assertEquals(0, $this->redis->getbit('binary', 63));
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
|
||||
$test->redis->getbit('binary', -1);
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
|
||||
$test->redis->getbit('binary', 'invalid');
|
||||
});
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
|
||||
$test->redis->rpush('metavars', 'foo');
|
||||
$test->redis->getbit('metavars', 0);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* commands operating on the key space */
|
||||
|
||||
@@ -358,7 +435,8 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
|
||||
sleep(2);
|
||||
$this->assertFalse($this->redis->exists('hoge'));
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_VALUE_NOT_INT, function($test) {
|
||||
// TODO: do not check the error message RC::EXCEPTION_VALUE_NOT_INT for now
|
||||
RC::testForServerException($this, null, function($test) {
|
||||
$test->redis->setex('hoge', 2.5, 'piyo');
|
||||
});
|
||||
RC::testForServerException($this, RC::EXCEPTION_SETEX_TTL, function($test) {
|
||||
@@ -733,6 +811,31 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals((float)(time() - $start), 2, '', 1);
|
||||
}
|
||||
|
||||
function testListBlockingPopLastPushHead() {
|
||||
// TODO: this test does not cover all the aspects of BLPOP/BRPOP as it
|
||||
// does not run with a concurrent client pushing items on lists.
|
||||
$numbers = RC::pushTailAndReturn($this->redis, 'numbers', array(1, 2, 3));
|
||||
$src_count = count($numbers);
|
||||
$dst_count = 0;
|
||||
|
||||
while ($item = $this->redis->brpoplpush('numbers', 'temporary', 1)) {
|
||||
$this->assertEquals(--$src_count, $this->redis->llen('numbers'));
|
||||
$this->assertEquals(++$dst_count, $this->redis->llen('temporary'));
|
||||
$this->assertEquals(array_pop($numbers), $this->redis->lindex('temporary', 0));
|
||||
}
|
||||
|
||||
$start = time();
|
||||
$this->assertNull($this->redis->brpoplpush('numbers', 'temporary', 2));
|
||||
$this->assertEquals(2, (float)(time() - $start), '', 1);
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
|
||||
$test->redis->del('numbers');
|
||||
$test->redis->del('temporary');
|
||||
$test->redis->set('numbers', 'foobar');
|
||||
$test->redis->brpoplpush('numbers', 'temporary', 1);
|
||||
});
|
||||
}
|
||||
|
||||
function testListInsert() {
|
||||
$numbers = RC::pushTailAndReturn($this->redis, 'numbers', RC::getArrayOfNumbers());
|
||||
|
||||
@@ -745,7 +848,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
|
||||
|
||||
RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
|
||||
$test->redis->set('foo', 'bar');
|
||||
$test->redis->lset('foo', 0, 0);
|
||||
$test->redis->linsert('foo', 'before', 0, 0);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user