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.
source share