这里有新鲜出炉的 Redis 设计与实现(第一版),程序狗速度看过来!
Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。
本文主要介绍了 asp.net 性能优化之使用 Redis 缓存的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
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 秒内再次访问读取缓存中数据
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持 PHPERZ!
来源: http://www.phperz.com/article/17/0827/337970.html