/** * {@inheritdoc} */ protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) { if ($lifetime) { $success = true; // Keys have lifetime, use SETEX for each of them foreach ($keysAndValues as $key => $value) { $response = $this->client->setex($key, $lifetime, serialize($value)); if ((string) $response != 'OK') { $success = false; } } return $success; } // No lifetime, use MSET $response = $this->client->mset(array_map(function ($value) { return serialize($value); }, $keysAndValues)); return (string) $response == 'OK'; }
/** * @param array $items * * @throws \Exception * * @return void */ public function setMulti(array $items) { $data = []; foreach ($items as $key => $value) { $dataKey = $this->getKeyName($key); if (!is_scalar($value)) { $value = json_encode($value); } $data[$dataKey] = $value; } if (count($data) === 0) { return; } $result = $this->client->mset($data); $this->addMultiWriteAccessStats($data); if (!$result) { throw new \Exception('could not set redisKeys for items: "[' . implode(',', array_keys($items)) . ']" with values: "[' . implode(',', array_values($items)) . ']"'); } }
/** * @group redis-strings */ public function testMultipleGet() { $this->client->mset(array('foo' => 'bar', 'hello' => 'world')); $this->assertEquals(array('bar', 'world'), $this->client->mget('foo', 'hello')); }