hyperf-chat/app/Cache/Repository/SetRedis.php

93 lines
1.8 KiB
PHP
Raw Normal View History

2021-05-20 16:53:34 +08:00
<?php
2021-08-28 16:11:38 +08:00
declare(strict_types=1);
2021-05-20 16:53:34 +08:00
namespace App\Cache\Repository;
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
*/
2021-08-28 16:11:38 +08:00
public function rem(string ...$member): int
2021-05-20 16:53:34 +08:00
{
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
*/
2021-08-28 16:11:38 +08:00
public function isMember(string $member): bool
2021-05-20 16:53:34 +08:00
{
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
*/
2021-08-28 16:11:38 +08:00
public function all(): array
2021-05-20 16:53:34 +08:00
{
2021-05-21 22:56:42 +08:00
return $this->redis()->sMembers($this->getCacheKey());
2021-05-20 16:53:34 +08:00
}
/**
* 获取集合中元素个数
*
* @return int
*/
2021-08-28 16:11:38 +08:00
public function count(): int
2021-05-20 16:53:34 +08:00
{
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
*/
2021-08-28 16:11:38 +08:00
public function delete(): bool
2021-05-20 16:53:34 +08:00
{
2021-05-21 22:56:42 +08:00
return (bool)$this->redis()->del($this->getCacheKey());
2021-05-20 16:53:34 +08:00
}
}