C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
更新時(shí)間:2024年03月05日 10:20:10 作者:搬磚的詩(shī)人Z
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
1.請(qǐng)先安裝CSRedisCore

接口:
namespace Tools.Redis
{
public interface IRedisTool
{
bool SetLongValue(string key, string value);
bool SetValue(string key, string value, int outSecond);
bool SetValue(string key, string value);
bool Exists(string key);
bool UpdateValue(string key, string value);
string? GetValue(string key);
T? GetValue<T>(string key);
T? GetEntity<T>(string key);
List<T>? GetLike<T>(string key);
void DeleteKey(string key);
void DeleteLike(string key);
}
}
實(shí)現(xiàn)接口方法
using CSRedis;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.Redis
{
public class RedisTool : IRedisTool
{
CSRedisClient csRedis;
public RedisTool(string redisConfig)
{
csRedis = new CSRedisClient(redisConfig);
RedisHelper.Initialization(csRedis);
}
/// <summary>
/// 設(shè)置長(zhǎng)時(shí)間存在的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetLongValue(string key, string value)
{
try
{
csRedis.Set(key, value);
return true;
}
catch (Exception ex)
{
NLogHelper.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 設(shè)置值,并設(shè)置清除時(shí)間
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="outSecond"></param>
/// <returns></returns>
public bool SetValue(string key, string value, int outSecond)
{
try
{
csRedis.Set(key, value, outSecond);
return true;
}
catch (Exception ex)
{
NLogHelper.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 設(shè)置值,存在則覆蓋,并沿用之前的清除時(shí)間
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetValue(string key, string value)
{
try
{
if (csRedis.Exists(key))
{
long time = csRedis.Ttl(key);
csRedis.Set(key, value, Convert.ToInt32(time));
}
else
csRedis.Set(key, value);
return true;
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
/// <summary>
/// 是否存在key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
{
try
{
return csRedis.Exists(key);
}
catch (Exception ex)
{
NLogHelper.Error("RedisDataHelper-KeyExists" + ex.Message);
return false;
}
}
/// <summary>
/// 更新Key,把自動(dòng)注銷時(shí)間設(shè)置為原來(lái)的key的時(shí)間,不存在返回false
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool UpdateValue(string key, string value)
{
try
{
if (csRedis.Exists(key))
{
long time = csRedis.Ttl(key);
csRedis.Set(key, value, Convert.ToInt32(time));
return true;
}
return false;
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
public string? GetValue(string key)
{
try
{
return csRedis.Get(key);
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-GetValue[{key}]" + ex.Message);
return null;
}
}
/// <summary>
/// 獲得json序列化后的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T? GetValue<T>(string key)
{
try
{
var data = csRedis.Get(key);
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-GetValue[{key}]" + ex.Message);
return default;
}
}
public T? GetEntity<T>(string key)
{
try
{
var data = csRedis.Get(key);
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public List<T>? GetLike<T>(string key)
{
try
{
var dataList = csRedis.Keys(key + "*");
List<T> list = new List<T>();
foreach (string item in dataList)
{
var data = GetEntity<T>(item);
if (data != null)
{
list.Add(data);
}
}
return list;
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public void DeleteKey(string key)
{
try
{
csRedis.Del(key);
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-DeleteKey[{key}]" + ex.Message);
}
}
public void DeleteLike(string key)
{
try
{
var dataList = csRedis.Keys(key + "*");
foreach (string item in dataList)
{
DeleteKey(item);
}
}
catch (Exception ex)
{
NLogHelper.Error($"RedisDataHelper-DeleteLike[{key}]" + ex.Message);
}
}
private bool AcquireLock(string lockKey, string lockValue, int lockTimeoutSeconds)
{
// 嘗試獲取鎖
bool lockAcquired = csRedis.SetNx(lockKey, lockValue);
// 如果成功獲取鎖,設(shè)置鎖的超時(shí)時(shí)間
if (lockAcquired)
{
csRedis.Expire(lockKey, lockTimeoutSeconds);
}
return lockAcquired;
}
private void ReleaseLock(string lockKey, string lockValue)
{
// 釋放鎖
// 使用 Lua 腳本確保只有持有鎖的客戶端才能釋放鎖
string luaScript = @"
if redis.call('get', KEYS[1]) == ARGV[1] then
return redis.call('del', KEYS[1])
else
return 0
end";
csRedis.Eval(luaScript, lockKey, new[] { lockValue });
}
}
}
以上就是C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#封裝常用Redis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# HttpClient上傳文件并附帶其它參數(shù)方式
這篇文章主要介紹了C# HttpClient上傳文件并附帶其它參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
WPF使用DrawingContext實(shí)現(xiàn)繪制刻度條
這篇文章主要為大家詳細(xì)介紹了如何利用WPF DrawingContext實(shí)現(xiàn)繪制刻度條,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09
舉例講解C#中自動(dòng)實(shí)現(xiàn)的屬性
這篇文章主要介紹了C#中自動(dòng)實(shí)現(xiàn)的屬性,包括使用自動(dòng)實(shí)現(xiàn)的屬性實(shí)現(xiàn)輕量類的方法,需要的朋友可以參考下2016-01-01
c# 數(shù)據(jù)庫(kù)的 sql 參數(shù)封裝類的編寫
c# 數(shù)據(jù)庫(kù)的 sql 參數(shù)封裝類的編寫...2007-12-12

