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

76 lines
1.5 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\StringRedisInterface;
/**
* String Cache
*
* @package App\Cache\Repository
*/
2021-05-21 22:56:42 +08:00
class StringRedis extends AbstractRedis implements StringRedisInterface
2021-05-20 16:53:34 +08:00
{
2021-05-21 22:56:42 +08:00
protected $prefix = 'rds-string';
2021-05-20 16:53:34 +08:00
2021-05-21 22:56:42 +08:00
protected $name = '';
2021-05-20 16:53:34 +08:00
/**
* 设置缓存
*
* @param string $key 缓存标识
* @param string $value 缓存数据
* @param null $expires 过期时间
* @return bool
*/
2021-08-28 16:11:38 +08:00
public function set(string $key, string $value, $expires = null): bool
2021-05-20 16:53:34 +08:00
{
return $this->redis()->set($this->getCacheKey($key), $value, $expires);
}
/**
* 获取缓存数据
*
* @param string $key 缓存标识
* @return false|mixed|string
*/
public function get(string $key)
{
return $this->redis()->get($this->getCacheKey($key));
}
/**
* 删除 String 缓存
*
* @param string $key 缓存标识
* @return bool
*/
2021-08-28 16:11:38 +08:00
public function delete(string $key): bool
2021-05-20 16:53:34 +08:00
{
return (bool)$this->redis()->del($key);
}
/**
* 判断缓存是否存在
*
* @param string $key 缓存标识
* @return bool
*/
2021-08-28 16:11:38 +08:00
public function isExist(string $key): bool
2021-05-20 16:53:34 +08:00
{
return (bool)$this->get($key);
}
/**
* 获取缓存过期时间
*
* @param string $key 缓存标识
* @return bool|int
*/
public function ttl(string $key)
{
return $this->redis()->ttl($this->getCacheKey($key));
}
}