StackExchange.Redis幫助類解決方案RedisRepository封裝(基礎配置)

本篇分享有部分瑕疵 請移步修正 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、基礎配置封裝

2、String字符串類型數據操做封裝

3、Hash散列類型數據操做封裝

4、List列表類型數據操做封裝(建議自行封裝)

5、Set集合類型數據操做封裝(建議自行封裝)

6、Sort Set集合數據類型操做封裝(建議自行封裝)

7、發佈訂閱(Pub/Sub)模式在StackExchange.Redis中的使用

8、主從配置,哨兵相關配置

1、基礎配置封裝

 首先咱們要從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
    }
}

 

接下來的幾篇分享,我將持續加入相關操做方法。若是個人點滴分享對您有點低幫助,歡迎點擊下方紅色關注,我將持續分享,共同進步

相關文章
相關標籤/搜索