最近一个项目ASP.NET+MySQL
有的网页打开初始化的查询需要10秒甚至更久,用户体验极差,而且并发量变大的时候网站容易崩溃
后来想了两种解决方案都不是太满意
1、数据库里建一张缓存表,后台作业定时去更新这张表,每次网页打开就可以直接从缓存表里查询
2、用户第一次打开网站将数据已文件的形式缓存到服务器上,下次直接从文件中读取数据
最后决定用Redis分布式缓存实现
Redis是在Linux系统上安装的,不过也有Windows版本的安装包,我是将它设置成了服务
下载地址 https://github.com/MicrosoftArchive/redis/releases
解压后目录结构如下
以管理员方式打开命令行,跳转到Redis解压的目录下,执行命令 redis-server --service-install redis.windows.conf
会提示服务安装成功
打开计算机管理->服务,已经可以看到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();
/// <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());
lbtest.Text = "数据库数据:" + "张三";
return;
}
lbtest.Text = "Redis缓存数据:" + UserName;
}
}
首次访问缓存中数据不存在,获取数据并写入缓存,并设定有效期10秒
10秒内再次访问读取缓存中数据