redisの実装プロセス

14562 ワード

1 redisのインストールパッケージをダウンロードし、操作に従ってインストールする
2私のパソコンを右クリック→管理→サービス→redisサービスに立って開く
注意:redisサービスがオープンすると、デフォルトのipとポート番号は127.0.0.1:6379です.
3プロジェクトにredisフォルダを追加し、主に3つのクラスをカプセル化します.
RedisConfigInfo:プロファイルの情報を読み込む
RedisManager:redisの管理
RedisOperation:redisの操作、主にキャッシュの書き込みと読み取り
4 webconfigに構成情報を追加する
ノードに
を追加
作用:redisを宣言する
にredisの構成情報を記入する

5実装
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace CacheDemo.Redis
{
    public class RedisConfigInfo : ConfigurationSection
    {
        public static RedisConfigInfo GetConfig()
        {
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
            return section;
        }

        public static RedisConfigInfo GetConfig(string sectionName)
        {
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
            if (section == null)
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            return section;
        }
        /// <summary>
        ///    Redis    
        /// </summary>
        [ConfigurationProperty("WriteServerList", IsRequired = false)]
        public string WriteServerList
        {
            get
            {
                return (string)base["WriteServerList"];
            }
            set
            {
                base["WriteServerList"] = value;
            }
        }


        /// <summary>
        ///    Redis    
        /// </summary>
        [ConfigurationProperty("ReadServerList", IsRequired = false)]
        public string ReadServerList
        {
            get
            {
                return (string)base["ReadServerList"];
            }
            set
            {
                base["ReadServerList"] = value;
            }
        }


        /// <summary>
        ///       
        /// </summary>
        [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxWritePoolSize
        {
            get
            {
                int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
                return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
            }
            set
            {
                base["MaxWritePoolSize"] = value;
            }
        }


        /// <summary>
        ///       
        /// </summary>
        [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxReadPoolSize
        {
            get
            {
                int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
                return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
            }
            set
            {
                base["MaxReadPoolSize"] = value;
            }
        }


        /// <summary>
        ///     
        /// </summary>
        [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
        public bool AutoStart
        {
            get
            {
                return (bool)base["AutoStart"];
            }
            set
            {
                base["AutoStart"] = value;
            }
        }



        /// <summary>
        ///         ,  : 
        /// </summary>
        [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
        public int LocalCacheTime
        {
            get
            {
                return (int)base["LocalCacheTime"];
            }
            set
            {
                base["LocalCacheTime"] = value;
            }
        }


        /// <summary>
        ///       ,        redis        , redis    ,     
        /// </summary>
        [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
        public bool RecordeLog
        {
            get
            {
                return (bool)base["RecordeLog"];
            }
            set
            {
                base["RecordeLog"] = value;
            }
        }
    }
}
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CacheDemo.Redis
{
    public class RedisManager
    {
        /// <summary>
        /// redis      
        /// </summary>
        private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();

        private static PooledRedisClientManager prcm;

        /// <summary>
        ////// </summary>
        static RedisManager()
        {
            CreateManager();
        }


        /// <summary>
        ///          
        /// </summary>
        private static void CreateManager()
        {
            string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
            string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");

            prcm = new PooledRedisClientManager(readServerList, writeServerList,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
                                 MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
                                 AutoStart = redisConfigInfo.AutoStart,
                             });
        }

        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }

        /// <summary>
        ///          
        /// </summary>
        public static IRedisClient GetClient()
        {
            if (prcm == null)
                CreateManager();

            return prcm.GetClient();
        }
    }
}
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CacheDemo.Redis
{
    public static class RedisOperation
    {
        /// <summary>
        ///     
        /// </summary>
        /// <typeparam name="T">       </typeparam>
        /// <param name="obj">     </param>
        /// <param name="second">       </param>
        /// <param name="name">      </param>
        public static void Set<T>(this T obj, int second, string name) where T : class
        {
            using (var redisClient = RedisManager.GetClient())
            {

                var redis = redisClient.Get<T>(name);

                if (redis == null)
                {
                    redisClient.Set(name, obj);
                    //      
                    redisClient.ExpireEntryAt(name, DateTime.Now.AddSeconds(second));
                }
            }
        }
        public static T Get<T>(string name) where T : class 
        {
            using (var redisClient = RedisManager.GetClient())
            {
                return redisClient.Get<T>(name);
            }
        }
    }
}