简体   繁体   English

ASP.NET MVC 3缓存持续时间

[英]ASP.NET MVC 3 Cache Duration

I have an ASP.NET MVC 3 app hosted on IIS. 我在IIS上托管了一个ASP.NET MVC 3应用程序。 This app is using MemoryCache to work with some data in memory to help minimize hits to my database. 此应用程序正在使用MemoryCache处理内存中的某些数据,以最大程度地减少对我的数据库的访问。 I would like this data to stay cached for up to a single day. 我希望这些数据最多可以保留一天。 In my code, I'm setting the following: 在我的代码中,我设置了以下内容:

List<MyObject> myItems = GetMyItems();
CacheItemPolicy cachePolicy = new CacheItemPolicy();
cachePolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(1440));
Cache.Set("myCacheKey", myItems, cachePolicy);

While my caching seems to be working. 虽然我的缓存似乎可以正常工作。 However, it does not seem to stay in the cache for the intended duration. 但是,它似乎没有在预期的持续时间内保留在缓存中。 Is there a setting I need to make in the web.config, if so, what setting? 我需要在web.config中进行设置吗? If not, is it an IIS setting that I need to set to enable a longer cache duration? 如果不是,是否需要设置IIS设置以启用更长的缓存持续时间?

The problem most likely lies in that your app pool is recycling after periods of non-use. 问题很可能在于您的应用程序池在闲置一段时间后正在回收。 When app pools are recycled, everything you had in memory is lost. 回收应用程序池后,您拥有的所有内存都将丢失。

If you need a more durable cache, you will need to switch to something that is not hosted in process. 如果需要更持久的缓存,则需要切换到未托管的内容。 There are a number of commercial and open-source caching services; 有许多商业和开源缓存服务。 I would recommend going with one of those. 我建议选择其中之一。

Here are just a few cache solutions: 以下是一些缓存解决方案:

  1. SharedCache 共享缓存
  2. AppFabric AppFabric
  3. NCache 缓存
  4. MemCached.NET MemCached.NET

Amongst others. 在其他人中。

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

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