码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net缓存

时间:2015-04-07 19:00:52      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

希望减少每次数据库的范围可以考虑用缓存。。

缓存分永久缓存、时间缓存、和依赖缓存。。

1.永久缓存

if (Cache["cache"]!=null)

{
  
}else
{
  Cache.Insert("cache", "test");
}

2.时间缓存。时间过期缓存自动清空

if (Cache["cache"]!=null)
{

}else
{

  //第三个参数基本为null 时间都是秒。下面表示缓存10秒后清空。
  Cache.Insert("cache", "test",null,DateTime.Now.AddSeconds(10),System.Web.Caching.Cache.NoSlidingExpiration);
}

3.依赖缓存。

if (Cache["data"] == null)
{

  //依赖缓存基本都是依赖某个文件等。。可以是永久依赖也可以时间依赖

string fileUrl = Server.MapPath("Key.txt");
System.Web.Caching.CacheDependency cdp = new System.Web.Caching.CacheDependency(fileUrl);
StreamReader sr = new StreamReader(fileUrl, System.Text.Encoding.GetEncoding("GB2312"));
string s = sr.ReadLine();
sr.Close();
Cache.Insert("data", s, cdp);

}

Asp.Net缓存

标签:

原文地址:http://www.cnblogs.com/changeMe/p/4398917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!