1.文件缓存依赖

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"]会立即被清空
}
}
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"]);

2.NoSlidingExpiration 绝对过期时间

注:NoSlidingExpiration  绝对过期时间,当超过设定时间,立即移除。

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(), System.Web.Caching.Cache.NoSlidingExpiration);
}
}
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"]);
}
}

3.NoAbsoluteExpiration 滑动过期时间

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());
}
}
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//直接打开本页面,输出弹性过期测试
//如果一直不停地刷新,都会继续输出,但是当超过10秒后再刷新,不会再输出 滑动缓存测试
Response.Write(HttpContext.Current.Cache["DD"]);
}
}

4.缓存的优先级设置

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(), CacheItemPriority.High, null);
}
}
//在服务器释放系统内存时,具有该优先级级别的缓存项最有可能被从缓存删除。
Low = ,//在服务器释放系统内存时,具有该优先级级别的缓存项比分配了 System.Web.Caching.CacheItemPriority.Normal
//优先级的项更有可能被从缓存删除。
BelowNormal = ,//在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,其被删除的可能性仅次于具有 System.Web.Caching.CacheItemPriority.Low
Normal = ,//缓存项优先级的默认值为 System.Web.Caching.CacheItemPriority.Normal。
Default = ,//在服务器释放系统内存时,具有该优先级级别的缓存项被删除的可能性比分配了 System.Web.Caching.CacheItemPriority.Normal
//优先级的项要小。
AboveNormal = ,//在服务器释放系统内存时,具有该优先级级别的缓存项最不可能被从缓存删除。
High = ,//在服务器释放系统内存时,具有该优先级级别的缓存项将不会被自动从缓存删除。但是,具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除
NotRemovable = ,
优先级
Low=1
BelowNormal=2
Normal=3
Default=3
AboveNormal=4
High=5
NotRemoveable=6

5.当缓存被移除时,通知程序

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() ,Cache.NoSlidingExpiration,CacheItemPriority.Low, Show);
} public void Show(string key, object value, CacheItemRemovedReason reason)
{
Cache cache = HttpRuntime.Cache;
Cache.Insert("MyData", "缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!");
}
}
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HttpRuntime.Cache["MyData"]);
}
}

System.Web.Caching.Cache类 缓存的更多相关文章

  1. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  2. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  3. C# System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...

  4. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

  5. 清除 System.Web.Caching.Cache 以"xxx"开头的缓存

    public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...

  6. System.Web.Caching.Cache 方法汇总

    在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类.必然是有缺陷,暂时没有实现滑动缓存 using System; using System.Collections; using System. ...

  7. 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。

    一. 揭开迷雾 1. 程序集准备 a.  需要给项目添加 System.Web 程序集. b.  需要给使用的地方添加两个引用. 2. 程序集探究      在对应的类中输入关键字 Cache,选中点 ...

  8. 缓存-System.Web.Caching.Cache

    实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...

  9. System.Web.Caching.Cache缓存帮助类

    /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...

随机推荐

  1. sed小知识总结

    1)sed默认是打印出文件中的所有行的,使用 -n 选项可以只打印出 匹配 的行 2)当用到sed不同的编辑命令时,用{},且不同编辑命令之间用分号

  2. Effective C++ -----条款45:运用成员函数模板接受所有兼容类型

    请使用member function templates(成员函数模板)生成”可接受所有兼容类型“的函数. 如果你声明member templates 用于“泛化copy构造”或“泛化assignme ...

  3. problem-record-mysql

    #!/bin/bash # # Update_Problem - updates problem record in database ################################ ...

  4. zookeeper windows 入门安装和测试

    一.序言       以下是我对zookeeper 的一些理解:       zookeeper 作为一个服务注册信息存储的管理工具,好吧,这样说得很抽象,我们举个“栗子”. 栗子1号: 假设我是一家 ...

  5. jquery.base64.js简单使用

    jquery.base64.js, 加密,使用,先引入jquery,然后引入jquery.base64.js 使用如下 js中加密如下 $.base64.encode(result[i].ipadre ...

  6. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  7. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  8. Split()的简单的用法

    1.Split分割一些简答的字符串事例: string str="aaa|bbb|ccc"; string[] list = str.Split('|'); 这是一个字符的切割,但 ...

  9. Mysql 死锁的详细分析方法

    用数据库的时候,偶尔会出现死锁,针对我们的业务系统,出现死锁的直接结果就是系统卡顿.客户找事儿,所以我们也在想尽全力的消除掉数据库的死锁.出现死锁的时候,如果只是想解锁,用show full proc ...

  10. vector 去重复

    ①首先将vector排序 sort( vecSrc.begin(), vecSrc.end() ); // 1,2,3,3,4,4,6,7,8,9 ②然后使用unique算法,unique返回值是重复 ...