我正在编写一个 MVC 5 互联网应用程序,并使用 MemoryCache 对象来缓存对象。我看到使用 MemoryCache.Set 方法可以指定 absoluteExpiration。
如果我使用以下方式从 MemoryCache 添加和检索对象,absoluteExpiration 设置为:
cache['cacheItem'] = testObject;
TestObject testObject = cache['cacheItem'] as TestObject;
此外,在 MVC 互联网应用程序中使用 MemoryCache 时,我应该设置可用于 MemoryCache 的内存量,还是默认实现安全够用一个 Azure 网站吗?
提前致谢。
请您参考如下方法:
您的代码相当于调用 Add,如下所示:
cache.Add("cacheItem", testObject, null);
添加的条目将具有默认的过期时间,该时间是无限的(即,它不会过期)。查看MSDN on CacheItemPolicy.AbsoluteExpiration了解详情。
回答有关内存使用的问题:(来自 CacheMemoryLimitMegabytes 属性):
The default is zero, which indicates that MemoryCache instances manage their own memory based on the amount of memory that is installed on the computer.
我会说让 MemoryCache 默认值决定使用多少内存是安全的,除非您正在做一些非常奇特的事情。
