redis = Instance::ensure($this->redis, Connection::className()); $this->redis->open(); } /** * @inheritdoc */ public function exists($key) { $key = $this->buildKey($key); return (bool)$this->redis->exists($key); } /** * @inheritdoc */ protected function getValue($key) { return $this->redis->get($key); } /** * @inheritdoc */ protected function getValues($keys) { $response = $this->redis->mget($keys); $result = []; $i = 0; foreach ($keys as $key) { $result[$key] = $response[$i++]; } return $result; } /** * @inheritdoc */ protected function setValue($key, $value, $expire) { if ($expire == 0) { return (bool)$this->redis->set($key, $value); } else { return (bool)$this->redis->setEx($key, $expire, $value); } } /** * @inheritdoc */ protected function setValues($data, $expire) { $failedKeys = []; if ($expire == 0) { $this->redis->mSet($data); } else { $expire = (int)$expire; $this->redis->multi(); $this->redis->mSet($data); $index = []; foreach ($data as $key => $value) { $this->redis->expire($key, $expire); $index[] = $key; } $result = $this->redis->exec(); array_shift($result); foreach ($result as $i => $r) { if ($r != 1) { $failedKeys[] = $index[$i]; } } } return $failedKeys; } /** * @inheritdoc */ protected function addValue($key, $value, $expire) { if ($expire == 0) { return (bool)$this->redis->setNx($key, $value); } return (bool)$this->redis->rawCommand('SET', $key, $value, 'EX', $expire, 'NX'); } /** * @inheritdoc */ protected function deleteValue($key) { return (bool)$this->redis->del($key); } /** * @inheritdoc */ protected function flushValues() { return $this->redis->flushdb(); } }