公司一直在用.net自带的缓存,大家都知道.net自带缓存的缺点,就不多说了,不知道的可以查一查,领导最近在说分布式缓存,我们选的是redis,领导让我不忙的时候封装一下,搜索了两天,选了选第三方的插件,ServiceStack.Redis和StackExchange.Redis以前都用过,不过都是别人封装好的,今天在往项目中整合的时候,我使用的ServiceStack.Redis版本不兼容.net 4.0,在网上找了一圈,ServiceStack.Redis对.net 4.0的支持用起来很麻烦,要改变系统引用的东西,最后放弃了,今天又拿NServiceKit.Redis重新封装了一下,下一篇在说NServiceKit.Redis。先说说ServiceStack.Redis,ServiceStack.Redis是Redis官方推荐的C#客户端,性能非常优越,使用也很方便,但是从v4版本已经逐渐商业化了,普通版每小时只能访问Redis 6000次,我没测试过,大家可以看看这篇文章http://blog.csdn.net/u010533180/article/details/52856586,所以我放弃了ServiceStack.Redis,选择了StackExchange.Redis,StackExchange.Redis也是开源的,这是地址:https://github.com/StackExchange/StackExchange.Redis,我使用的是.net 4.5,工具采用 vs2015, StackExchange.Redis版本是1.0.488。
对于与Redis连接的配置写法很简单,最简单的写法写个IP和端口就可以了,如你有其他想法自己去查资料吧。StackExchange.Redis中的核心是ConnectionMultiplexer,没事好研究研究。这里我只封装了对缓存的操作,以后可能会更新其他的。
先说说我的想法:
1.定义ICache接口。
2.Redis类和Memcached类,以后想用那个缓存修改一下配置就可以了,非常方便。
3.CacheHelper的实现。
上代码:
ICache.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common { public interface ICache { ////// 缓存过期时间 /// int TimeOut { set; get; } /// /// 获得指定键的缓存值 /// /// 缓存键 /// 缓存值 object Get(string key); /// /// 获得指定键的缓存值 /// T Get (string key); /// /// 从缓存中移除指定键的缓存值 /// /// 缓存键 void Remove(string key); /// /// 清空所有缓存对象 /// //void Clear(); /// /// 将指定键的对象添加到缓存中 /// /// 缓存键 /// 缓存值 void Insert(string key, object data); /// /// 将指定键的对象添加到缓存中 /// /// 缓存键 /// 缓存值 void Insert (string key, T data); /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间(秒钟) void Insert(string key, object data, int cacheTime); /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间(秒钟) void Insert (string key, T data, int cacheTime); /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间 void Insert(string key, object data, DateTime cacheTime); /// /// 将指定键的对象添加到缓存中,并指定过期时间 /// /// 缓存键 /// 缓存值 /// 缓存过期时间 void Insert (string key, T data, DateTime cacheTime); /// /// 判断key是否存在 /// bool Exists(string key); } }
Redis.cs
using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common { 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() { this.address = ConfigurationManager.AppSettings["RedisServer"]; if (this.address == null || string.IsNullOrWhiteSpace(this.address.ToString())) throw new ApplicationException("配置文件中未找到RedisServer的有效配置"); connectionMultiplexer = ConnectionMultiplexer.Connect(address); database = connectionMultiplexer.GetDatabase(); } /// /// 连接超时设置 /// public int TimeOut { get { return Default_Timeout; } set { Default_Timeout = value; } } public object Get(string key) { return Get<object>(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<object>() { 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); } } }
CacheHelper.cs
using System; using System.Collections.Generic; namespace Common { ////// 缓存 /// public static class Cache { private static object cacheLocker = new object();//缓存锁对象 private static ICache cache = null;//缓存接口 static Cache() { Load(); } /// /// 加载缓存 /// /// private static void Load() { try { cache = new Redis(); } catch (Exception ex) { //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 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); } } }
代码下载
原文地址:https://www.cnblogs.com/itmu89/p/7118474.html