简体   繁体   English

了解HttpContext.Current.Cache

[英]Understanding HttpContext.Current.Cache

I've been making some changes to a site and have noticed that when I make a change in the database, ie. 我一直在对网站进行一些更改,并注意到当我在数据库中进行更改时,即。 add a new person value, the person's name doesn't appear in a dropdown list. 添加一个新的人员值,该人员的姓名不会出现在下拉列表中。

If I reset the application pool a few times the name does finally appear for me. 如果我重设了几次应用程序池,该名称最终会出现在我身上。 Though doesn't always seem to for others. 虽然似乎并不总是对别人有用。

It does seem to be controlled heavily by cache and I've found the following: 它似乎确实受高速缓存的控制,我发现以下内容:

HttpContext.Current.Cache.Insert("PersohnList", d, Nothing, 
       DateTime.MaxValue, New System.TimeSpan(0, 30, 0), 
       Caching.CacheItemPriority.High, Nothing)

I've noticed a few values here of interest, 我注意到这里有一些有趣的价值,

a)  DateTime.MaxValue  
b)  New System.TimeSpan(0, 30, 0)

Will the cache expire after 30 mins and insert the new person's name or will it not expire because of DateTime.MaxValue. 缓存将在30分钟后过期并插入新用户的姓名,还是由于DateTime.MaxValue而不会过期?

Any ideas? 有任何想法吗?

I am not certain which would win out. 我不确定哪个会赢。 It seems that this code is setting the slidingExpiration and the absoluteExpiration. 看来这段代码正在设置slideingExpiration和absoluteExpiration。 I would assume the latter would win in this but the MSDN documentation says that you should only use one or the other. 我以为后者会赢,但是MSDN文档说您只能使用其中一个。 If using the slidingExpiration, you should set the absoluteExpiration parameter to NoAbsoluteExpiration and if using the absoluteExpiration, you should set the slidingExpiration to NoSlidingExpiration . 如果使用slideExpiration,则应将absoluteExpiration参数设置为NoAbsoluteExpiration ,如果使用absoluteExpiration,则应将slideExpiration设置为NoSlidingExpiration

If I were you, I would just invalidate the cache when the value is being updated (HttpContext.Current.Cache.Remove("PersohnList") and reset it again. 如果您是我,那么我将在更新值(HttpContext.Current.Cache.Remove("PersohnList")时使缓存无效,然后再次将其重置。

+1 for sliding window. +1用于滑动窗口。 Each time another user is touching that key, "PersohnList", the cache expiration adds 30 more minutes. 每次其他用户触摸该键“ PersohnList”时,缓存过期将增加30分钟。 Also, check out System.Runtime.Caching as a more modern alternative. 另外,请查看System.Runtime.Caching作为更现代的替代方法。

From http://msdn.microsoft.com/en-us/library/system.runtime.caching%28v=vs.110%29.aspx 来自http://msdn.microsoft.com/zh-cn/library/system.runtime.caching%28v=vs.110%29.aspx

"The System.Runtime.Caching namespace contains types that let you implement caching in NET Framework applications. “ System.Runtime.Caching命名空间包含用于在NET Framework应用程序中实现缓存的类型。

The classes in this namespace provide a way to use caching facilities like those in ASP.NET, but without a dependency on the System.Web assembly." 该命名空间中的类提供了一种使用缓存工具的方式,例如ASP.NET中的缓存工具,但不依赖于System.Web程序集。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM