【发布时间】:2012-10-07 00:11:24
【问题描述】:
我在我的 WCF 服务中使用 .Net 4.0 MemoryCache。 我最初使用的默认缓存如下:
var cache = MemoryCache.Default;
然后按照通常的模式尝试在缓存中查找内容,获取,然后 如果没有找到则设置到缓存中(代码sn-p/伪代码如下):
var geoCoordinate = cache.Get(cacheKey) as GeoCoordinate;
if (geoCoordinate == null)
{
geoCoordinate = get it from somewhere;
cache.Set(cacheKey, geoCoordinate, DateTimeOffset.Now.AddDays(7));
}
我发现我的条目在大约 1 分钟后消失了。 2分钟。即使我的代码将丢失的条目放回缓存中,后续的缓存 Gets 也会返回 null。 我的 WCF 服务由 IIS 7.5 托管。如果我回收应用程序池,一切都会正常工作 2 分钟,但随后会重复上述模式。
在做了一些研究之后,我做了以下替换: var cache = MemoryCache.Default;
// 使用新代码创建缓存如下:
var config = new NameValueCollection();
//Hack: Set Polling interval to 10 days, so will no clear cache.
config.Add("pollingInterval", "10:00:00:00");
config.Add("physicalMemoryLimitPercentage", "90");
config.Add("cacheMemoryLimitMegabytes", "2000");
//instantiate cache
var cache = new MemoryCache("GeneralCache", config);
似乎无论我将什么放入physicalMemoryLimitPercentage 或cacheMemoryLimitMegabytes 似乎都没有帮助。但是将pollingInterval 放到一个大的datespan 上就可以了。
ie:如果我设置如下: config.Add("pollingInterval", "00:00:15:00"); 然后一切正常 15 分钟。
注意:如果我的 WCF 服务由我的开发环境中的 IISExpress 托管,我将无法重现。 当我的 WCF 服务由 IIS 7.5 托管时,这似乎也会发生。 我在 IIS 7.5 上的应用程序池未回收。
有没有人经历过这样的事情? 我看过以下内容: MemoryCache does not obey memory limits in configuration
谢谢,
马特
【问题讨论】: