Creates a new transaction context and returns it, or returns the results
of a transaction executed inside the optionally provided callable object.
public transaction ( ) : |
||
Результат |
/** * @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()); } }); }
/** * 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)); }
/** * @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]; }
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)); } } } }
/** * @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(); }
/** * {@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; }
/** * @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; }
/** * */ 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); } } } }); }
/** * @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); } }
/** * @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()); }); } }
/** * @group disconnected */ public function testMethodTransactionIsAliasForMethodMultiExec() { $client = new Client(); $this->assertInstanceOf('Predis\\Transaction\\MultiExecContext', $client->transaction()); }
/** * @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); }
public function getAndDeleteKey($key) { $responses = $this->_client->transaction()->get($key)->del(array($key))->execute(); return $responses[0]; }