惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Project Zero
Project Zero
N
Netflix TechBlog - Medium
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
The Register - Security
The Register - Security
P
Palo Alto Networks Blog
GbyAI
GbyAI
量子位
Simon Willison's Weblog
Simon Willison's Weblog
Cyberwarzone
Cyberwarzone
M
MIT News - Artificial intelligence
T
Threatpost
腾讯CDC
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
博客园 - 叶小钗
V
V2EX
美团技术团队
NISL@THU
NISL@THU
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
爱范儿
爱范儿
G
Google Developers Blog
博客园 - Franky
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
B
Blog
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Latest news
Latest news
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
F
Full Disclosure
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
Scott Helme
Scott Helme
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs

博客园 - aming

.Net线程 浅析C# new和override的区别 收集一些.NET开发资源站点和部分优 秀.NET开源项目 (转) 你必须知道的.net C#正则表达式整理备忘 ORACLE 基础 深入认识javascript中的eval函数 FCKEditor的使用及配置方法 个人总结 javascript跨域问题 Javascript的IE和Firefox兼容问题 JavaScript的私有变量 typeof、instanceof、constructor与对象类型 javascript内存泄漏问题 JavaScript 中的内存泄露模式 jQuery Ajax 全解析 悟透JavaScript (转) JavaScript的9个陷阱及评点 全面理解javascript的caller,callee,call,apply概念(修改版)
asp.net cache
aming · 2010-04-29 · via 博客园 - aming

asp.net 使用驻留在页面中的Cache缓存常用可定时更新的数据

这个就先需要先知道一下System.Web.Caching.Cache类,其实在我做WEB应用的时候,我会将一些使用频繁但是又要经常使用并且需要急时更新的对象放到Cache中,这样可以很大程序上减少从硬盘上读取数据的次数。

就是希望让Web应用程序从一开始运行到结束都一直存在,有人就说为什么不用Application呢?其实Cache是可以一段时间内自动更新数据的,而Application就无法做成这样的,另外Application在Web这种高并发的系统中一定要考虑线程安全的问题,Application本身就不是线程安全的,而Cache就是线程安全。所以一般我都会在很多对象中我只从Web开始运行的时候从数据库或文件里获取一次数据,在不同的页面中,都是使用Cache的,而且Cache中的数据可能弄成自动更新的,所以一般情况不需要考虑数据更新的问题,另外Cache也是也应用程序关联的,全部可能驻留在该Web应用程序中的任何页面。

当然首先我们应该熟悉一下System.Web.Caching.Cache这个类,我这里也就只说说其中使用比较多的几个方法和属性,如果需要更详细的介绍请查阅MSDN。

首先介绍它的Add()方法,将指定的对象添加到Cache对象集合中。

Insert()方法将覆盖有相同Key的Cache顶。

Remove()从应用程序的Cache对象中移除指定项。

Count属性,获取存储在缓存中对象数。

这里我主要要说的是Add()方法,因为我们如果想要长期存在Cache并且在一段时间内自动替换Cache,就必须非常了解它。我们看看MSDN中对该方法的详细介绍吧。呵。

C#

public Object Add(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback
)
参数
key
类型:System.String

用于引用该项的缓存键。

value
类型:System.Object

要添加到缓存的项。

dependencies
类型:System.Web.Caching.CacheDependency

该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。

absoluteExpiration
类型:System.DateTime

所添加对象将到期并被从缓存中移除的时间。如果使用可调到期,则 absoluteExpiration 参数必须为 NoAbsoluteExpiration。

slidingExpiration
类型:System.TimeSpan

最后一次访问所添加对象时与该对象到期时之间的时间间隔。如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并从缓存中移除。如果使用绝对到期,则 slidingExpiration 参数必须为 NoSlidingExpiration。

priority
类型:System.Web.Caching.CacheItemPriority

对象的相对成本,由 CacheItemPriority 枚举表示。缓存在退出对象时使用该值;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。

onRemoveCallback
类型:System.Web.Caching.CacheItemRemovedCallback

在从缓存中移除对象时所调用的委托(如果提供)。当从缓存中删除应用程序的对象时,可使用它来通知应用程序。

返回值

类型:System.Object

备注

如果 Cache 中已保存了具有相同 key 参数的项,则对此方法的调用将失败。若要使用相同的 key 参数覆盖现有的 Cache 项,请使用 Insert 方法。

无法同时设置 absoluteExpiration 和 slidingExpiration 参数。如果要让缓存项在特定时间到期,可将 absoluteExpiration 参数设置为特定时间,并将 slidingExpiration 参数设置为 NoSlidingExpiration。

如果要让缓存项在最后一次访问该项后的某段时间之后到期,可将 slidingExpiration 参数设置为到期间隔,并将 absoluteExpiration 参数设置为 NoAbsoluteExpiration。

 此段转载自MSDN

具体的怎样添加这种缓存了,一般情况我会在母版页中Init事件或Load事件中添加缓存,因为这样可以保证引用程序从一开始执行就可以将需要频繁使用的对象缓存起来了。

protected void Page_Load(object sender, EventArgs e)
{
    Cache.Add("key",                //需要添加到Cache中的键
        new { value="添加值"},      //对应的值
        null,                       //缓存依赖项。
        DateTime.Now.AddMinutes(1),//固定缓存时间
        System.Web.Caching.Cache.NoSlidingExpiration, //可到延时缓存时间,
        System.Web.Caching.CacheItemPriority.NotRemovable, //缓存中的优先级。
        new System.Web.Caching.CacheItemRemovedCallback(OnMoveCacheBack));//移除时调用的回调函数
}

public void OnMoveCacheBack(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
{
    if (Cache[key] != null)
    {
        Cache.Remove(key);
    }
    Cache.Add("key",                //需要添加到Cache中的键
        new { value = "更新值" },      //对应的值
        null,                       //缓存依赖项。
        DateTime.Now.AddMinutes(1),//固定缓存时间
        System.Web.Caching.Cache.NoSlidingExpiration, //可到延时缓存时间,
        System.Web.Caching.CacheItemPriority.NotRemovable, //缓存中的优先级。
        new System.Web.Caching.CacheItemRemovedCallback(OnMoveCacheBack));//移除时调用的回调函数
}

参数的具体使用上一定要注意三点,

第一就是缓存的依赖项一定要指定为null。

第二固定到期缓存时间不能和可到延时缓存时间同时指定时间,实现我说的定期更换数据的,当然就要使用固定到期缓存时间了。

第三就是该缓存的优先级了,这个也比较关键,一定要指定为System.Web.Caching.CacheItemPriority.NotRemovable枚举值,这样才不会被自动收回,但是一定要注意该缓存的大小。