asp.net性能优化之使用Redis缓存(入门)

本文主要介绍了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将参数写入配置文件

  

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; ///  /// 创建链接池管理对象 ///  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()); } ///  /// 客户端缓存操作对象 ///  public static IRedisClient GetClient() { if (prcm == null) CreateManager(); return prcm.GetClient(); } ///  /// 缓存默认24小时过期 ///  public static TimeSpan expiresIn = TimeSpan.FromHours(24); ///  /// 设置一个键值对,默认24小时过期 ///  ///  ///  ///  ///  ///  public static bool Set(string key, T value, IRedisClient redisClient) { return redisClient.Set(key, value, expiresIn); } ///  /// 将某类数据插入到list中 ///  ///  /// 一般是BiaoDiGuid ///  ///  public static void Add2List(string key, T item, IRedisClient redisClient) { var redis = redisClient.As(); var list = redis.Lists[GetListKey(key)]; list.Add(item); } ///  /// 获取一个list ///  ///  ///  ///  ///  public static IRedisList GetList(string key, IRedisClient redisClient) { var redis = redisClient.As(); 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测试页面前后台代码

 
 protected void btn1_Click(object sender, EventArgs e) { string UserName; //读取数据,如果缓存存在直接从缓存中读取,否则从数据库读取然后写入redis using (var redisClient = RedisManager.GetClient()) { UserName = redisClient.Get("UserInfo_123"); if (string.IsNullOrEmpty(UserName)) //初始化缓存 { //TODO 从数据库中获取数据,并写入缓存 UserName = "张三"; redisClient.Set("UserInfo_123", UserName, DateTime.Now.AddSeconds(10)); lbtest.Text = "数据库数据:" + "张三"; return; } lbtest.Text = "Redis缓存数据:" + UserName; } }

测试结果图

首次访问缓存中数据不存在,获取数据并写入缓存,并设定有效期10秒

10秒内再次访问读取缓存中数据

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持html中文网!

以上就是asp.net性能优化之使用Redis缓存(入门)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程