using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyCode.Project.Infrastructure.Common; using MyCode.Project.Infrastructure.Extensions; namespace MyCode.Project.Infrastructure.Cache { public class RedisCache : IMyCodeCacheService { private static ConnectionMultiplexer _connectionMultiplexer; private static IDatabase _database; /// /// Redis缓存前缀 /// private static string _prefix = ""; public RedisCache(string address, string prefix) { if (_connectionMultiplexer == null) { _connectionMultiplexer = ConnectionMultiplexer.Connect(address); _database = _connectionMultiplexer.GetDatabase(0); _prefix = prefix; } } #region GetCacheKey(得到带前缀的缓存key) private string GetCacheKey(string key) { return _prefix + key; } #endregion #region SetInt(设置一个整型缓存) /// /// 设置一个整型缓存 /// /// /// public void SetIncr(string key, int value) { _database.StringSet(GetCacheKey(key), value); } #endregion #region Incr(自增1,返回自增后的值) /// /// 自增1,返回自增后的值 /// public long Incr(string key) { var result= _database.StringIncrement(GetCacheKey(key)); _database.KeyExpire(GetCacheKey(key),new TimeSpan(1,0,0,0)); return result; } #endregion #region KeyExpire(设置缓存有效时长) /// /// 设置缓存有效时长 /// /// /// public void KeyExpire(string key ,TimeSpan? expiry) { if (expiry.HasValue) { _database.KeyExpire(GetCacheKey(key), expiry); } } #endregion #region Decr(递减1,返回递减后的值) /// /// 递减1,返回递减后的值 /// /// /// public long Decr(string key) { return _database.StringDecrement(GetCacheKey(key)); } #endregion #region Get(根据key得到对象) public object Get(string key) { var cacheValue = _database.StringGet(GetCacheKey(key)); if (cacheValue.HasValue) { var value = JsonHelper.ToObject(cacheValue); if (value != null) return value; else return cacheValue; } return null; } #endregion #region Get(得到缓存key的值) public T Get(string key) { var value = Get(key); if (value == null) { return default(T); } return (T)value; } #endregion #region Set(设置缓存) public void Set(string key, object data, TimeSpan? cacheTime) { if (cacheTime == null || cacheTime.Value == TimeSpan.Zero) { cacheTime = new TimeSpan(0, 0, 30); } if (data == null) { return; } try { _database.StringSet(GetCacheKey(key), JsonHelper.ObjectToByte(data), cacheTime); } catch(Exception ex) { throw new Exception(ex.Message); } } #endregion #region Remove(按key删除) /// /// 删除 /// /// public void Delete(string key) { _database.KeyDelete(GetCacheKey(key), CommandFlags.HighPriority); } #endregion #region Exists(判断key是否存在) /// /// 判断key是否存在 /// public bool Exists(string key) { return _database.KeyExists(GetCacheKey(key)); } #endregion #region Push(放入队列) public void Push(string key, object data, TimeSpan? timeSpan = null) { //,TimeSpan? timeSpan = null _database.ListRightPush(GetCacheKey(key), JsonHelper.ObjectToByte(data)); if(timeSpan!=null) _database.KeyExpire(GetCacheKey(key), timeSpan); } #endregion #region Push(放入队列) public void Push(string key, object data ) { //,TimeSpan? timeSpan = null _database.ListRightPush(GetCacheKey(key), JsonHelper.ObjectToByte(data)); } #endregion #region Popup(获取第一条数据,并删除) public object Popup(string key) { var cacheValue = _database.ListLeftPop(GetCacheKey(key)); if (cacheValue.HasValue) { try { var value = JsonHelper.ToObject(cacheValue); return value; } catch (Exception ex) { return cacheValue; } } return null; } #endregion } }