星空网 > 软件开发 > ASP.net

StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

本文版权归博客园和作者吴双本人共同所有,转载和爬虫,请注明原文地址。http://www.cnblogs.com/tdws/p/5815735.html

写在前面

这不是教程,分享而已,也欢迎园友们多提建议和指正。关于更多详细介绍,请到github上看Docs,下面附上地址。

关于Redis基础控制它台操作有疑问的,欢迎阅读本人Redis系列命令拾遗分享  http://www.cnblogs.com/tdws/tag/NoSql/

如今StackService.Redis已经转向商业版本。4.0以下的低版本依然免费和开源,低版本的不更新了,有没有bug谁知道呢?

但是我们依然有一个非常棒的选择,StackExchange.Redis。我给你一个使用它的理由,StackOverflow在使用它,我想其他的不说,这个理由足够了。

我要做的事情是什么,我为什么要做这件事情呢?

相信在平时工作中,我们使用redis大多是调用SOA接口,架构师或者缓存中心封装出dll给我们使用,然后你看不到源码,这很不爽啊!首先我把写博客当成另一种事业,所以我要做的就是分享封装Redis帮助类的方法以及过程,希望能帮助到自己和热爱技术的朋友们。

StackExchange在github上文档的地址:https://github.com/StackExchange/StackExchange.Redis/tree/master/Docs

目录

本系列会包括如下内容,打算15天内更完,15天速度正好,我也加强了,相信大家也掌握了:

一、基础配置封装

二、String字符串类型数据操作封装

三、Hash散列类型数据操作封装

四、List列表类型数据操作封装

五、Set集合类型数据操作封装

六、Sort Set集合数据类型操作封装

七、主从配置,哨兵相关配置

一、基础配置封装

 首先我们要从nuget中引用StackExchange.Redis到解决方案中的项目。

StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

项目目录结构如下:

StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

首先给大家看下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#endregionusing 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  }}

 

接下来的几篇分享,我将持续加入相关操作方法。如果我的点滴分享,对您能有一点帮助,欢迎点赞支持。

 




原标题:StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

关键词:Redis

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

2020最火的是什么?浅谈跨境电商的红人营销之路:https://www.ikjzd.com/articles/125762
限时红利!Wish A+物流计划订单部分可享受延迟到仓免责支持:https://www.ikjzd.com/articles/125788
关于日本海关逆算计税法的一些了解:https://www.ikjzd.com/articles/125790
关于易得客多店铺IP管理的价值及安全防护机制!:https://www.ikjzd.com/articles/125792
亚马逊热销品存在严重火灾隐患,已遭严查禁售!:https://www.ikjzd.com/articles/125793
亚马逊卖什么好,如何做选品???:https://www.ikjzd.com/articles/125795
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流