Releases: predis/predis
Predis v0.6.3
Predis is a flexible and feature-complete PHP client library for Redis. This is a maintenance release for the 0.6 series that features support for check and set (CAS) operations using the Predis\MultiExecBlock
abstraction for MULTI
/EXEC
transactions and the addition of the remaining commands that will be part of Redis 2.2 (now in the RC stage). As with previous releases, Predis is also available for PHP 5.2 with an officially supported backport (PHP >= 5.2.6). What follows is an overview of the new features introduced in this new release. For a more in-depth list of changes please see the CHANGELOG.
New features and changes
Transactions with CAS support
With the addition of the WATCH
command in Redis 2.2, it is now possible to use optimistic locking on keys to provide check and set operations. The abstraction for MULTI
/EXEC
implemented by the Predis\MultiExecBlock
class now provides the ability to leverage this powerful concept by initializing a transaction with the CAS option set to true
:
$options = array('cas' => true, 'watch' => 'foo');
$replies = $redis->multiExec($options, function($tx) {
$foo = $tx->get('foo');
// when cas => true, we *must* explicitly call MULTI
$tx->multi();
$tx->set('foobar', "atomic $foo!");
$tx->mget('foo', 'foobar');
});
In case another client modified one of the WATCHed keys causing the current transaction to be aborted by the server, by default the client throws a Predis\AbortedMultiExec
exception. By using the retry
option, it is possible to instruct the client to transparently retry a certain number of times before giving up and throwing an exception:
$options = array('retry' => 2, 'cas' => true, 'watch' => 'foo');
$replies = $redis->multiExec($options, function($tx) {
// attempts to execute this block for 3 times before giving up
});
It should be noted that developers can use the new CAS mode when writing code using the fluent interface API, with the only difference that the automatic retry mechanism for aborted transaction is not available (which means developers must roll their own solution):
$tx = $redis->multiExec(array('watch' => 'foo', 'cas' => true));
$foo = $tx->get('foo');
$replies = $tx->multi()
->set('foobar', "atomic $foo!")
->mget('foo', 'foobar')
->exec();
New commands for Redis v2.2
All of the commands added in Redis v2.2 are now available with this release using the dev
profile. Here is a list of the new commands added since Predis v0.6.2:
Notes
Downloads
Useful links
Predis v0.6.2
Predis is a flexible and feature-complete PHP client library for Redis. This is a maintenance release for the 0.6 series that mainly features internal optimizations, a more stable support for transactions (MULTI/EXEC) and comes with a bunch of new commands for Redis 2.2-dev. As with previous releases, Predis is also available for PHP 5.2 with an officially supported backport (PHP >= 5.2.6). What follows is an overview of the new features introduced in this new release. For a more in-depth list of changes please see the CHANGELOG.
New features and changes
Miscellaneous
Support MULTI/EXEC has been internally improved to handle a few corner cases with client-side exceptions and aborted transaction.
Aside from a bunch of new commands added for Redis 2.2 (STRLEN
, LINSERT
, RPUSHX
, LPUSHX
, ZREVRANGEBYSCORE
, PERSIST
) and a couple of fixes for SORT
, now WATCH
can be also called with an array of keys:
$redis->watch(array('foo', 'bar'));
Method chaining with UNWATCH
and DISCARD
using Predis\MultiExecBlock
has been fixed and now it is possible to do:
$replies = $redis->multiExec(array('watch' => 'foo'))
->set('foo', 'bar')
->unwatch()
->discard()
->set('foo', 'bar')
->execute();
Notes
Downloads
Useful links
Predis v0.6.1
Predis is a flexible and feature-complete PHP client library for Redis. This is a maintenance release for the 0.6 series featuring some internal optimizations and a more stable and consistent support for pipelines and transactions (MULTI/EXEC) without any breaking changes. As with previous releases, Predis is also available for PHP 5.2 with an officially supported backport (PHP >= 5.2.6). What follows is an overview of the new features introduced in this new release. For a more in-depth list of changes please see the CHANGELOG.
New features and changes
Transactions (MULTI/EXEC)
Support for dynamic arguments has been added to the Predis\Client::multiExec()
method which can also accept an optional array of parameters to initialize the underlying transaction. Moreover, support for the WATCH
and UNWATCH
commands has been added when using the current development profile (Redis v2.2):
$transaction = $redis->multiExec();
$transaction->watch('foo')->get('foo')->unwatch('foo')->execute();
// or ...
$options = array('watch' => 'foo');
$transaction = $redis->multiExec($options, function($transaction) {
$transaction->get('foo');
$transaction->unwatch('foo');
});
See the WATCH
and UNWATCH
commands in the command reference of Redis for more details.
Command pipelines
Despite having been introduced with 0.6.0, the Predis\Client::pipelineSafe()
method has been already deprecated (and might be removed in the next major release) in favour of the usual Predis\Client::pipeline()
method with added support for dynamic arguments and an optional array of parameters used to initialize the underlying pipeline. The following lines of code are equivalent:
// the old way, deprecated but still valid for compatibility
$redis->pipelineSafe(function($pipe) {
// ...
});
// the new way
$redis->pipeline(array('safe' => true), function($pipe) {
// ...
});
// get a pipeline object instead of passing a callable
$pipe = $redis->pipeline(array('safe' => true));
Miscellaneous
Predis\MultiExecBlock
and Predis\PubSubContext
now throw an exception when trying to use features that depend on commands not supported by the server profile that is being used for the client:
$profile = '2.0'; // Redis 2.0 does not support WATCH and UNWATCH
$redis = new Predis\Client('redis://127.0.0.1/', '2.0');
$transaction = $redis->multiExec();
$transaction->watch("foo"); // throws a Predis\ClientException
An exception is also raised when trying to initialize Predis\MultiExecBlock
and Predis\PubSubContext
using a client connected to a cluster of servers since both work only on single connections.
Optional modifiers for ZRANGE
, ZREVRANGE
and ZRANGEBYSCORE
can now be passed as an associative array to their respective methods. Also, ZRANGEBYSCORE
now support the LIMIT
modifier:
$redis->zrangebyscore('zsetkey', 1, 100, array(
'limit' => array('offset' => 1, 'count' => 2), // or simply array(1, 2)
'withscores' => true,
));
Notes
Downloads
Useful links
Predis v0.6.0
Predis is a flexible and feature-complete PHP client library for Redis. This release provides developers an even more stable, customizable and up-to-date client than previous versions, with just a tiny bit of overhead added when compared to the 0.5.x series. As with previous releases, Predis is also available for PHP 5.2 with an officially supported backport (PHP >= 5.2.6). What follows is an overview of the new features introduced in this new release. For a more in-depth list of changes plese see the CHANGELOG.
New features and changes
Server profiles
Predis 0.6.0 is fully compatible with Redis 2.0 (which has reached the RC stages just a few days ago at the time of writing) and its command set. The default server profile is set to 2.0
. Those who are using older versions of Redis should specify the correct server profile accordingly when instantiating a new client (highly recommended):
$redis = new Predis\Client('redis://127.0.0.1/', '1.2');
For compatibility reasons, the old way of specifying which server profile to use is still valid:
$profile = Predis\RedisServerProfile::get('1.2');
$redis = new Predis\Client('redis://127.0.0.1/', $profile);
Please note that default support for Redis 1.0 has been removed and it is now provided only when including Predis_Compatibility.php as follows:
require_once('lib/Predis.php');
require_once('lib/Predis_Compatibility.php');
$redis = new Predis\Client('redis://127.0.0.1/', '1.0');
In accordance with the latest recommendations, Predis now uses the so-called new protocol (every command is serialized as a multibulk request) when connecting to Redis >= 1.2. The server profile for Redis 1.0 still uses inline and bulk requests though.
Client options
Now the second argument for Predis\Client::__construct()
accepts also an array of client-level options or an instance of Predis\ClientOptions.
$options = array(
'profile' => '2.0',
'key_distribution' => 'Predis\Distribution\HashRing',
'throw_on_error' => true,
'iterable_multibulk' => false,
);
$redis = new Predis\Client('redis://127.0.0.1/', $options);
- profile [default:
2.0
]:
specifies which server profile to use. Accepts a version string (e.g.1.2
,2.0
,default
,dev
) or an instance of a class that inherits fromPredis\RedisServerProfile
. - key_distribution [cluster only - default:
Predis\Distribution\HashRing
]:
specifies which distribution strategy to use to distribute keys when connecting to a cluster of Redis servers. Accepts a full class name (as a string) or an instance of a class that implements thePredis\Distribution\IDistributionStrategy
interface. - throw_on_error [default:
true
]:
specifies if server errors throw exceptions or they are returned as instances ofPredis\ResponseError
. Accepts boolean values. - iterable_multibulk [default:
false
]:
specifies if multibulk replies are returned as arrays or as iterator instances (the latter enables the client to stream replies down to application's code). Accepts boolean values.
Connection parameters
The following example shows the new connection parameters that are in addition to the ones previously supported.
$connection_parameters = array(
'alias' => 'cache-a',
'weight' => 100,
'connection_async' => false,
'connection_persistent' => false,
);
$redis = new Predis\Client($connection_parameters);
- alias [cluster only - default: not set]:
identifies each connection by providing a mnemonic alias. Accepts string values. - weight [cluster only - default: not set]:
specifies a weight used to balance the distribution of keys asymmetrically across multiple servers. Accepts integer values. - connection_async [default:
false
]:
specifies if connections to servers are estabilished in a non-blocking way (the client is not blocked while the underlying resource performs the actual connection). Accepts boolean values. - connection_persistent [default:
false
]:
specifies if the underlying connection resource should be left open when a script ends its lifecycle. Accepts boolean values.
Command pipelines
Command pipelines now support method chaining.
$replies = $redis->pipeline()->set('key', 'value')->get('key')->execute();
The new method Predis\Client::pipelineSafe()
initializes a command pipeline that does not throw exceptions on server, protocol or connection errors. Instead, the generated exceptions are returned as values in the replies array. This new approach is useful when pipelines are used in a clustered environment since it enables the client to continue processing commands even if one or more servers in the cluster generate an error or become unavailable.
Transactions (Redis 2.0)
Predis 0.5.x already provided Redis transaction blocks via Predis\Client::multiExec()
. The current implementation now supports method chaining and the ability to discard the commands issued in a transaction (see the DISCARD
command in the command reference of Redis).
Publish / Subscribe (Redis 2.0)
Predis 0.6.0 supports PUBSUB contexts via PHP iterators with the new method Predis\Client::pubSubContext()
. See the following example for more details.
Miscellaneous
It is now possible to get a new client instance for a single connection of a cluster by passing its alias to the method Predis\Client::getClientFor()
. The new client inherits the same options of the originator client.
$cluster = new Predis\Client(array(
'redis://127.0.0.1:6379?alias=first',
'redis://127.0.0.1:6379?alias=second'
));
$first = $cluster->getClientFor('first');
$first->info();
The Predis\RedisServerProfile
class now allows developers to register their own server profiles or override the default ones provided by Predis.
class MyCustomProfile extends Predis\RedisServerProfile {
public function getVersion() { return '2.2'; }
public function getSupportedCommands() {
// Implementation here...
}
}
Predis\RedisServerProfile::registerProfile('MyCustomProfile', 'custom');
$redis = new Predis\Client('redis://127.0.0.1/', 'custom');
Notes
Roadmap for future releases
Since this new version of Predis already offers a multitude of new features and enhancements, the next releases in the 0.6.x series should contain only bug fixes, performance improvements and minor changes or additions. All the big new features will be reserved to the 0.7.x series, whose development cycle will be heavily influenced by the development cycle of Redis. Depending on the work needed to support future releases of Redis, 0.7.x might introduce some breaking changes. Long aliases for commands (e.g. $redis->setMultiple()
, $redis->getMultiple()
and such) are discouraged and should be considered obsolete as they could be removed in future major releases of Predis, depending on the feedback from the community.
Credits
I would like to especially thank Lorenzo Castelli for providing a whole lot of suggestions and reference code that hugely contributed to the final implementation of many new features shipped in this new version of Predis (see partial failures in pipelines, asynchronous connects, persistent connections, improvements in the current hashring implementation and a few minor fixes).
Thanks also to those who have reported bugs on the issue tracker and, finally, to all those who have sent me emails with words of appreciation for the work that has been made in Predis: these words are shared with anyone who contributed to make this new release possible.