84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
|
|
using CSRedis;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Configuration;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using static CSRedis.CSRedisClient;
|
|||
|
|
|
|||
|
|
namespace Common
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Redis缓存辅助类
|
|||
|
|
/// </summary>
|
|||
|
|
public class CSRedisCacheHelper
|
|||
|
|
{
|
|||
|
|
public static CSRedisClient? redis;
|
|||
|
|
public static CSRedisClient? redis1;
|
|||
|
|
|
|||
|
|
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=0");
|
|||
|
|
redis1 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
|||
|
|
var DingYueMsg = ("CallServer", new Action<SubscribeMessageEventArgs>(async (args) =>
|
|||
|
|
{
|
|||
|
|
string body = args.Body;
|
|||
|
|
}));
|
|||
|
|
|
|||
|
|
CSRedisCacheHelper.redis.Subscribe(DingYueMsg);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <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>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
|
|||
|
|
public static bool Contains(string key)
|
|||
|
|
{
|
|||
|
|
bool result = redis.Exists(key);
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|