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。我給你一個使用它的理由,StackOverflow在使用它,我想其他的不說,這個理由足夠了。

我要做的事情是什么,我為什么要做這件事情呢?

相信在平時工作中,我們使用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到解決方案中的項目。

項目目錄結構如下:

首先給大家看下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
    }
}

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM