ASP.NET性能优化之构建自定义文件缓存

ASP.NET的输出缓存(即静态HTML)在.NET4.0前一直是基于内存的。这意味着如果我们的站点含有大量的缓存,则很容易消耗掉本机内存。现在,借助于.NET4.0中的OutputCacheProvider,我们可以有多种选择创建自己的缓存。如,我们可以把HTML输出缓存存储到memcached分布式集群服务器,或者MongoDB中(一种常用的面向文档数据库,不妨阅读本篇http://msdn.microsoft.com/zh-cn/magazine/gg650661.aspx)。当然,我们也可以把缓存作为文件存储到硬盘上,考虑到可扩展性,这是一种最廉价的做法,本文就是介绍如果构建自定义文件缓存。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册虚拟主机、营销软件、网站建设、莘县网站维护、网站推广。

  1:OutputCacheProvider

  OutputCacheProvider是一个抽象基类,我们需要override其中的四个方法,它们分别是:

  Add 方法,将指定项插入输出缓存中。

  Get 方法,返回对输出缓存中指定项的引用。

  Remove 方法,从输出缓存中移除指定项。

  Set 方法,将指定项插入输出缓存中,如果该项已缓存,则覆盖该项。

  2:创建自己的文件缓存处理类

  该类型为FileCacheProvider,代码如下:

 
 
 
 
  1.   public class FileCacheProvider : OutputCacheProvider
  2.   {
  3.   private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  4.  public override void Initialize(string name, NameValueCollection attributes)
  5.   {
  6.   base.Initialize(name, attributes);
  7.   CachePath = HttpContext.Current.Server.MapPath(attributes["cachePath"]);
  8.   }
  9.   public override object Add(string key, object entry, DateTime utcExpiry)
  10.   {
  11.   Object obj = Get(key);
  12.   if (obj != null) //这一步很重要
  13.   {
  14.   return obj;
  15.   }
  16.   Set(key,entry,utcExpiry);
  17.   return entry;
  18.   }
  19.   public override object Get(string key)
  20.   {
  21.   string path = ConvertKeyToPath(key);
  22.   if (!File.Exists(path))
  23.   {
  24.   return null;
  25.   }
  26.   CacheItem item = null;
  27.   using (FileStream file = File.OpenRead(path))
  28.   {
  29.   var formatter = new BinaryFormatter();
  30.   item = (CacheItem)formatter.Deserialize(file);
  31.   }
  32.   if (item.ExpiryDate <= DateTime.Now.ToUniversalTime())
  33.   {
  34.   log.Info(item.ExpiryDate + "*" + key);
  35.   Remove(key);
  36.   return null;
  37.   }
  38.   return item.Item;
  39.   }
  40.   public override void Set(string key, object entry, DateTime utcExpiry)
  41.   {
  42.   CacheItem item = new CacheItem(entry, utcExpiry);
  43.   string path = ConvertKeyToPath(key);
  44.   using (FileStream file = File.OpenWrite(path))
  45.   {
  46.   BinaryFormatter formatter = new BinaryFormatter();
  47.   formatter.Serialize(file, item);
  48.   }
  49.   }
  50.   public override void Remove(string key)
  51.   {
  52.   string path = ConvertKeyToPath(key);
  53.   if (File.Exists(path))
  54.   File.Delete(path);
  55.  }
  56.   public string CachePath
  57.   {
  58.   get;
  59.   set;
  60.   }
  61.   private string ConvertKeyToPath(string key)
  62.   {
  63.   string file = key.Replace('/', '-');
  64.   file += ".txt";
  65.   return Path.Combine(CachePath, file);
  66.   }
  67.   }
  68.   [Serializable]
  69.   public class CacheItem
  70.   {
  71.   public DateTime ExpiryDate;
  72.   public object Item;
  73.   public CacheItem(object entry, DateTime utcExpiry)
  74.   {
  75.   Item = entry;
  76.   ExpiryDate = utcExpiry;
  77.   }
  78.   }

  有两个地方需要特别说明:

  在Add方法中,有一个条件判断,必须做出这样的处理,否则缓存机制将会缓存***次的结果,过了有效期后缓存讲失效并不再重建;

  在示例程序中,我们简单的将缓存放到了Cache目录下,在实际的项目实践中,考虑到缓存的页面将是成千上万的,所以我们必须要做目录分级,否则寻找并读取缓存文件将会成为效率瓶颈,这会耗尽CPU。

  3:配置文件

  我们需要在Web.config中配置缓存处理程序是自定义的FileCacheProvider,即在 下添加节点:

 
 
 
 
  1.   
  2.   
  3.   
  4.   
  5.   
  6.   
  7.  

  4:缓存的使用

  我们假设在MVC的控制中使用(如果要在ASP.NET页面中使用,则在页面中包含<%@OutputCache VaryByParam="none" Duration="10" %>),可以看到,Index是未进行输出缓存的,而Index2进行了输出缓存,缓存时间为10秒。

 
 
 
 
  1.   public class HomeController : Controller
  2.   {
  3.   private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  4.   static string s_conn = "Data Source=192.168.0.77;Initial Catalog=luminjidb;User Id=sa;Password=sa;";
  5.   public ActionResult Index()
  6.   {
  7.   using (DataSet ds = Common.SqlHelper.ExecuteDataset(s_conn, CommandType.Text, "select top 1* from NameTb a, DepTb b where a.DepID = b.ID ORDER BY NEWID()"))
  8.   {
  9.   ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();
  10.   }
  11.   return View();
  12.   }
  13.   [OutputCache(Duration = 10, VaryByParam = "none")]
  14.   public ActionResult Index2()
  15.   {
  16.   using (DataSet ds = Common.SqlHelper.ExecuteDataset(s_conn, CommandType.Text, "select top 1* from NameTb a, DepTb b where a.DepID = b.ID ORDER BY NEWID()"))
  17.   {
  18.   ViewBag.Message = ds.Tables[0].Rows[0]["name"].ToString();
  19.   }
  20.   return View();
  21.   }
  22.   }

  5:查看下效果

  上面的代码,在访问了Index2后,将会在Cache文件夹下产生缓存文件,如下:

  现在,我们开始评价下有输出缓存和无输出缓存的性能对比,模拟100个用户并发1000次请求如下:

可以看到,有输出缓存后,吞吐率明显提高了10倍。

  6:代码下载

  FileCacheProvider的原始代码来自于网络,我修改了其中的BUG,全部代码下载如下:MvcApplication20110907.rar

  职业指导:

  在使用某一技能三个月后,你还不是专家,即便使用时间是三年,你还不是。马尔科姆·格莱德威尔在《异类》一书中指出,成为一名真正的专家,需要10000小时。10000小时!如果一天用10小时,每天都学习,则大概需要3年时间。如果一天5小时,一年学习200天,则大概需要10年时间。10年!

原文:http://www.cnblogs.com/luminji/archive/2011/09/08/2169955.html

网站标题:ASP.NET性能优化之构建自定义文件缓存
URL网址:http://www.shufengxianlan.com/qtweb/news18/56318.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联