修改:Redis方法修改。

This commit is contained in:
2025-12-22 17:47:19 +08:00
parent 67f9192182
commit aa0d7043bf
11 changed files with 237 additions and 85 deletions

View File

@@ -1,10 +1,10 @@
using CSRedis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using static CSRedis.CSRedisClient;
namespace Common
{
@@ -14,20 +14,19 @@ namespace Common
public class CSRedisCacheHelper
{
public static CSRedisClient redis;
private const string ip = "localhost";
private const string port = "26379";
//private static readonly string[] redisHosts = null;
private static int SessionExpireMinutes = 1;
private static int MonitorLogExpireMinutes = 1;
//过期时间60个小时
private static int DeviceStatusExpireMinutes = 60;
public static string HeartBeatPrefix = "HeartBeatMonitor";
static CSRedisCacheHelper()
{
var redisHostStr = string.Format("{0}:{1}", ip, port);
string redisHostStr = "47.119.147.104:26379";
if (!string.IsNullOrEmpty(redisHostStr))
{
redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0");
var DingYueMsg = ("CellCorelDRAWUser", new Action<SubscribeMessageEventArgs>(async (args) =>
{
string body = args.Body;
}));
CSRedisCacheHelper.redis.Subscribe(DingYueMsg);
}
}
/// <summary>
@@ -36,30 +35,103 @@ namespace Common
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set<T>(string key, T value, int ExpireTime)
public static void Set<T>(string key, T value)
{
redis?.Set(key, value, ExpireTime * 60);
redis.Set(key, value, SessionExpireMinutes * 60);
}
public static T Get<T>(string key)
{
return redis.Get<T>(key);
}
public static void Forever<T>(string key, T value)
public static void Del(string[] key)
{
redis.Set(key, value, -1);
redis.Del(key.ToArray());
}
public static void Del(string key)
public static void Del_Partition(string key)
{
redis.Del(key);
}
public static T ForeverGet<T>(string key)
{
return redis.Get<T>(key);
}
public static void Forever<T>(string key, T value)
{
redis.Set(key, value, MonitorLogExpireMinutes * 24 * 60 * 60);
}
public static T Get_Partition<T>(string key)
{
return redis.Get<T>(key);
}
public static void Set_Partition<T>(string key, T value)
{
redis.Set(key, value, DeviceStatusExpireMinutes * 60 * 60);
}
/// <summary>
/// 设置过期时间
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="ExpireTime_Minutes"></param>
public static void Set_PartitionWithTime<T>(string key, T value, int ExpireTime_Minutes)
{
redis.Set(key, value, ExpireTime_Minutes * 60);
}
public static void Set_PartitionWithForever<T>(string key, T value)
{
redis.Set(key, value, -1);
}
public static void Del_Partition(string[] key)
{
redis.Del(key);
}
public static bool Contains_Partition(string key)
{
return redis.Exists(key);
}
public static long GetForever_ExpireMinutes<T>(string key)
{
return redis.Ttl(key);
}
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key, string mac)
{
T obj = redis.Get<T>(mac);
if (obj == null)
{
return redis.Get<T>(key);
}
return obj;
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="key"></param>
/// <param name="mac"></param>
/// <returns></returns>
public static bool Contains(string key, string mac)
{
bool result = redis.Exists(mac);
if (!result)
{
result = redis.Exists(key);
}
return result;
}
public static bool Contains(string key)
{
@@ -67,28 +139,10 @@ namespace Common
return result;
}
/// <summary>
/// 发布消息
/// </summary>
/// <param name="Topic"></param>
/// <param name="Payload"></param>
public static void Publish(string Topic, string Payload)
{
CSRedisCacheHelper.redis.PublishNoneMessageId(Topic, Payload);
CSRedisCacheHelper.redis.Publish(Topic, Payload);
}
public static void LPush(string Key, Dictionary<string, string> Value)
{
CSRedisCacheHelper.redis.LPush<Dictionary<string,string>>(Key,Value);
}
public static T BRPop<T>(string Key)
{
return CSRedisCacheHelper.redis.BRPop<T>(3,Key);
}
/// <summary>
/// 添加Hash缓存
/// </summary>
@@ -115,16 +169,73 @@ namespace Common
{
return redis.HMGet<T>(key, KeyV);
}
/// <summary>
/// Example
/// WXFault_酒店编号_房间号
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static Dictionary<string, string> HMGetAll(string key)
{
return redis.HGetAll(key);
}
/// <summary>
/// 获取 某个hash值的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetHMCount(string key)
{
return redis.HGetAll(key).Count;
}
/// <summary>
/// 删除Hash缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static long HDelAll<T>(string key)
{
return redis.HDel(key);
}
public static long HDel(string key, string key1)
{
return redis.HDel(key, key1);
}
public static void ListAdd(string key, params object[] obj)
{
redis.LPush(key, obj);
}
public static int MaxLen = 500000;
public static void StreamAdd(string key, string Value)
{
try
{
// 添加简单消息
redis.XAdd(key, MaxLen, "*", new ValueTuple<string, string>("__data", Value));
}
catch (Exception)
{
}
}
public static void StreamConsume(string key, string group = "UDPData", string consumer = "Crics1")
{
var data = redis.XReadGroup(group, consumer, 100, 10, new ValueTuple<string, string>(key, ">"));
if (data != null)
{
//检查pending表的长度
//消费确认前pending 应该等于2
var pending = redis.XPending(key, group);
foreach (var item in data)
{
var idarray = item.Item2;
foreach (var SerializeNo in idarray)
{
var id1 = SerializeNo.Item1;
redis.XAck(key, group, id1);
redis.XDel(key, id1);
}
}
}
}
}
}