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
|
|
|
|
2016-11-27 20:11:39 +05:30
|
|
|
use ArrayIterator;
|
2016-02-14 23:20:10 +05:30
|
|
|
use IteratorAggregate;
|
2018-07-08 20:50:19 +05:30
|
|
|
use Yii;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
class Set extends Key implements IteratorAggregate {
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function add($value): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->sadd($this->getKey(), $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function remove($value): self {
|
2018-07-08 20:50:19 +05:30
|
|
|
Yii::$app->redis->srem($this->getKey(), $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function members(): array {
|
2018-07-08 20:50:19 +05:30
|
|
|
return Yii::$app->redis->smembers($this->getKey());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function getValue(): array {
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this->members();
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function exists(string $value = null): bool {
|
2016-11-29 04:27:58 +05:30
|
|
|
if ($value === null) {
|
|
|
|
return parent::exists();
|
|
|
|
}
|
2017-09-19 22:36:16 +05:30
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
return (bool)Yii::$app->redis->sismember($this->getKey(), $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function diff(array $sets): array {
|
2018-07-08 20:50:19 +05:30
|
|
|
return Yii::$app->redis->sdiff([$this->getKey(), implode(' ', $sets)]);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
2016-11-27 20:11:39 +05:30
|
|
|
return new ArrayIterator($this->members());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|