添加项目文件。
This commit is contained in:
119
QYZH.InteractiveMagazine.Infrastructure/Cache/RedisHelper.cs
Normal file
119
QYZH.InteractiveMagazine.Infrastructure/Cache/RedisHelper.cs
Normal file
@ -0,0 +1,119 @@
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Infrastructure.Cache;
|
||||
|
||||
/// <summary>
|
||||
/// Redis操作封装
|
||||
/// </summary>
|
||||
public static class RedisHelper
|
||||
{
|
||||
private static string? _keyPrefix;
|
||||
|
||||
/// <summary>
|
||||
/// Redis连接实例
|
||||
/// </summary>
|
||||
public static IConnectionMultiplexer? Connection { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设置键前缀
|
||||
/// </summary>
|
||||
/// <param name="prefix">前缀字符串</param>
|
||||
public static void SetKeyPrefix(string prefix)
|
||||
{
|
||||
_keyPrefix = prefix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Redis数据库实例
|
||||
/// </summary>
|
||||
/// <param name="db">数据库索引</param>
|
||||
/// <returns>IDatabase实例</returns>
|
||||
public static IDatabase GetDatabase(int db = -1)
|
||||
{
|
||||
if (Connection == null || !Connection.IsConnected)
|
||||
{
|
||||
throw new InvalidOperationException("Redis连接未初始化或已断开");
|
||||
}
|
||||
|
||||
return Connection.GetDatabase(db);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置字符串值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
/// <param name="expiry">过期时间</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static async Task<bool> StringSetAsync(string key, string value, TimeSpan? expiry = null)
|
||||
{
|
||||
var db = GetDatabase();
|
||||
var prefixedKey = GetPrefixedKey(key);
|
||||
|
||||
if (expiry.HasValue)
|
||||
{
|
||||
return await db.StringSetAsync(prefixedKey, value, (Expiration)expiry.Value);
|
||||
}
|
||||
|
||||
return await db.StringSetAsync(prefixedKey, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字符串值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns>值</returns>
|
||||
public static async Task<string?> StringGetAsync(string key)
|
||||
{
|
||||
var db = GetDatabase();
|
||||
var prefixedKey = GetPrefixedKey(key);
|
||||
return await db.StringGetAsync(prefixedKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除键
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static async Task<bool> KeyDeleteAsync(string key)
|
||||
{
|
||||
var db = GetDatabase();
|
||||
var prefixedKey = GetPrefixedKey(key);
|
||||
return await db.KeyDeleteAsync(prefixedKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查键是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns>是否存在</returns>
|
||||
public static async Task<bool> KeyExistsAsync(string key)
|
||||
{
|
||||
var db = GetDatabase();
|
||||
var prefixedKey = GetPrefixedKey(key);
|
||||
return await db.KeyExistsAsync(prefixedKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置键的过期时间
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="expiry">过期时间</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static async Task<bool> KeyExpireAsync(string key, TimeSpan expiry)
|
||||
{
|
||||
var db = GetDatabase();
|
||||
var prefixedKey = GetPrefixedKey(key);
|
||||
return await db.KeyExpireAsync(prefixedKey, expiry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取带前缀的键
|
||||
/// </summary>
|
||||
/// <param name="key">原始键</param>
|
||||
/// <returns>带前缀的键</returns>
|
||||
private static string GetPrefixedKey(string key)
|
||||
{
|
||||
return string.IsNullOrEmpty(_keyPrefix) ? key : $"{_keyPrefix}:{key}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user