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;
|
|
|
|
|
|
|
|
class Set extends Key implements IteratorAggregate {
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function add($value): self {
|
|
|
|
$this->getRedis()->sadd($this->getKey(), $value);
|
2018-04-18 02:17:25 +05:30
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function remove($value): self {
|
|
|
|
$this->getRedis()->srem($this->getKey(), $value);
|
2018-04-18 02:17:25 +05:30
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function members(): array {
|
|
|
|
return $this->getRedis()->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
|
|
|
|
|
|
|
return (bool)$this->getRedis()->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 {
|
|
|
|
return $this->getRedis()->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
|
|
|
}
|
|
|
|
|
|
|
|
}
|