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

时间:2023-03-08 17:54:55
StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

本篇分享有部分瑕疵 请移步修正 http://www.cnblogs.com/tdws/p/6341494.html

本文版权归博客园和作者吴双本人共同所有,转载和爬虫,请注明原文地址。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。我给你一个使用它的理由,*在使用它,我想其他的不说,这个理由足够了。

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

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

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

目录

加强篇 http://www.cnblogs.com/tdws/p/6341494.html

本系列会包括如下内容,相信大家也掌握了:

一、基础配置封装

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

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

四、List列表类型数据操作封装(建议自行封装)

五、Set集合类型数据操作封装(建议自行封装)

六、Sort Set集合数据类型操作封装(建议自行封装)

七、发布订阅(Pub/Sub)模式在StackExchange.Redis中的使用

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

一、基础配置封装

首先我们要从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 = ;
public static int Port
{
get { return _port; }
set { _port = value; }
} private static int _connectTimeout = ;
public static int ConnectTimeout
{
get { return _connectTimeout; }
set { _connectTimeout = value; }
} private static int _connectRetry = ;
public static int ConnectRetry
{
get { return _connectRetry; }
set { _connectRetry = value; }
} private static int _defaultDatabase = ConfigurationHelper.Get("RedisDataBase", );
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
}
}

接下来的几篇分享,我将持续加入相关操作方法。如果我的点滴分享对您有点低帮助,欢迎点击下方红色关注,我将持续分享,共同进步