using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; namespace Nirvana.Common { public static class RedisHelpers { private static object cacheLocker = new object();//缓存锁对象 private static ICache cache = null;//缓存接口 static RedisHelpers() { Load(); } /// /// 加载缓存 /// /// private static void Load() { try { cache = new Cache(); } catch (Exception) { // Log.Error(ex.Message); } } public static ICache GetCache() { return cache; } /// 缓存过期时间 /// public static int TimeOut { get { return cache.TimeOut; } set { lock (cacheLocker) { cache.TimeOut = value; } } } /// /// 获得指定键的缓存值 /// /// 缓存键 /// 缓存值 public static object Get(string key) { if (string.IsNullOrWhiteSpace(key)) return null; return cache.Get(key); } /// /// 获得指定键的缓存值 /// /// 缓存键 /// 缓存值 public static T Get(string key) { return cache.Get(key); } public static T stringGet(string key) { return cache.stringGet(key); } public static string stringGet(string key) { return stringGet(key); } /// /// 将指定键的对象添加到缓存中 /// /// 缓存键 /// 缓存值 public static void Insert(string key, object data) { if (string.IsNullOrWhiteSpace(key) || data == null) return; //lock (cacheLocker) { cache.Insert(key, data); } } /// /// 将指定键的对象添加到缓存中 /// /// 缓存键 /// 缓存值 public static void Insert(string key, T data) { if (string.IsNullOrWhiteSpace(key) || data == null) return; //lock (cacheLocker) { cache.Insert(key, data); } } /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间(秒) public static void Insert(string key, object data, int cacheTime) { if (!string.IsNullOrWhiteSpace(key) && data != null) { //lock (cacheLocker) { cache.Insert(key, data, cacheTime); } } } /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间(秒) public static void Insert(string key, T data, int cacheTime) { if (!string.IsNullOrWhiteSpace(key) && data != null) { //lock (cacheLocker) { cache.Insert(key, data, cacheTime); } } } /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间 public static void Insert(string key, object data, DateTime cacheTime) { if (!string.IsNullOrWhiteSpace(key) && data != null) { //lock (cacheLocker) { cache.Insert(key, data, cacheTime); } } } /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间 public static void Insert(string key, T data, DateTime cacheTime) { if (!string.IsNullOrWhiteSpace(key) && data != null) { //lock (cacheLocker) { cache.Insert(key, data, cacheTime); } } } /// /// 从缓存中移除指定键的缓存值 /// /// 缓存键 public static void Remove(string key) { if (string.IsNullOrWhiteSpace(key)) return; lock (cacheLocker) { cache.Remove(key); } } /// /// 判断key是否存在 /// public static bool Exists(string key) { return cache.Exists(key); } #region Hash操作 public static bool HashExists(string key, string field) { return cache.HashExists(key, field); } public static bool HashDelete(string key, string field) { return cache.HashDelete(key, field); } public static bool HashSet(string key, string field, string value) { return cache.HashSet(key, field, value); } public static void HashSet(string key, IEnumerable hashFields) { cache.HashSet(key, hashFields); } public static object HashGet(string key, string field) { if (string.IsNullOrWhiteSpace(key)) return null; return cache.HashGet(key, field); } public static List HashValues(string key) { List result = new List(); RedisValue[] arr = cache.HashValues(key); foreach (var item in arr) { if (!item.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject(item)); } } return result; } #endregion } }