1:使用Redis缓存的优化思路
redis的使用场景很多,仅说下本人所用的一个场景:
1.1对于大量的数据读取,为了缓解数据库的压力将一些不经常变化的而又读取频繁的数据存入redis缓存
大致思路如下:执行一个查询
1.2首先判断缓存中是否存在,如存在直接从Redis缓存中获取。
1.3如果Redis缓存中不存在,实时读取数据库数据,同时写入缓存(并设定缓存失效的时间)。
1.4缺点,如果直接修改了数据库的数据而又没有更新缓存,在缓存失效的时间内将导致读取的Redis缓存是错误的数据。
2:Redis傻瓜式安装
2.1双击执行redis-2.4.6-setup-64-bit.exe程序(下载地址:https://github.com/dmajkic/redis/downloads)
2.2可以将此服务设置为windows系统服务:
2.3测试是否安装成功:
再回到redis文件夹下,找到redis-cli.exe文件,它就是Redis客户端程序。打开,输入:
Set test 123
即在Redis中插入了一条key为test,value为123的数据,继续输入: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> /// 创建链接池管理对象 /// </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秒内再次访问读取缓存中数据