2025-12-20 15:22:24 +08:00
|
|
|
|
using CSRedis;
|
2025-12-22 17:47:19 +08:00
|
|
|
|
using StackExchange.Redis;
|
2025-12-20 15:22:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis缓存辅助类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class CSRedisCacheHelper
|
|
|
|
|
|
{
|
2025-12-20 15:35:14 +08:00
|
|
|
|
public static CSRedisClient redis;
|
2025-12-22 17:47:19 +08:00
|
|
|
|
//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";
|
2025-12-20 15:22:24 +08:00
|
|
|
|
static CSRedisCacheHelper()
|
|
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
string redisHostStr = "47.119.147.104:26379";
|
2025-12-20 15:22:24 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(redisHostStr))
|
|
|
|
|
|
{
|
2025-12-20 15:35:14 +08:00
|
|
|
|
redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0");
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加缓存
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static void Set<T>(string key, T value)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
redis.Set(key, value, SessionExpireMinutes * 60);
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static T Get<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.Get<T>(key);
|
|
|
|
|
|
}
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static void Del(string[] key)
|
|
|
|
|
|
{
|
|
|
|
|
|
redis.Del(key.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void Del_Partition(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
redis.Del(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static T ForeverGet<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.Get<T>(key);
|
|
|
|
|
|
}
|
2025-12-20 15:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
public static void Forever<T>(string key, T value)
|
2025-12-22 17:47:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
redis.Set(key, value, -1);
|
|
|
|
|
|
}
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static void Del_Partition(string[] key)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
redis.Del(key);
|
|
|
|
|
|
}
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static bool Contains_Partition(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.Exists(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static long GetForever_ExpireMinutes<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.Ttl(key);
|
|
|
|
|
|
}
|
2025-12-20 15:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
/// 获取
|
2025-12-20 15:22:24 +08:00
|
|
|
|
/// </summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2025-12-20 15:22:24 +08:00
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static T Get<T>(string key, string mac)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
T obj = redis.Get<T>(mac);
|
|
|
|
|
|
if (obj == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.Get<T>(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
return obj;
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
/// 判断是否存在
|
2025-12-20 15:22:24 +08:00
|
|
|
|
/// </summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <param name="mac"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool Contains(string key, string mac)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
bool result = redis.Exists(mac);
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = redis.Exists(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static bool Contains(string key)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
bool result = redis.Exists(key);
|
|
|
|
|
|
return result;
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static void Publish(string Topic, string Payload)
|
2025-12-20 15:22:24 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
CSRedisCacheHelper.redis.Publish(Topic, Payload);
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
2025-12-20 15:35:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加Hash缓存
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
public static void HMSet(string key, params object[] value)
|
|
|
|
|
|
{
|
|
|
|
|
|
redis.HMSet(key, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void HMSet(int ExpireTime_M, string key, params object[] value)
|
|
|
|
|
|
{
|
|
|
|
|
|
redis.HMSet(key, value);
|
|
|
|
|
|
redis.Expire(key, new TimeSpan(0, ExpireTime_M, 0));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取Hash缓存
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static T[] HMGet<T>(string key, string KeyV)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.HMGet<T>(key, KeyV);
|
|
|
|
|
|
}
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static Dictionary<string, string> HMGetAll(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return redis.HGetAll(key);
|
|
|
|
|
|
}
|
2025-12-20 15:35:14 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
/// 获取 某个hash值的数量
|
2025-12-20 15:35:14 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public static int GetHMCount(string key)
|
2025-12-20 15:35:14 +08:00
|
|
|
|
{
|
2025-12-22 17:47:19 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-20 15:35:14 +08:00
|
|
|
|
}
|
2025-12-20 15:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|