Skip to content

Instantly share code, notes, and snippets.

@romainneutron
Forked from anonymous/gist:4398451
Last active December 10, 2015 07:08
Show Gist options
  • Save romainneutron/4398479 to your computer and use it in GitHub Desktop.
Save romainneutron/4398479 to your computer and use it in GitHub Desktop.
<?php
use Predis\Async\Client as PredisClient;
function save(PredisClient $redis)
{
$deferred = new Deferred();
$saddDeferred = new Deferred();
// let's start a transaction
$tx = $redis->multiExec();
// transaction has began
// increment the job-counter key
$tx->incr('job-counter');
$tx->execute(function ($replies, $redis) use ($job, $deferred, $saddDeferred) {
// we execute the transaction here
// the result of the commands previously executed are in the $reply array
// here, $replies[0] contains the Id
$hashId = 'job-' . $replies[0];
$deferred->then(function() use ($redis, $hashId, $saddDeferred) {
// once the promised is resolved, execute SADD as second command to store the hash key in a set
$redis->sadd('jobs', $hashId, function() use ($hashId, $saddDeferred) {
// once the second command is executed, resolve the promise
$saddDeferred->resolve($hashId);
});
return $saddDeferred->promise();
});
// lets build the argument of the HMSET command that is used to store hashes :
// HMSET key field1 value1 field2 value2
$hash = array_merge(array($hashId), array('key' => 'value'), array(function() use ($deferred, $hashId){
// once the command has been executed, lets resolve the promise
$deferred->resolve($hashId);
}));
// execute the command
call_user_func_array(array($redis, 'hmset'), $hash);
});
return $saddDeferred->promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment