97 lines
2.9 KiB
C#
97 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Configuration;
|
|
using CSRedis;
|
|
|
|
namespace BLWWS_BLL.Common
|
|
{
|
|
/// <summary>
|
|
/// Redis缓存辅助类
|
|
/// </summary>
|
|
public class CSRedisCacheHelper
|
|
{
|
|
public static CSRedisClient redis;
|
|
public static CSRedisClient redis3;
|
|
public static CSRedisClient redis5;
|
|
|
|
private const string ip = "127.0.0.1";
|
|
private const string port = "6379";
|
|
static CSRedisCacheHelper()
|
|
{
|
|
var redisHostStr = string.Format("{0}:{1}", ip, port);
|
|
if (!string.IsNullOrEmpty(redisHostStr))
|
|
{
|
|
redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
|
redis3 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=3");
|
|
//准备存储取电数据
|
|
redis5 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=5");
|
|
//string channel = "__keyevent@1__:expired";
|
|
//var QQQ = new ValueTuple<string, Action<CSRedisClient.SubscribeMessageEventArgs>>(channel, (msg) =>
|
|
// {
|
|
// if (channel.Equals(""))
|
|
// {
|
|
|
|
// }
|
|
// Console.WriteLine(msg.MessageId);
|
|
// Console.WriteLine(msg.Body);
|
|
// Console.WriteLine("11111111");
|
|
// });
|
|
//redis.Subscribe(QQQ);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 添加缓存
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void Set<T>(string key, T value, int ExpireTime)
|
|
{
|
|
redis?.Set(key, value, ExpireTime * 60);
|
|
}
|
|
|
|
public static T Get<T>(string key)
|
|
{
|
|
return redis.Get<T>(key);
|
|
}
|
|
|
|
public static void Forever<T>(string key, T value)
|
|
{
|
|
redis.Set(key, value, -1);
|
|
}
|
|
public static void Del(string key)
|
|
{
|
|
redis.Del(key);
|
|
}
|
|
|
|
/// <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.redis3.Publish(Topic, Payload);
|
|
}
|
|
}
|
|
}
|