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

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

3:asp.net使用Redis緩存簡單示例
3.1測試Demo的結構

3.2添加引用

3.3將參數寫入配置文件
<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讀取配置文件參數類
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)建鏈接池管理對象
/// </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;
}
}
}
3.6測試頁面前后臺代碼
<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秒內再次訪問讀取緩存中數據

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- .NET?Core使用Redis實現創(chuàng)建分布式鎖
- .net core 3.1 Redis安裝和簡單使用
- 在.NET?Core中使用CSRedis的詳細過程
- ASP.NET?Core中使用Redis實現緩存
- .NET Core中使用Redis與Memcached的序列化問題詳析
- .net core如何使用Redis發(fā)布訂閱
- .net core使用redis基于StackExchange.Redis
- 詳解.NET中使用Redis數據庫
- 詳解Asp.net Core 使用Redis存儲Session
- 詳解如何在ASP.NET Core中使用Redis
- .net web優(yōu)雅地使用 redis的方法步驟
相關文章
IIS上部署Asp.net core Webapi的實現步驟
ASP.NET Core Web API是構建RESTful應用程序的理想平臺,本文主要介紹了IIS上部署Asp.net core Webapi的實現步驟,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Entity?Framework?Core相關包的概念介紹與安裝
這篇文章介紹了Entity?Framework?Core相關包的概念與安裝方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03

