System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
Posted ceci
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了System.Web.Caching.Cache类 缓存 各种缓存依赖(转)相关的知识,希望对你有一定的参考价值。
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html
Cache类,是一个用于缓存常用信息的类。HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例。尽量使用前者。
一、属性
属性 | 说明 |
Count | 获取存储在缓存中的项数。 |
EffectivePercentagePhysicalMemoryLimit | 获取在 ASP.NET 开始从缓存中移除项之前应用程序可使用的物理内存百分比。 |
EffectivePrivateBytesLimit | 获取可用于缓存的字节数。 |
Item | 获取或设置指定键处的缓存项。 |
二、方法
方法名称 | 说明 |
Add | 将指定项添加到 Cache 对象,该对象具有依赖项、到期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序)。 |
Get | 从 Cache 对象检索指定项。 |
GetEnumerator | 检索用于循环访问包含在缓存中的键设置及其值的字典枚举数。 |
Insert(String, Object) | 向 Cache 对象插入项,该项带有一个缓存键引用其位置,并使用 CacheItemPriority 枚举提供的默认值。 |
Insert(String, Object, CacheDependency) | 向 Cache 中插入具有文件依赖项或键依赖项的对象。 |
Insert(String, Object, CacheDependency, DateTime, TimeSpan) | 向 Cache 中插入具有依赖项和到期策略的对象。 |
Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) | 将对象与依赖项、到期策略以及可用于在从缓存中移除项之前通知应用程序的委托一起插入到 Cache 对象中。 |
Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) | 向 Cache 对象中插入对象,后者具有依赖项、到期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序)。 |
Remove | 从应用程序的 Cache 对象移除指定项。 |
三、静态字段
名称 | 说明 |
NoAbsoluteExpiration | 用于 Insert 方法调用中的 absoluteExpiration 参数中以指示项从不到期。 此字段为只读。 |
NoSlidingExpiration | 用作 Insert 或 Add 方法调用中的 slidingExpiration 参数,以禁用可调到期。 此字段为只读。 |
先来看基本的示例:
index.aspx.cs页面代码:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person p = new Person();
p.Id = 1;
p.Name = "诸葛亮";
Cache cache = HttpRuntime.Cache;
cache.Insert("AA",p);
cache.Insert("BB","字符串");
Response.Write(cache.Get("BB").ToString()); //输出 字符串
Person p2 = cache["AA"] as Person;
Response.Write(p2.Id + " : " + p2.Name); //输出 1 : 诸葛亮
Response.Write(cache.EffectivePrivateBytesLimit); //-1 这是一个只读属性,那就没什么好说了,只能输出来看看了,但是-1是什么意思呢?无限吗
Response.Write(cache.EffectivePercentagePhysicalMemoryLimit); //98 开始移除项之前可以使用到98%
Response.Write(cache.Count); //输出 3
Response.Write(cache["BB"]); //输出 字符串 支持索引器式的读取
cache.Remove("BB"); //从cache中移除一项
Response.Write("~~~" + cache["BB"] + "~~~"); //移除了输出 null,但程序不报错
foreach (var obj in cache)
{
Response.Write(obj.GetType() + "<br/>"); //输出不知道什么鸟东西
}
}
}
public class Person
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
四、文件缓存依赖
当被依赖的文件更改时,缓存会立即被清空:
index.aspx.cs代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cache cache = HttpContext.Current.Cache;
//文件缓存依赖
cache.Insert("CC", "依赖项测试", new CacheDependency(@"D:\\123.txt"));
//这时候在about.aspx页面添加一行代码,当更改一下D:123.txt时,cache["cc"]会立即被清空
}
}
about.aspx.cs代码:
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//直接打开本页面,输出缓存依赖项测试
//当更改D:\\123.txt之后,在刷新,输出空,表明该Cache是依赖于D:\\123.txt的
Response.Write(HttpContext.Current.Cache["CC"]);
}
}
五、NoSlidingExpiration 绝对过期时间
NoSlidingExpiration 绝对过期时间,当超过设定时间,立即移除。
下面来看下绝对过期时间的示例,index.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cache cache = HttpContext.Current.Cache;
//30秒后就到期,立即移除,没商量
cache.Insert("DD", "绝对过期测试", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration);
}
}
about.aspx.cs:
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//先打开index.aspx添加到缓存 然后立即打开本页面,输出 绝对过期测试
//持续刷新5秒后,不会再输出 绝对过期测试
Response.Write(HttpContext.Current.Cache["DD"]);
}
}
六、NoAbsoluteExpiration 滑动过期时间
NoAbsoluteExpiration 当超过设定时间没再使用时,才移除缓存
滑动过期测试,index.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cache cache = HttpContext.Current.Cache;
//弹性过期时间,当缓存没使用10秒就过期
cache.Insert("DD", "滑动过期测试", null, System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(10));
}
}
about.aspx.cs:
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//直接打开本页面,输出弹性过期测试
//如果一直不停地刷新,都会继续输出,但是当超过10秒后再刷新,不会再输出 滑动缓存测试
Response.Write(HttpContext.Current.Cache["DD"]);
}
}
注意 当设置绝对到期时间时,请使用 DateTime 结构。当设置弹性过期时间时,请使用 TimeSpan 结构。另外,如果您创建的弹性过期时间小于零或大于一年,则将引发 ArgumentOutOfRangeException 类。
七、缓存的优先级设置
CacheItemPriority枚举 设置缓存的优先级
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cache cache = HttpContext.Current.Cache;
//文件权重级别
cache.Add("MyData", "缓存重要级别", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null);
}
}
该枚举一共有六级:
//在服务器释放系统内存时,具有该优先级级别的缓存项最有可能被从缓存删除。
Low = 1,//在服务器释放系统内存时,具有该优先级级别的缓存项比分配了 System.Web.Caching.CacheItemPriority.Normal
//优先级的项更有可能被从缓存删除。
BelowNormal = 2,//在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,其被删除的可能性仅次于具有 System.Web.Caching.CacheItemPriority.Low
Normal = 3,//缓存项优先级的默认值为 System.Web.Caching.CacheItemPriority.Normal。
Default = 3,//在服务器释放系统内存时,具有该优先级级别的缓存项被删除的可能性比分配了 System.Web.Caching.CacheItemPriority.Normal
//优先级的项要小。
AboveNormal = 4,//在服务器释放系统内存时,具有该优先级级别的缓存项最不可能被从缓存删除。
High = 5,//在服务器释放系统内存时,具有该优先级级别的缓存项将不会被自动从缓存删除。但是,具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除
NotRemovable = 6,
优先级 |
Low=1 |
BelowNormal=2 |
Normal=3 |
Default=3 |
AboveNormal=4 |
High=5 |
NotRemoveable=6 |
八、当缓存被移除时,通知程序
这时就要用到Add的最后一个参数,委托了:
index.aspx.cs代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Cache cache = HttpRuntime.Cache;
//文件权重级别
cache.Add("MyData", "缓冲移除通知", null, DateTime.Now.AddSeconds(10) ,Cache.NoSlidingExpiration,CacheItemPriority.Low, Show);
}
System.Web.Caching.Cache类 缓存 各种缓存依赖(转)