asp.net性能優(yōu)化之使用Redis緩存(入門(mén))
1:使用Redis緩存的優(yōu)化思路
redis的使用場(chǎng)景很多,僅說(shuō)下本人所用的一個(gè)場(chǎng)景:
1.1對(duì)于大量的數(shù)據(jù)讀取,為了緩解數(shù)據(jù)庫(kù)的壓力將一些不經(jīng)常變化的而又讀取頻繁的數(shù)據(jù)存入redis緩存
大致思路如下:執(zhí)行一個(gè)查詢
1.2首先判斷緩存中是否存在,如存在直接從Redis緩存中獲取。
1.3如果Redis緩存中不存在,實(shí)時(shí)讀取數(shù)據(jù)庫(kù)數(shù)據(jù),同時(shí)寫(xiě)入緩存(并設(shè)定緩存失效的時(shí)間)。
1.4缺點(diǎn),如果直接修改了數(shù)據(jù)庫(kù)的數(shù)據(jù)而又沒(méi)有更新緩存,在緩存失效的時(shí)間內(nèi)將導(dǎo)致讀取的Redis緩存是錯(cuò)誤的數(shù)據(jù)。
2:Redis傻瓜式安裝
2.1雙擊執(zhí)行redis-2.4.6-setup-64-bit.exe程序(下載地址:https://github.com/dmajkic/redis/downloads)
2.2可以將此服務(wù)設(shè)置為windows系統(tǒng)服務(wù):

2.3測(cè)試是否安裝成功:
再回到redis文件夾下,找到redis-cli.exe文件,它就是Redis客戶端程序。打開(kāi),輸入:
Set test 123
即在Redis中插入了一條key為test,value為123的數(shù)據(jù),繼續(xù)輸入:get test
得到value保存的數(shù)據(jù)123。
如果想知道Redis中一共保存了多少條數(shù)據(jù),則可以使用:keys * 來(lái)查詢:

3:asp.net使用Redis緩存簡(jiǎn)單示例
3.1測(cè)試Demo的結(jié)構(gòu)

3.2添加引用

3.3將參數(shù)寫(xiě)入配置文件
<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>
3.4讀取配置文件參數(shù)類
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;
}
3.5連接Redis,以及其他的一些操作類
public class RedisManager
{
private static PooledRedisClientManager prcm;
/// <summary>
/// 創(chuàng)建鏈接池管理對(duì)象
/// </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>
/// 客戶端緩存操作對(duì)象
/// </summary>
public static IRedisClient GetClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient();
}
/// <summary>
/// 緩存默認(rèn)24小時(shí)過(guò)期
/// </summary>
public static TimeSpan expiresIn = TimeSpan.FromHours(24);
/// <summary>
/// 設(shè)置一個(gè)鍵值對(duì),默認(rèn)24小時(shí)過(guò)期
/// </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>
/// 將某類數(shù)據(jù)插入到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>
/// 獲取一個(gè)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;
}
}
}
3.6測(cè)試頁(yè)面前后臺(tái)代碼
<form id="form1" runat="server"> <div> <asp:Label runat="server" ID="lbtest"></asp:Label> <asp:Button runat="server" ID ="btn1" OnClick="btn1_Click" Text="獲取測(cè)試數(shù)據(jù)"/> </div> </form>
protected void btn1_Click(object sender, EventArgs e)
{
string UserName;
//讀取數(shù)據(jù),如果緩存存在直接從緩存中讀取,否則從數(shù)據(jù)庫(kù)讀取然后寫(xiě)入redis
using (var redisClient = RedisManager.GetClient())
{
UserName = redisClient.Get<string>("UserInfo_123");
if (string.IsNullOrEmpty(UserName)) //初始化緩存
{
//TODO 從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù),并寫(xiě)入緩存
UserName = "張三";
redisClient.Set<string>("UserInfo_123", UserName, DateTime.Now.AddSeconds(10));
lbtest.Text = "數(shù)據(jù)庫(kù)數(shù)據(jù):" + "張三";
return;
}
lbtest.Text = "Redis緩存數(shù)據(jù):" + UserName;
}
}
測(cè)試結(jié)果圖
首次訪問(wèn)緩存中數(shù)據(jù)不存在,獲取數(shù)據(jù)并寫(xiě)入緩存,并設(shè)定有效期10秒

10秒內(nèi)再次訪問(wèn)讀取緩存中數(shù)據(jù)

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
asp.net下配置數(shù)據(jù)源時(shí)出現(xiàn): 未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例。
未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例的一個(gè)另類解決方法2008-06-06
ASP.NET MVC 數(shù)據(jù)驗(yàn)證及相關(guān)內(nèi)容
這篇文章主要介紹了ASP.NET MVC 數(shù)據(jù)驗(yàn)證及相關(guān)內(nèi)容的相關(guān)資料,需要的朋友可以參考下2014-10-10
asp.net textbox javascript實(shí)現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似
今天與大家分享一下 asp.net textbox javascript實(shí)現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似于QQ),這個(gè)功能到底怎么實(shí)現(xiàn)?首先聲明以下幾點(diǎn)2012-01-01
ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問(wèn)題詳解
這篇文章主要介紹了ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問(wèn)題詳解,需要的朋友可以參考下。2016-06-06
Entity?Framework實(shí)現(xiàn)數(shù)據(jù)遷移
本文詳細(xì)講解了Entity?Framework實(shí)現(xiàn)數(shù)據(jù)遷移的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
使用Aspose.Cells組件生成Excel文件實(shí)例
這篇文章主要介紹了使用Aspose.Cells組件生成Excel文件的方法,大家參考使用吧2013-11-11
asp.net coolite 刪除時(shí)彈出確定按鈕
如果用coolite的 Confirm() 是不知道你選擇了什么的 如上代碼才可以的2009-09-09

