/** * {@inheritdoc} */ public function close() { $buffer =& $this->buffer; $this->redis->multiExec(function ($multi) use($buffer) { foreach ($buffer as $record) { $multi->rpush('monolog', $record); } }); }
/** * @param ModelInterface $model * @param array $ids * @return array */ public function getMany(ModelInterface $model, array $ids = []) { $table = $this->getTableName($model); $this->db->multiExec(); $data = []; if (empty($ids)) { $keys = $this->db->keys($table . ':*'); foreach ($keys as $key) { $json = $this->db->get($key); $data[] = $model->fromRaw(json_decode($json)); } } else { foreach ($ids as $id) { $data[] = $this->getById($model, $id); } } return $data; }
/** * {@inheritdoc} */ public function close() { if ($this->redis instanceof \Redis) { $multi = $this->redis->multi(); foreach ($this->buffer as $record) { $multi->rpush($this->key, $record); } $multi->exec(); } else { $key =& $this->key; $buffer =& $this->buffer; $this->redis->multiExec(function ($multi) use($key, $buffer) { foreach ($buffer as $record) { $multi->rpush($key, $record); } }); } }
/** * 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->multiExec(); $this->addMultiExecIncrement($multi, $subject, $bucket); $this->addMultiExecCount($multi, $subject, $bucket, $count); return array_sum(array_slice($multi->exec(), 4)); }
/** * {@inheritdoc} */ public function swapItems($firstIndex, $secondIndex) { $this->initializeRedis(); $key = $this->getKeyForItemList(); $options = array('cas' => true, 'watch' => $key, 'retry' => 3); $this->redis->multiExec($options, function ($tx) use($key, $firstIndex, $secondIndex) { $firstItem = $tx->lindex($key, $firstIndex); $secondItem = $tx->lindex($key, $secondIndex); $tx->multi(); $tx->lset($key, $firstIndex, $secondItem); $tx->lset($key, $secondIndex, $firstItem); }); }
/** * @param string $id * @param string[] $tags * @return bool */ public function removeTags($id, array $tags) { if (count($tags) > 0) { $transaction = $this->redis->multiExec(); $commandArgs = $tags; array_unshift($commandArgs, $this->getTagsForIdKey($id)); $command = $this->redis->createCommand('srem', $commandArgs); $transaction->executeCommand($command); foreach ($tags as $tag) { $command = $this->redis->createCommand('srem', array($this->getIdsForTagKey($tag), $id)); $transaction->executeCommand($command); } $responses = $transaction->exec(); foreach ($responses as $response) { if ($response instanceof ResponseErrorInterface) { return false; } } } return true; }
/** * @group disconnected */ public function testMultiExecWithArrayAndCallableExecutesMultiExec() { // NOTE: we use CAS since testing the actual MULTI/EXEC context // here is not the point. $options = array('cas' => true, 'retry' => 3); $connection = $this->getMock('Predis\\Connection\\SingleConnectionInterface'); $connection->expects($this->once())->method('executeCommand')->will($this->returnValue(new ResponseQueued())); $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->multiExec($options, $callable); }