本文整理汇总了PHP中Predis\Client::transaction方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::transaction方法的具体用法?PHP Client::transaction怎么用?PHP Client::transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Predis\Client
的用法示例。
在下文中一共展示了Client::transaction方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: remove
/**
* @param User $user
*/
public function remove(User $user)
{
$this->redisClient->transaction(function (MultiExec $tx) use($user) {
$id = $user->getId();
$tx->hdel(self::USER_HASH_STORE, $id);
if (!empty($user->getGithubId())) {
$tx->hdel('github_' . self::USER_HASH_STORE, $user->getGithubId());
}
if (!empty($user->getGoogleId())) {
$tx->hdel('google_' . self::USER_HASH_STORE, $user->getGoogleId());
}
if (!empty($user->getBitbucketId())) {
$tx->hdel('bitbucket_' . self::USER_HASH_STORE, $user->getBitbucketId());
}
});
}
示例2: incrementAndCount
/**
* Calls the increment() and count() function using a single MULTI/EXEC block.
*
* @param string $subject A unique identifier, for example a session id or an IP
* @param int $interval Interval in seconds
*
* @return int
*/
public function incrementAndCount($subject, $interval)
{
$bucket = $this->getBucket();
$subject = $this->key . ':' . $subject;
$count = (int) floor($interval / $this->bucketInterval);
$multi = $this->client->transaction();
$this->addMultiExecIncrement($multi, $subject, $bucket);
$this->addMultiExecCount($multi, $subject, $bucket, $count);
return array_sum(array_slice($multi->exec(), 4));
}
示例3: pop
/**
* @inheritdoc
*/
public function pop($queue)
{
foreach ([':delayed', ':reserved'] as $type) {
$options = ['cas' => true, 'watch' => $queue . $type];
$this->redis->transaction($options, function (MultiExec $transaction) use($queue, $type) {
$data = $this->redis->zrangebyscore($queue . $type, '-inf', $time = time());
if (!empty($data)) {
$transaction->zremrangebyscore($queue . $type, '-inf', $time);
$transaction->rpush($queue, $data);
}
});
}
$data = $this->redis->lpop($queue);
if ($data === null) {
return false;
}
$this->redis->zadd($queue . ':reserved', [$data => time() + $this->expire]);
$data = Json::decode($data);
return ['id' => $data['id'], 'body' => $data['body'], 'queue' => $queue];
}
示例4: work
public function work()
{
foreach ($this->limits as $index => $limit) {
$counterName = "queue:{$this->tube}:throttle:{$index}";
$started = (bool) $this->redis->setnx($counterName, $limit['requests']);
$counter = $this->redis->get($counterName);
if ($started) {
$this->redis->transaction(function (MultiExec $transaction) use($counterName, $limit) {
$transaction->expire($counterName, $limit['seconds']);
$transaction->decr($counterName);
});
} else {
if ($counter > 1) {
$this->redis->decr($counterName);
} else {
$this->pheanstalk->pauseTube($this->tube, $this->redis->ttl($counterName));
}
}
}
}
示例5: save
/**
* @param object $object
*/
public function save($object)
{
if (!is_object($object)) {
throw new InvalidArgumentException(sprintf('You must pass an object to Tystr\\RedisOrm\\Repository\\PredisRepository::save(), %s given.', gettype($object)));
}
$metadata = $this->getMetadataFor($this->className);
$key = $this->keyNamingStrategy->getKeyName(array($metadata->getPrefix(), $this->getIdForClass($object, $metadata)));
$originalData = $this->redis->hgetall($key);
$transaction = $this->redis->transaction();
$transaction->hmset($key, $newData = $this->hydrator->toArray($object, $metadata));
// @todo compute changeset here
$this->handleProperties($object, $metadata, $originalData, $newData, $transaction);
$transaction->execute();
}
示例6: purge
/**
* {@inheritdoc}
*/
protected function purge()
{
try {
$this->client->transaction(function ($tx) {
$keyPrefix = $this->client->getOptions()->prefix;
foreach (new \Predis\Collection\Iterator\Keyspace($this->client, '*') as $key) {
$tx->del(substr($key, strlen($keyPrefix)));
}
});
} catch (\Predis\Transaction\AbortedMultiExecException $e) {
return false;
}
return true;
}
示例7: setCurrentValue
/**
* @param $key
* @param $value
* @param $expire
* @param $now
*/
private function setCurrentValue($namespace, $key, $value, $expire = 0)
{
$namespaceKey = $this->buildNamespaceKey($namespace, $key);
$currentKey = $this->buildCurrentKey($namespaceKey);
$previousKey = $this->buildPreviousKey($namespaceKey);
list($previousValue, , ) = $this->redis->transaction(function (MultiExec $tx) use($currentKey, $previousKey, $value, $expire) {
$tx->get($previousKey);
$tx->set($currentKey, $value);
if ($expire > 0) {
$tx->expire($currentKey, $expire);
}
});
$this->previousValues[$namespaceKey] = $previousValue;
$this->currentValues[$namespaceKey] = $value;
}
示例8: flush
/**
*
*/
public function flush()
{
$this->client->transaction(function () {
foreach ($this->buffer as $name => $options) {
$ttl = $this->client->ttl($name);
$exists = $this->client->exists($name);
$this->client->set($name, $options['value']);
if ($options['expire'] > 0) {
$this->client->expire($name, $options['expire']);
}
if ($options['expire'] < 0) {
if (!$exists || $ttl < 0) {
$this->client->expireat($name, time() + $options['expire'] * -1);
} else {
$this->client->expire($name, $ttl);
}
}
}
});
}
示例9: set
/**
* @param Session $session
*/
public function set(Session $session)
{
$sid = $session->getSessionId();
$redisId = $this->buildKey($sid);
if ($session->shouldKill()) {
$this->redis->del($redisId);
} else {
$this->redis->transaction(function (MultiExec $tx) use($session, $redisId) {
foreach ($session->getDeletedKeys() as $key) {
$tx->hdel($redisId, $key);
}
$changes = $session->getChanges();
if (count($changes) > 0) {
$tx->hmset($redisId, $session->getChanges());
}
// session regeneration is not supported
$tx->expire($redisId, $this->getExpires());
});
$this->oracleKeepAlive($sid);
}
}
示例10: set
/**
* @param Session $session
*/
public function set(Session $session)
{
$redisId = $this->buildKey($session->getSessionId());
if ($session->shouldKill()) {
$this->redis->del($redisId);
} else {
$this->redis->transaction(function (MultiExec $tx) use($session, $redisId) {
foreach ($session->getDeletedKeys() as $key) {
$tx->hdel($redisId, $key);
}
$changes = $session->getChanges();
if (count($changes) > 0) {
$tx->hmset($redisId, $session->getChanges());
}
if ($session->shouldRegenerate()) {
$session->setSid(UUID::v4());
$newRedisId = $this->buildKey($session->getSessionId());
$tx->rename($redisId, $newRedisId);
$redisId = $newRedisId;
}
$tx->expire($redisId, $this->getExpires());
});
}
}
示例11: testMethodTransactionIsAliasForMethodMultiExec
/**
* @group disconnected
*/
public function testMethodTransactionIsAliasForMethodMultiExec()
{
$client = new Client();
$this->assertInstanceOf('Predis\\Transaction\\MultiExecContext', $client->transaction());
}
示例12: testTransactionWithArrayAndCallableExecutesMultiExec
/**
* @group disconnected
*/
public function testTransactionWithArrayAndCallableExecutesMultiExec()
{
// We use CAS here as we don't care about the actual MULTI/EXEC context.
$options = array('cas' => true, 'retry' => 3);
$connection = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
$connection->expects($this->once())->method('executeCommand')->will($this->returnValue(new Response\Status('QUEUED')));
$txCallback = function ($tx) {
$tx->ping();
};
$callable = $this->getMock('stdClass', array('__invoke'));
$callable->expects($this->once())->method('__invoke')->will($this->returnCallback($txCallback));
$client = new Client($connection);
$client->transaction($options, $callable);
}
示例13: getAndDeleteKey
public function getAndDeleteKey($key)
{
$responses = $this->_client->transaction()->get($key)->del(array($key))->execute();
return $responses[0];
}