Cache expires before it

Can someone help me here? I have the following code for storing and playing catch, however it does not work. The cache expires in minutes, even if I set it to 14 days in slideExpiration mode. Thanks in advance!

public static List<ReplyDTO> VideoCommentList() { List<ReplyDTO> replyList = new List<ReplyDTO>(); if (HttpRuntime.Cache["videoComment"] == null) { HttpRuntime.Cache.Remove("videoComment"); HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14)); } else { replyList = (List<ReplyDTO>)HttpRuntime.Cache["videoComment"]; } if (replyList.Count > 8) { replyList = replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList(); } else { replyList = replyList.OrderByDescending(x => x.DateCreated).ToList(); } return replyList; } public static List<ReplyDTO> AddVideoComment(ReplyDTO replyDTO) { List<ReplyDTO> replyList = new List<ReplyDTO>(); replyList = VideoCommentList(); replyList.Add(replyDTO); HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14)); if (replyList.Count > 8) { replyList = replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList(); } else { replyList = replyList.OrderByDescending(x => x.DateCreated).ToList(); } return replyList; } 
+4
source share
5 answers

The ASP.net cache is stored in memory, so if your IIS process or application pool is recycled, it will become clear. You can check the following things that can cause the process to recycle.

  • If you modify web.config , IIS shuts down the old instance and slowly transfers the traffic to the new instance, in-memory is processed in this process. How to check it out . You can detect this situation by checking AppDomain.IsFinalizingForUnload and registering it during the callback.
  • Utilization of the application pool : IIS has a configuration according to which, if the IIS process is idle for a certain time, it processes it. You can check this on the server and increase this time, or completely disable processing.
  • Each process has a limit on the amount of memory that it can consume, if you add too many objects to the memory, this will increase the IIS memory consumption, and at critical times the OS will process the process.

EDIT

In your program, you add the replyList element to the cache, and then perform the .Take() operation. Since replyList is a reference object, if you modify it, it will also be updated in the cache. So if in your program, if you do replyList == null , it will update the item in the cache.

So change your code like this and try

 public static List<ReplyDTO> VideoCommentList() { List<ReplyDTO> replyList = new List<ReplyDTO>(); if (HttpRuntime.Cache["videoComment"] == null) { //Call to .Remove is not required //HttpRuntime.Cache.Remove("videoComment"); HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14)); } else { //No need to check count > 8, Take will handle it for you replyList = ((List<ReplyDTO>)HttpRuntime.Cache["videoComment"]) .OrderByDescending(x => x.DateCreated) .Take(8).ToList(); } return replyList; } public static List<ReplyDTO> AddVideoComment(ReplyDTO replyDTO) { //Read from cache List<ReplyDTO> replyList = ((List<ReplyDTO>)HttpRuntime.Cache["videoComment"]); if(replyList == null) replyList = VideoCommentList(); replyList.Add(replyDTO); HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14)); //Here you are creating a new list, and not referencing the one in the cache return replyList.OrderByDescending(x => x.DateCreated).Take(8).ToList(); } 

IMPORTANT OFFER

If you want to check when and why your object is removed from the cache, you can use the CacheItemRemovedCallback option at insertion. Using this parameter and CacheItemRemovedReason , you can register the reason for removing the object from the cache. Causes

  • Deleted . Your code removed the item from the cache by calling the Insert or Remove method.
  • Expired . The item is removed from the cache because it has expired.
  • Underutilized . When the system is running low on memory, it frees up memory by deleting an item from the cache.
  • DependencyChanged . An item is removed from the cache because its cache dependency is associated with it. (In your case, this is not valid)

We hope this information helps you.

+6
source

To keep track of WHY your item is being removed from the cache, I would recommend using another overload of the HttpRuntime.Cache.Insert method, which allows you to specify the CacheItemRemovedCallback callback function.

Cache.Insert Method (String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

Also, your caching code seems good. But as soon as you change your code to indicate a callback, write down the reason for the extraction, and this will probably give you a better idea of ​​why your cache code becomes clear.

Like most of the other answers, I suspect your application is being processed / reset for any reason. I think that most applications on a production machine are processed at least once a day, especially in a collaborative environment. Therefore, I would suggest that your data will be stored in the cache for a maximum of a day.

+1
source

The cache is in memory and expires when you reuse your application. I assume that you rate this on a development machine, where either low traffic or changes to your file cause the application to reload.

0
source

Your cache objects may be truncated for several reasons ...

AppDomain Recycling. Memory pressure. Crash. Using the web garden. Load balancing. and so on...

This post should clarify a bit more ...

http://blogs.msdn.com/b/praveeny/archive/2006/12/11/asp-net-2-0-cache-objects-get-trimmed-when-you-have-low-available-memory. aspx

0
source

In the AddVideoComment () method, change the insert line of the cache element:

  HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14),CacheItemPriority.NotRemovable,null); 

And in the VideoCommentList () method use:

  if (HttpRuntime.Cache["videoComment"] == null) { replyList = VideoCommentList(); HttpRuntime.Cache.Insert("videoComment", replyList, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(14),CacheItemPriority.NotRemovable,null); } 

No need to use HttpRuntime.Cache.Remove("videoComment"); , since HttpRuntime.Cache.Insert( will replace the existing cache element.

Cheers, DeveloperConcord

0
source

Source: https://habr.com/ru/post/1392596/


All Articles