Files
crm-server/app/util/RedisHelper.php
T
2025-04-02 15:17:13 +08:00

171 lines
4.7 KiB
PHP

<?php
namespace app\util;
use support\Redis;
use support\Log;
class RedisHelper
{
/**
* 存储数据(自动序列化数组)
* @param string $key 键名
* @param mixed $value 值
* @param int|null $ttl 过期时间(秒)
* @return bool
*/
public static function set(string $key, $value, ?int $ttl = null): bool
{
try {
$storageValue = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
if ($ttl !== null) {
return Redis::setex($key, $ttl, $storageValue);
}
return Redis::set($key, $storageValue);
} catch (\Throwable $e) {
Log::error("Redis存储失败: {$e->getMessage()}", ['key' => $key]);
return false;
}
}
/**
* 获取数据(自动反序列化数组)
* @param string $key 键名
* @param mixed $default 默认值
* @return mixed
*/
public static function get(string $key, $default = null)
{
try {
$value = Redis::get($key);
if ($value === false || $value === null) {
return $default;
}
// 尝试JSON解码
$decoded = json_decode($value, true);
return json_last_error() === JSON_ERROR_NONE ? $decoded : $value;
} catch (\Throwable $e) {
Log::error("Redis读取失败: {$e->getMessage()}", ['key' => $key]);
return $default;
}
}
/**
* 删除键
* @param string|array $keys 键名或键名数组
* @return int 删除的键数量
*/
public static function del($keys): int
{
try {
return Redis::del(...(array)$keys);
} catch (\Throwable $e) {
Log::error("Redis删除失败: {$e->getMessage()}", ['keys' => $keys]);
return 0;
}
}
/**
* 检查键是否存在
* @param string $key 键名
* @return bool
*/
public static function exists(string $key): bool
{
try {
return (bool)Redis::exists($key);
} catch (\Throwable $e) {
Log::error("Redis检查存在失败: {$e->getMessage()}", ['key' => $key]);
return false;
}
}
/**
* 设置带过期时间的缓存
* @param string $key 键名
* @param mixed $value 值
* @param int $ttl 过期时间(秒)
* @return bool
*/
public static function setex(string $key, $value, int $ttl): bool
{
return self::set($key, $value, $ttl);
}
/**
* 哈希表设置字段值
* @param string $key 键名
* @param string $field 字段名
* @param mixed $value 值
* @return bool
*/
public static function hSet(string $key, string $field, $value): bool
{
try {
$storageValue = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
return Redis::hSet($key, $field, $storageValue);
} catch (\Throwable $e) {
Log::error("Redis哈希设置失败: {$e->getMessage()}", ['key' => $key, 'field' => $field]);
return false;
}
}
/**
* 哈希表获取字段值
* @param string $key 键名
* @param string $field 字段名
* @param mixed $default 默认值
* @return mixed
*/
public static function hGet(string $key, string $field, $default = null)
{
try {
$value = Redis::hGet($key, $field);
if ($value === false) {
return $default;
}
$decoded = json_decode($value, true);
return json_last_error() === JSON_ERROR_NONE ? $decoded : $value;
} catch (\Throwable $e) {
Log::error("Redis哈希获取失败: {$e->getMessage()}", ['key' => $key, 'field' => $field]);
return $default;
}
}
/**
* 自增计数器
* @param string $key 键名
* @param int $increment 增量
* @return int|false
*/
public static function incr(string $key, int $increment = 1)
{
try {
return $increment === 1
? Redis::incr($key)
: Redis::incrBy($key, $increment);
} catch (\Throwable $e) {
Log::error("Redis自增失败: {$e->getMessage()}", ['key' => $key]);
return false;
}
}
/**
* 获取剩余过期时间
* @param string $key 键名
* @return int 剩余秒数(-2表示键不存在,-1表示没有设置过期时间)
*/
public static function ttl(string $key): int
{
try {
return Redis::ttl($key);
} catch (\Throwable $e) {
Log::error("Redis获取TTL失败: {$e->getMessage()}", ['key' => $key]);
return -2;
}
}
}