Asp.Net Performance Optimization Using Redis Cache (Getting Started)
1: Optimization idea of using Redis cache
There are many usage scenarios of redis, only one scenario that I use:
1.1 For a large number of data reads, in order to relieve the pressure on the database, store some infrequently changed and frequently read data into the redis cache
The general idea is as follows: Execute a query
1.2 First, determine whether the cache exists, if so, get it directly from the Redis cache.
1.3 If the Redis cache does not exist, read the database data in real time and write to the cache at the same time (and set the cache invalidation time).
1.4 Disadvantage: If the data in the database is directly modified without updating the cache, the read Redis cache will be the wrong data within the time when the cache is invalid.
2: Redis fool installation
2.1 Double-click to execute the redis-2.4.6-setup-64-bit.exe program (download address: https://github.com/dmajkic/redis/downloads)
2.2 You can set this service as a windows system service:
2.3 Test whether the installation is successful:
Go back to the redis folder and find the redis-cli.exe file, which is the Redis client program. Open, enter:
Set test 123
That is, a data whose key is test and value is 123 is inserted into Redis, and continues to enter: get test
Get the data 123 saved by value.
If you want to know how many pieces of data are saved in Redis, you can use: keys * to query:
3: Simple example of asp.net using Redis cache
3.1 Test the structure of Demo
3.2 Add references
3.3 Write parameters to the configuration file
<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 Read configuration file parameter class
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 Connect to Redis, and some other operation classes
3.6 Test page front-end and back-end code
<form id="form1" runat="server"> <div> <asp:Label runat="server" ID="lbtest"></asp:Label> <asp:Button runat="server" ID="btn1" OnClick="btn1_Click" Text="Get test data"/> </div> </form>
protected void btn1_Click(object sender, EventArgs e) { string UserName; //Read data, if the cache exists, read directly from the cache, otherwise read from the database and then write to redis using (var redisClient = RedisManager.GetClient()) { UserName = redisClient.Get<string>("UserInfo_123"); if (string.IsNullOrEmpty(UserName)) //Initialize the cache { //TODO fetches data from database and writes to cache UserName = "Zhang San"; redisClient.Set<string>("UserInfo_123", UserName, DateTime.Now.AddSeconds(10)); lbtest.Text = "Database data:" + "Zhang San"; return; } lbtest.Text = "Redis cache data: " + UserName; } }
Test result graph
If the data does not exist in the cache for the first time, get the data and write it into the cache, and set the validity period to 10 seconds
Access the data in the read cache again within 10 seconds