2021-05-20 16:53:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Cache\Repository;
|
|
|
|
|
2021-05-21 22:56:42 +08:00
|
|
|
use App\Traits\StaticInstance;
|
2021-05-20 16:53:34 +08:00
|
|
|
use App\Cache\Contracts\SetRedisInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis Set
|
|
|
|
*
|
|
|
|
* @package App\Cache\Repository
|
|
|
|
*/
|
2021-05-21 22:56:42 +08:00
|
|
|
class SetRedis extends AbstractRedis implements SetRedisInterface
|
2021-05-20 16:53:34 +08:00
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
protected $prefix = 'rds-set';
|
2021-05-20 16:53:34 +08:00
|
|
|
|
2021-05-21 22:56:42 +08:00
|
|
|
protected $name = 'default';
|
2021-05-20 16:53:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加集合元素
|
|
|
|
*
|
|
|
|
* @param string ...$member
|
|
|
|
* @return bool|int
|
|
|
|
*/
|
|
|
|
public function add(string ...$member)
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->sAdd($this->getCacheKey(), ...$member);
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除集合元素
|
|
|
|
*
|
|
|
|
* @param string ...$member
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function rem(string ...$member)
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->sRem($this->getCacheKey(), ...$member);
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断是否是集合元素
|
|
|
|
*
|
|
|
|
* @param string $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isMember(string $member)
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->sIsMember($this->getCacheKey(), $member);
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取集合中所有元素
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->sMembers($this->getCacheKey());
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取集合中元素个数
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->scard($this->getCacheKey());
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取随机集合中的元素
|
|
|
|
*
|
|
|
|
* @param int $count
|
|
|
|
* @return array|bool|mixed|string
|
|
|
|
*/
|
|
|
|
public function randMember($count = 1)
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return $this->redis()->sRandMember($this->getCacheKey(), $count);
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除 Set 集合表
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
2021-05-21 22:56:42 +08:00
|
|
|
return (bool)$this->redis()->del($this->getCacheKey());
|
2021-05-20 16:53:34 +08:00
|
|
|
}
|
|
|
|
}
|