1. 程式人生 > >ASP.NET結合Redis實現分散式快取

ASP.NET結合Redis實現分散式快取

 

最近一個專案ASP.NET+MySQL

有的網頁開啟初始化的查詢需要10秒甚至更久,使用者體驗極差,而且併發量變大的時候網站容易崩潰

後來想了兩種解決方案都不是太滿意

1、資料庫裡建一張快取表,後臺作業定時去更新這張表,每次網頁開啟就可以直接從快取表裡查詢

2、使用者第一次開啟網站將資料已檔案的形式快取到伺服器上,下次直接從檔案中讀取資料

最後決定用Redis分散式快取實現

Redis是在Linux系統上安裝的,不過也有Windows版本的安裝包,我是將它設定成了服務

下載地址 https://github.com/MicrosoftArchive/redis/releases

解壓後目錄結構如下

以管理員方式開啟命令列,跳轉到Redis解壓的目錄下,執行命令 redis-server --service-install redis.windows.conf

會提示服務安裝成功

開啟計算機管理->服務,已經可以看到Redis服務了,將它設定為開機自動執行

接下來,要使用ServiceStack.Redis在C#中操作Redis

可以通過Nuget安裝,也可以到網上下載這個類庫

最新版本的版本(好像是5.0)是收費的,免費版一個小時之內有快取6000次的限制。

如果需要破解版可以聯絡我QQ 22378930

當然也可以使用比較舊的ServiceStack版本,比如3.0

首先新增ServiceStack的引用

ServiceStack.Common.dll

ServiceStack.Interfaces.dll

ServiceStack.Redis.dll

ServiceStack.Text.dll

新增引用後,在web.config中新增一些配置

 <appSettings>
    <add key="WriteServerList" value="127.0.0.1:6379" />
    <add key="ReadServerList" value="127.0.0.1:6379" />
    <
add key="MaxWritePoolSize" value="60" /> <add key="MaxReadPoolSize" value="60" /> <add key="AutoStart" value="true" /> <add key="LocalCacheTime" value="1800" /> <add key="RecordeLog" value="false" /> </appSettings>

配置檔案操作類

 public class RedisConfigInfo
    {
        public static string WriteServerList = ConfigurationManager.AppSettings["WriteServerList"];
        public static string ReadServerList = ConfigurationManager.AppSettings["ReadServerList"];
        public static int MaxWritePoolSize = Convert.ToInt32(ConfigurationManager.AppSettings["MaxWritePoolSize"]);
        public static int MaxReadPoolSize = Convert.ToInt32(ConfigurationManager.AppSettings["MaxReadPoolSize"]);
        public static int LocalCacheTime = Convert.ToInt32(ConfigurationManager.AppSettings["LocalCacheTime"]);
        public static bool AutoStart = ConfigurationManager.AppSettings["AutoStart"].Equals("true") ? true : false;
    }

連線Redis,以及其他的一些操作類

public class RedisManager
    {
        private static PooledRedisClientManager prcm;

        /// <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();
        }
        /// <summary>
        /// 快取預設24小時過期
        /// </summary>
        public static TimeSpan expiresIn = TimeSpan.FromHours(24);
        /// <summary>
        /// 設定一個鍵值對,預設24小時過期
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="redisClient"></param>
        /// <returns></returns>
        public static bool Set<T>(string key, T value, IRedisClient redisClient)
        {

            return redisClient.Set<T>(key, value, expiresIn);
        }

        /// <summary>
        /// 將某類資料插入到list中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">一般是BiaoDiGuid</param>
        /// <param name="item"></param>
        /// <param name="redisClient"></param>
        public static void Add2List<T>(string key, T item, IRedisClient redisClient)
        {
            var redis = redisClient.As<T>();
            var list = redis.Lists[GetListKey(key)];
            list.Add(item);
        }

        /// <summary>
        /// 獲取一個list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="redisClient"></param>
        /// <returns></returns>
        public static IRedisList<T> GetList<T>(string key, IRedisClient redisClient)
        {
            var redis = redisClient.As<T>();
            return redis.Lists[GetListKey(key)];
        }

        public static string GetListKey(string key, string prefix = null)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                return "urn:" + key;
            }
            else
            {
                return "urn:" + prefix + ":" + key;
            }
        }
    }

下面我們來測試一下是否能夠成功讀取Redis快取

 <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lbtest"></asp:Label>
        <asp:Button runat="server" ID ="btn1" OnClick="btn1_Click" Text="獲取測試資料"/>
    </div>
    </form>
 protected void btn1_Click(object sender, EventArgs e)
        {
            string UserName;
            //讀取資料,如果快取存在直接從快取中讀取,否則從資料庫讀取然後寫入redis
            using (var redisClient = RedisManager.GetClient())
            {
                UserName = redisClient.Get<string>("UserInfo_123");
                if (string.IsNullOrEmpty(UserName)) //初始化快取
                {
                    //TODO 從資料庫中獲取資料,並寫入快取
                    UserName = "張三";
                    redisClient.Set<string>("UserInfo_123", UserName, DateTime.Now.AddSeconds(10));
                    lbtest.Text = "資料庫資料:" + "張三";
                    return;
                }
                lbtest.Text = "Redis快取資料:" + UserName;
            }
        }

首次訪問快取中資料不存在,獲取資料並寫入快取,並設定有效期10秒

10秒內再次訪問讀取快取中資料