using System; using System.Configuration; using Newtonsoft.Json; using StackExchange.Redis; namespace BLL { /// /// /// public class Redis : ICache { int Default_Timeout = 600;//默认超时时间(单位秒) string address; JsonSerializerSettings jsonConfig = new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore }; ConnectionMultiplexer connectionMultiplexer; IDatabase database; class CacheObject { public int ExpireTime { get; set; } public bool ForceOutofDate { get; set; } public T Value { get; set; } } /// /// /// /// public Redis(int dbbase = 0) { this.address = this.address = ConfigurationManager.AppSettings["RedisHosts"]; if (this.address == null || string.IsNullOrWhiteSpace(this.address.ToString())) throw new ApplicationException("配置文件中未找到RedisServer的有效配置"); connectionMultiplexer = ConnectionMultiplexer.Connect(address); database = connectionMultiplexer.GetDatabase(dbbase); } /// /// 连接超时设置 /// public int TimeOut { get { return Default_Timeout; } set { Default_Timeout = value; } } /// /// /// /// /// public object Get(string key) { return Get(key); } /// /// /// /// /// /// public T Get(string key) { DateTime begin = DateTime.Now; var cacheValue = database.StringGet(key); DateTime endCache = DateTime.Now; var value = default(T); if (!cacheValue.IsNull) { var cacheObject = JsonConvert.DeserializeObject>(cacheValue, jsonConfig); if (!cacheObject.ForceOutofDate) database.KeyExpire(key, new TimeSpan(0, 0, cacheObject.ExpireTime)); value = cacheObject.Value; } DateTime endJson = DateTime.Now; return value; } /// /// /// /// /// public void Insert(string key, object data) { var jsonData = GetJsonData(data, TimeOut, false); database.StringSet(key, jsonData); } /// /// /// /// /// /// public void Insert(string key, object data, int cacheTime) { var timeSpan = TimeSpan.FromSeconds(cacheTime); var jsonData = GetJsonData(data, TimeOut, true); database.StringSet(key, jsonData, timeSpan); } /// /// /// /// /// /// public void Insert(string key, object data, DateTime cacheTime) { var timeSpan = cacheTime - DateTime.Now; var jsonData = GetJsonData(data, TimeOut, true); database.StringSet(key, jsonData, timeSpan); } /// /// /// /// /// /// public void Insert(string key, T data) { var jsonData = GetJsonData(data, TimeOut, false); database.StringSet(key, jsonData); } /// /// /// /// /// /// /// public void Insert(string key, T data, int cacheTime) { var timeSpan = TimeSpan.FromSeconds(cacheTime); var jsonData = GetJsonData(data, TimeOut, true); database.StringSet(key, jsonData, timeSpan); } /// /// /// /// /// /// /// public void Insert(string key, T data, DateTime cacheTime) { var timeSpan = cacheTime - DateTime.Now; var jsonData = GetJsonData(data, TimeOut, true); database.StringSet(key, jsonData, timeSpan); } /// /// /// /// /// /// /// string GetJsonData(object data, int cacheTime, bool forceOutOfDate) { var cacheObject = new CacheObject() { Value = data, ExpireTime = cacheTime, ForceOutofDate = forceOutOfDate }; return JsonConvert.SerializeObject(cacheObject, jsonConfig);//序列化对象 } /// /// /// /// /// /// /// /// string GetJsonData(T data, int cacheTime, bool forceOutOfDate) { var cacheObject = new CacheObject() { Value = data, ExpireTime = cacheTime, ForceOutofDate = forceOutOfDate }; return JsonConvert.SerializeObject(cacheObject, jsonConfig);//序列化对象 } /// /// 删除 /// /// public void Remove(string key) { database.KeyDelete(key, CommandFlags.HighPriority); } /// /// 判断key是否存在 /// public bool Exists(string key) { return database.KeyExists(key); } /// /// 右侧入队 /// /// 队列名称 /// 值 /// public long EnqueueListRightPush(RedisKey queueName, RedisValue redisValue) { return database.ListRightPush(queueName, redisValue); } /// /// 左侧入队 /// /// 队列名称 /// 队列值 /// public long EnqueueListLeftPush(RedisKey queueName, RedisValue redisvalue) { return database.ListLeftPush(queueName, redisvalue); } /// /// 获取队列长度 /// /// 队列名称 /// public long EnqueueListLength(RedisKey queueName) { if (database.KeyExists(queueName)) { return database.ListLength(queueName); } else { return 0; } } /// /// 左侧出队 /// /// /// public string DequeueListPopLeft(RedisKey queueName) { int count = database.ListRange(queueName).Length; if (count > 0) { string redisValue = database.ListLeftPop(queueName); if (!string.IsNullOrEmpty(redisValue)) return redisValue; else return string.Empty; } else { return "-1"; throw new Exception($"队列{queueName}数据为零"); } } /// /// 右侧出队 /// public string DequeueListPopRight(RedisKey queueName) { int count = database.ListRange(queueName).Length; if (count > 0) { string redisValue = database.ListRightPop(queueName); if (!string.IsNullOrEmpty(redisValue)) return redisValue; else return string.Empty; } else { return "-1"; throw new Exception($"队列{queueName}数据为零"); } } /// /// 分布式加锁 /// /// 键 /// 值 /// 过期时间 /// public bool LockTake(string key, string data, TimeSpan seconds, int db = 0) { return database.LockTake(key, data, seconds); } /// /// 解锁 /// /// 键 /// 值 /// public bool LockRelease(string key, string data, int db = -1) { return database.LockRelease(key, data); } } }