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

80 lines
1.4 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\ListRedisInterface;
/**
* Redis List
*
* @package App\Cache\Repository
*/
2021-05-21 22:56:42 +08:00
class ListRedis extends AbstractRedis implements ListRedisInterface
2021-05-20 16:53:34 +08:00
{
2021-05-21 22:56:42 +08:00
protected $prefix = 'rds-list';
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
/**
* Push 队列任务
*
* @param string ...$value
* @return false|int
*/
public function push(string ...$value)
{
2021-05-21 22:56:42 +08:00
return $this->redis()->lPush($this->getCacheKey(), ...$value);
2021-05-20 16:53:34 +08:00
}
/**
* 获取队列中的任务
*
* @return bool|mixed
*/
public function pop()
{
2021-05-21 22:56:42 +08:00
return $this->redis()->rPop($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 (int)$this->redis()->lLen($this->getCacheKey());
2021-05-20 16:53:34 +08:00
}
/**
* 清除列表中所有元素
*
* @return boolean
*/
2021-08-28 16:11:38 +08:00
public function clear(): bool
2021-05-20 16:53:34 +08:00
{
2021-05-21 22:56:42 +08:00
return $this->redis()->lTrim($this->getCacheKey(), 1, 0);
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()->lRange($this->getCacheKey(), 0, -1);
2021-05-20 16:53:34 +08:00
}
/**
* 删除 List
*
* @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
}
}