242 lines
7.0 KiB
C#
242 lines
7.0 KiB
C#
using CSRedis;
|
||
using StackExchange.Redis;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Text;
|
||
|
||
namespace Common
|
||
{
|
||
/// <summary>
|
||
/// Redis缓存辅助类
|
||
/// </summary>
|
||
public class CSRedisCacheHelper
|
||
{
|
||
public static CSRedisClient redis;
|
||
//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()
|
||
{
|
||
string redisHostStr = "47.119.147.104:26379";
|
||
if (!string.IsNullOrEmpty(redisHostStr))
|
||
{
|
||
redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 添加缓存
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="key"></param>
|
||
/// <param name="value"></param>
|
||
public static void Set<T>(string key, T value)
|
||
{
|
||
redis.Set(key, value, SessionExpireMinutes * 60);
|
||
}
|
||
|
||
public static T Get<T>(string key)
|
||
{
|
||
return redis.Get<T>(key);
|
||
}
|
||
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);
|
||
}
|
||
|
||
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)
|
||
{
|
||
bool result = redis.Exists(key);
|
||
return result;
|
||
}
|
||
|
||
public static void Publish(string Topic, string Payload)
|
||
{
|
||
CSRedisCacheHelper.redis.Publish(Topic, Payload);
|
||
}
|
||
/// <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);
|
||
}
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|