Используем в качестве компонента для соединения с редисом библиотеку Predis

This commit is contained in:
ErickSkrauch
2016-11-27 17:41:39 +03:00
parent bec00df77b
commit 744ec9520a
13 changed files with 472 additions and 22 deletions

View File

@@ -0,0 +1,58 @@
<?php
namespace common\components\Redis;
use InvalidArgumentException;
use Yii;
class Key {
protected $key;
/**
* @return \yii\redis\Connection
*/
public function getRedis() {
return Yii::$app->redis;
}
public function getKey() {
return $this->key;
}
public function getValue() {
return json_decode($this->getRedis()->get($this->key), true);
}
public function setValue($value) {
$this->getRedis()->set($this->key, json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return $this;
}
public function delete() {
$this->getRedis()->executeCommand('DEL', [$this->key]);
return $this;
}
public function expire($ttl) {
$this->getRedis()->executeCommand('EXPIRE', [$this->key, $ttl]);
return $this;
}
private function buildKey(array $parts) {
$keyParts = [];
foreach($parts as $part) {
$keyParts[] = str_replace('_', ':', $part);
}
return implode(':', $keyParts);
}
public function __construct(...$key) {
if (empty($key)) {
throw new InvalidArgumentException('You must specify at least one key.');
}
$this->key = $this->buildKey($key);
}
}