You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.7 KiB
C#

5 months ago
using CSRedis;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using DS.Module.Core;
5 months ago
using Amazon.Runtime.Internal.Util;
using MySqlConnector.Logging;
5 months ago
namespace DS.Module.RedisModule
{
public class RedisService: IRedisService
{
CSRedisClient csRedis;
5 months ago
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
5 months ago
private readonly string redisConfig;
public RedisService(IServiceProvider serviceProvider)
{
redisConfig = AppSetting.app(new string[] { "RedisInfo", "RedisConfig" });
csRedis = new CSRedisClient(redisConfig);
RedisHelper.Initialization(csRedis);
}
/// <summary>
/// 设置长时间存在的值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetLongValue(string key, string value)
{
try
{
5 months ago
RedisHelper.Set(key, value);
5 months ago
return true;
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 设置值,并设置清除时间
/// </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
{
5 months ago
RedisHelper.Set(key, value, outSecond);
5 months ago
return true;
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-SetValue" + ex.Message);
return false;
}
}
/// <summary>
/// 设置值,存在则覆盖,并沿用之前的清除时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetValue(string key, string value)
{
try
{
5 months ago
if (RedisHelper.Exists(key))
5 months ago
{
5 months ago
long time = RedisHelper.Ttl(key);
RedisHelper.Set(key, value, Convert.ToInt32(time));
5 months ago
}
else
5 months ago
RedisHelper.Set(key, value);
5 months ago
return true;
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
/// <summary>
/// 是否存在key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
{
try
{
5 months ago
return RedisHelper.Exists(key);
5 months ago
}
catch (Exception ex)
{
Logger.Error("RedisDataHelper-KeyExists" + ex.Message);
return false;
}
}
/// <summary>
/// 更新Key把自动注销时间设置为原来的key的时间,不存在返回false
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool UpdateValue(string key, string value)
{
try
{
5 months ago
if (RedisHelper.Exists(key))
5 months ago
{
5 months ago
long time = RedisHelper.Ttl(key);
RedisHelper.Set(key, value, Convert.ToInt32(time));
5 months ago
return true;
}
return false;
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-SetValue[{key}-{value}]" + ex.Message);
return false;
}
}
public string? GetValue(string key)
{
try
{
5 months ago
return RedisHelper.Get(key);
5 months ago
}
catch (Exception ex)
{
Logger.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
{
5 months ago
var data = RedisHelper.Get(key);
5 months ago
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetValue[{key}]" + ex.Message);
return default;
}
}
public T? GetEntity<T>(string key)
{
try
{
5 months ago
var data = RedisHelper.Get(key);
5 months ago
return JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public List<T>? GetLike<T>(string key)
{
try
{
5 months ago
var dataList = RedisHelper.Keys(key + "*");
5 months ago
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)
{
Logger.Error($"RedisDataHelper-GetList[{key}]" + ex.Message);
return default;
}
}
public void DeleteKey(string key)
{
try
{
5 months ago
RedisHelper.Del(key);
5 months ago
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-DeleteKey[{key}]" + ex.Message);
}
}
public void DeleteLike(string key)
{
try
{
5 months ago
var dataList = RedisHelper.Keys(key + "*");
5 months ago
foreach (string item in dataList)
{
DeleteKey(item);
}
}
catch (Exception ex)
{
Logger.Error($"RedisDataHelper-DeleteLike[{key}]" + ex.Message);
}
}
private bool AcquireLock(string lockKey, string lockValue, int lockTimeoutSeconds)
{
// 尝试获取锁
5 months ago
bool lockAcquired = RedisHelper.SetNx(lockKey, lockValue);
5 months ago
// 如果成功获取锁,设置锁的超时时间
if (lockAcquired)
{
5 months ago
RedisHelper.Expire(lockKey, lockTimeoutSeconds);
5 months ago
}
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";
5 months ago
RedisHelper.Eval(luaScript, lockKey, new[] { lockValue });
5 months ago
}
5 months ago
/// <summary>
/// 释放Redis链接
/// </summary>
public void Dispose()
{
csRedis.Dispose();
}
5 months ago
}
}