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;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class Set extends Key implements IteratorAggregate {
|
|
|
|
|
|
|
|
/**
|
2016-11-27 20:11:39 +05:30
|
|
|
* @return Connection
|
2016-02-14 23:20:10 +05:30
|
|
|
*/
|
|
|
|
public static function getDb() {
|
2016-07-17 21:43:40 +05:30
|
|
|
return Yii::$app->redis;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function add($value) {
|
2016-11-27 20:11:39 +05:30
|
|
|
static::getDb()->sadd($this->key, $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($value) {
|
2016-11-27 20:11:39 +05:30
|
|
|
static::getDb()->srem($this->key, $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function members() {
|
2016-11-27 20:11:39 +05:30
|
|
|
return static::getDb()->smembers($this->key);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue() {
|
|
|
|
return $this->members();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exists($value) {
|
2016-11-27 20:11:39 +05:30
|
|
|
return (bool)static::getDb()->sismember($this->key, $value);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function diff(array $sets) {
|
2016-11-27 20:11:39 +05:30
|
|
|
return static::getDb()->sdiff([$this->key, 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|