最近一個項目ASP.NET+MySQLgit
有的網頁打開初始化的查詢須要10秒甚至更久,用戶體驗極差,並且併發量變大的時候網站容易崩潰github
後來想了兩種解決方案都不是太滿意web
一、數據庫裏建一張緩存表,後臺做業定時去更新這張表,每次網頁打開就能夠直接從緩存表裏查詢redis
二、用戶第一次打開網站將數據已文件的形式緩存到服務器上,下次直接從文件中讀取數據數據庫
最後決定用Redis分佈式緩存實現windows
Redis是在Linux系統上安裝的,不過也有Windows版本的安裝包,我是將它設置成了服務緩存
下載地址 https://github.com/MicrosoftArchive/redis/releases服務器
解壓後目錄結構以下併發
以管理員方式打開命令行,跳轉到Redis解壓的目錄下,執行命令 redis-server --service-install redis.windows.confapp
會提示服務安裝成功
打開計算機管理->服務,已經能夠看到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秒內再次訪問讀取緩存中數據