C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
更新時(shí)間:2024年03月05日 10:20:10 作者:搬磚的詩人Z
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
1.請先安裝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è)置長時(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è)置為原來的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的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#基于FTP協(xié)議的簡易軟件自動(dòng)升級程序
這篇文章主要為大家詳細(xì)介紹了C#基于FTP協(xié)議的簡易軟件自動(dòng)升級程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
.NET使用C#設(shè)置Excel單元格數(shù)值格式
這篇文章主要為大家詳細(xì)介紹了如何使用C#在.NET程序中實(shí)現(xiàn)對Excel單元格數(shù)字格式的設(shè)置,幫助實(shí)現(xiàn)更完善的Excel文件處理,感興趣的小伙伴可以了解下2024-12-12
如何使用C#串口通訊實(shí)現(xiàn)數(shù)據(jù)的發(fā)送和接收
本文詳細(xì)介紹了如何使用C#實(shí)現(xiàn)基于串口通訊的數(shù)據(jù)發(fā)送和接收,通過SerialPort類,我們可以輕松實(shí)現(xiàn)串口通訊,并結(jié)合事件機(jī)制實(shí)現(xiàn)數(shù)據(jù)的傳遞和處理,感興趣的朋友一起看看吧2025-03-03
WPF如何利用附加屬性修改ShowGridLines效果詳解
這篇文章主要給大家介紹了關(guān)于WPF如何利用附加屬性修改ShowGridLines效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-04-04

