The performance of the interface that reads the database each time and reads the interface through the cache is tested in detail above
Here we are going to prepare an actual introduction. Add the original database to the Redis cache optimization interface
1. Download Redis and Redis view management tools
Click to download the extraction code: 9tgg backup download address
After downloading, install Redis first and then install Redis view management tool
After opening, enter the following picture
After installing the necessary software, we start to make changes in the software
First, you need to add a StackExchange.Redis.dll reference to the project
You can download this file on Nuget or use the version I provided below
If you use the nuget right-click project to manage the nuget package, then select Browse in the upper right corner
Enter Redis search in the search box and find StackExchange.Redis
Please note that there is a version dependency requirement below because my mvc depends on .netFramwork4.5, so I chose a relatively low version 1.1.608 to install here
Of course, you can also use download dll to add dll by right-click-add
Click to download the extraction code: jcs1 alternate download address
After adding the required dll, we need to add RedisHelper to simplify our operation
Copy my RedisHelper to the project
Click to download the extraction code: u25p alternate download address
Please note that after copying, please modify the namespace to your project namespace address, if the dll reference is not available, please add the corresponding reference
Because the helper writes the address in the web.confg database connection address
So please add the following code in web.config
<connectionStrings>
<add name="RedisExchangeHosts" connectionString="127.0.0.1:6379,allowadmin=true"/>
</connectionStrings>
After the addition is complete, the reference to Redis is basically completed
Now you can add Redis code to your controller
RedisHelper redis = new RedisHelper(1);
This is to initialize Redis, 1 means to connect to the second database, if it is 0, it is to connect to the first database
The usage code is as follows:
using (MiniProfiler.Current.Step("开始加载数据:"))
{
try
{
//判断是否走缓存加载全场折扣
if (redis.StringGet<AllDiscount>("AllDiscount" + shopid) != null)
{
MiniProfiler.Current.Step("从Redis服务器中加载数据:");
allDiscount = redis.StringGet<AllDiscount>("AllDiscount" + shopid);
}
else
{
MiniProfiler.Current.Step("从MSSQL服务器中加载数据:");
allDiscount = ds.GetModel<AllDiscount>(m => m.Entid == shopid && m.Isopen == true && m.StartDate <= now && m.EndDate >= now);
redis.StringSet<AllDiscount>("AllDiscount" + shopid, allDiscount, TimeSpan.FromHours(2));
}
}
catch (Exception e)
{
MiniProfiler.Current.CustomTiming("Errors:", "Redis服务未启用,请开启该服务,并且请注意端口号,本项目使用的的6319,而且我的是没有设置密码。" + e.Message);
allDiscount = ds.GetModel<AllDiscount>(m => m.Entid == shopid && m.Isopen == true && m.StartDate <= now && m.EndDate >= now);
}
}
In the study of this writing Redis and referred a lot https://www.cnblogs.com/cang12138/p/8884362.html this blogger's blog, read this to see if the play still do not understand, then go look at this suggestion Article, at the same time welcome the blogger's QQ: 864015769 for discussion, although the blogger is also a small chicken (please note when you call me, otherwise the blogger may disagree)