-
-
Save romainneutron/4398479 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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