本篇分享有部分瑕疵 请移步修正 http://www.cnblogs.com/tdws/p/6341494.htmlhtml
本文版权归博客园和做者吴双本人共同全部,转载和爬虫,请注明原文地址。http://www.cnblogs.com/tdws/p/5815735.htmlgit
这不是教程,分享而已,也欢迎园友们多提建议和指正。关于更多详细介绍,请到github上看Docs,下面附上地址。github
关于Redis基础控制它台操做有疑问的,欢迎阅读Redis系列命令拾遗分享 http://www.cnblogs.com/tdws/tag/NoSql/web
现在StackService.Redis已经转向商业版本。4.0如下的低版本依然免费和开源,低版本的不更新了,有没有bug谁知道呢?redis
可是咱们依然有一个很是棒的选择,StackExchange.Redis。我给你一个使用它的理由,StackOverflow在使用它,我想其余的不说,这个理由足够了。数据库
我要作的事情是什么,我为何要作这件事情呢?缓存
相信在平时工做中,咱们使用redis大可能是调用SOA接口,架构师或者缓存中心封装出dll给咱们使用,而后你看不到源码,这很不爽啊!首先我把写博客当成另外一种事业,因此我要作的就是分享封装Redis帮助类的方法以及过程,但愿能帮助到本身和热爱技术的朋友们。架构
StackExchange在github上文档的地址:https://github.com/StackExchange/StackExchange.Redis/tree/master/Docsapp
增强篇 http://www.cnblogs.com/tdws/p/6341494.html异步
本系列会包括以下内容,相信你们也掌握了:
1、基础配置封装
4、List列表类型数据操做封装(建议自行封装)
5、Set集合类型数据操做封装(建议自行封装)
6、Sort Set集合数据类型操做封装(建议自行封装)
7、发布订阅(Pub/Sub)模式在StackExchange.Redis中的使用
8、主从配置,哨兵相关配置
首先咱们要从nuget中引用StackExchange.Redis到解决方案中的项目。
项目目录结构以下:
首先给你们看下RedisClientConfiguration.cs的代码。在这里咱们定义了Redis连接地址,关于Get方法咱们接下来再看。还定义了Port端口,连接超时时间,重试次数,Redis默认使用的数据库0-15,十六个。PreserveAsyncOrder用于配置异步操做是否应以保证其原始交付顺序的方式调用。
using RedisRepository.Helpers; namespace RedisRepository { public static class RedisClientConfigurations { private static string _url = ConfigurationHelper.Get("RedisServer", "127.0.0.1"); public static string Url { get { return _url; } set { _url = value; } } private static int _port = 6379; public static int Port { get { return _port; } set { _port = value; } } private static int _connectTimeout = 10000; public static int ConnectTimeout { get { return _connectTimeout; } set { _connectTimeout = value; } } private static int _connectRetry = 3; public static int ConnectRetry { get { return _connectRetry; } set { _connectRetry = value; } } private static int _defaultDatabase = ConfigurationHelper.Get("RedisDataBase", 0); public static int DefaultDatabase { get { return _defaultDatabase; } set { _defaultDatabase = value; } } private static bool _preserveAsyncOrder = false; public static bool PreserveAsyncOrder { get { return _preserveAsyncOrder; } set { _preserveAsyncOrder = value; } } } }
下面介绍ConfigurationHelper.cs中的Get方法。这就是获取咱们WebConfig配置文件中Redis地址设置,而且必须指定默认地址。
using System; using System.Configuration; namespace RedisRepository.Helpers { public static class ConfigurationHelper { internal static T Get<T>(string appSettingsKey, T defaultValue) { string text = ConfigurationManager.AppSettings[appSettingsKey]; if (string.IsNullOrWhiteSpace(text)) return defaultValue; try { var value = Convert.ChangeType(text, typeof(T)); return (T)value; } catch { return defaultValue; } } } }
另外就到了咱们的关键部分,定义Redis操做类接口IRedisClient.cs以及其实现类RedisClient.cs。接口未来暴露给外部调用者。
#region 程序集 RedisRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // Author:吴双 2016.8.28 联系邮箱wscoder@outlook.com #endregion using System; using System.Collections.Generic; using StackExchange.Redis; namespace RedisRepository { public interface IRedisClient { } }
using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using StackExchange.Redis; namespace RedisRepository { public class RedisClient : IRedisClient { #region 私有公用方法 在其中咱们序列化操做使用Newtonsoft.Json组件 private string SerializeContent(object value) { return JsonConvert.SerializeObject(value); } private T DeserializeContent<T>(RedisValue myString) { return JsonConvert.DeserializeObject<T>(myString); } #endregion } }
接下来的几篇分享,我将持续加入相关操做方法。若是个人点滴分享对您有点低帮助,欢迎点击下方红色关注,我将持续分享,共同进步