using CSRedis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace Common
{
///
/// Redis缓存辅助类
///
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");
}
}
///
/// 添加缓存
///
///
///
///
public static void Set(string key, T value)
{
redis.Set(key, value, SessionExpireMinutes * 60);
}
public static T Get(string key)
{
return redis.Get(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(string key)
{
return redis.Get(key);
}
public static void Forever(string key, T value)
{
redis.Set(key, value, MonitorLogExpireMinutes * 24 * 60 * 60);
}
public static T Get_Partition(string key)
{
return redis.Get(key);
}
public static void Set_Partition(string key, T value)
{
redis.Set(key, value, DeviceStatusExpireMinutes * 60 * 60);
}
///
/// 设置过期时间
///
///
///
///
///
public static void Set_PartitionWithTime(string key, T value, int ExpireTime_Minutes)
{
redis.Set(key, value, ExpireTime_Minutes * 60);
}
public static void Set_PartitionWithForever(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(string key)
{
return redis.Ttl(key);
}
///
/// 获取
///
///
///
///
public static T Get(string key, string mac)
{
T obj = redis.Get(mac);
if (obj == null)
{
return redis.Get(key);
}
return obj;
}
///
/// 判断是否存在
///
///
///
///
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);
}
///
/// 添加Hash缓存
///
///
///
///
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));
}
///
/// 获取Hash缓存
///
///
///
///
public static T[] HMGet(string key, string KeyV)
{
return redis.HMGet(key, KeyV);
}
public static Dictionary HMGetAll(string key)
{
return redis.HGetAll(key);
}
///
/// 获取 某个hash值的数量
///
///
///
public static int GetHMCount(string key)
{
return redis.HGetAll(key).Count;
}
///
/// 删除Hash缓存
///
///
///
///
public static long HDelAll(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("__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(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);
}
}
}
}
}
}