2016-02-14 20:50:10 +03:00
|
|
|
<?php
|
2016-11-27 17:41:39 +03:00
|
|
|
namespace common\components\Redis;
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2016-11-27 17:41:39 +03:00
|
|
|
use ArrayIterator;
|
2016-02-14 20:50:10 +03:00
|
|
|
use IteratorAggregate;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class Set extends Key implements IteratorAggregate {
|
|
|
|
|
|
|
|
/**
|
2016-11-27 17:41:39 +03:00
|
|
|
* @return Connection
|
2016-02-14 20:50:10 +03:00
|
|
|
*/
|
|
|
|
public static function getDb() {
|
2016-07-17 19:13:40 +03:00
|
|
|
return Yii::$app->redis;
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add($value) {
|
2016-11-27 17:41:39 +03:00
|
|
|
static::getDb()->sadd($this->key, $value);
|
2016-02-14 20:50:10 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($value) {
|
2016-11-27 17:41:39 +03:00
|
|
|
static::getDb()->srem($this->key, $value);
|
2016-02-14 20:50:10 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function members() {
|
2016-11-27 17:41:39 +03:00
|
|
|
return static::getDb()->smembers($this->key);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue() {
|
|
|
|
return $this->members();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exists($value) {
|
2016-11-27 17:41:39 +03:00
|
|
|
return (bool)static::getDb()->sismember($this->key, $value);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function diff(array $sets) {
|
2016-11-27 17:41:39 +03:00
|
|
|
return static::getDb()->sdiff([$this->key, implode(' ', $sets)]);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getIterator() {
|
2016-11-27 17:41:39 +03:00
|
|
|
return new ArrayIterator($this->members());
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|