2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2016-11-27 20:11:39 +05:30
|
|
|
namespace common\components\Redis;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Yii;
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2016-02-14 23:20:10 +05:30
|
|
|
class Key {
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
private $key;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function __construct(...$key) {
|
|
|
|
if (empty($key)) {
|
|
|
|
throw new InvalidArgumentException('You must specify at least one key.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->key = $this->buildKey($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getKey(): string {
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue() {
|
2018-07-08 20:50:19 +05:30
|
|
|
return Yii::$app->redis->get($this->key);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function setValue($value): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->set($this->key, $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function delete(): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->del($this->getKey());
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function exists(): bool {
|
2018-07-08 20:50:19 +05:30
|
|
|
return (bool)Yii::$app->redis->exists($this->key);
|
2016-11-29 04:27:58 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function expire(int $ttl): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->expire($this->key, $ttl);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function expireAt(int $unixTimestamp): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->expireat($this->key, $unixTimestamp);
|
2016-11-30 04:49:14 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
private function buildKey(array $parts): string {
|
2016-02-14 23:20:10 +05:30
|
|
|
$keyParts = [];
|
2018-04-18 02:17:25 +05:30
|
|
|
foreach ($parts as $part) {
|
2016-02-14 23:20:10 +05:30
|
|
|
$keyParts[] = str_replace('_', ':', $part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(':', $keyParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|