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系统服务:

asp.net性能优化之如何使用Redis缓存

2.3测试是否安装成功:

再回到redis文件夹下,找到redis-cli.exe文件,它就是Redis客户端程序。打开,输入:

Set test 123

即在Redis中插入了一条key为test,value为123的数据,继续输入:get test

得到value保存的数据123。

如果想知道Redis中一共保存了多少条数据,则可以使用:keys * 来查询:

asp.net性能优化之如何使用Redis缓存

3:asp.net使用Redis缓存简单示例

3.1测试Demo的结构

asp.net性能优化之如何使用Redis缓存

3.2添加引用

asp.net性能优化之如何使用Redis缓存

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秒

asp.net性能优化之如何使用Redis缓存

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

asp.net性能优化之如何使用Redis缓存

关于“asp.net性能优化之如何使用Redis缓存”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


标题名称:asp.net性能优化之如何使用Redis缓存-创新互联
本文URL:http://scyanting.com/article/gjgpj.html