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: