4

I have plenty of available ram (about 25 GB of free memory) and I don't want the cache to expire and I just remove and recache items when there is a change.As my website is in testing process it has 1 or 2 KBs of cached items but when I check cache after some time (like half an hour) I see that they have expired. I use this code for inserting into cache:

Cache.Insert(ckey, Results, null, Cache.NoAbsoluteExpiration, TimeSpan.Zero);

This is my first time to use cache, Does anybody know what is wrong with the code or the cache?

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

3 Answers3

1

try this

Cache.Insert(
 ckey, Results,
 null,                     /*CacheDependency*/
Cache.NoAbsoluteExpiration,     /*absoluteExpiration*/
Cache.NoSlidingExpiration,      /*slidingExpiratioin*/
CacheItemPriority.Normal, /*priority*/
null                      /*onRemoveCallback*/
);

View this article for further info, it may be already answered there:

Default duration of Cache.Insert in ASP.NET

Community
  • 1
  • 1
Baahubali
  • 4,604
  • 6
  • 33
  • 72
1

Chances are if you are leaving it for some time then your app domain is shutting down due to lack of use and if that goes so does its in memory cache.

ASP.NET Data Cache - preserve contents after app domain restart discusses this issue and some possible solutions to it.

Community
  • 1
  • 1
Chris
  • 27,210
  • 6
  • 71
  • 92
  • but does the app domain shut down after half an hour? – Ashkan Mobayen Khiabani Jun 18 '15 at 08:57
  • You tell me. :) It might do. There are various ways to tell if your app domain has been recycled which I'm sure a quick google will tell you about. If it is important that your cache not be lost though then you need to be aware of the possibility at least so you can look at other forms of caching that might be more reliable. – Chris Jun 18 '15 at 09:06
  • well my website has about 100000 page views per day and now I'm writing my website from scratch adding multi language and ... so as I have lots of available memory I want to do the best I can – Ashkan Mobayen Khiabani Jun 18 '15 at 09:22
0

I have stumbled upon a similar issue. It looks that HttpRuntime.Cache takes the liberty of removing items from cache when it "feels" that there is not enough memory. This happens even if CacheItemPriority.NotRemovable priority is provided along with no absolute/no sliding expiration and under normal application domain operation (no shutdown).

How to catch actual expiration

HttpRuntime.Cache provides a remove callback to be used when an item is removed. Of course, in order to filter out normal evictions on application pool shutdown, System.Web.Hosting.HostingEnvironment.ShutdownReason should be checked.

public class ApplicationPoolService : IApplicationPoolService
{
    public bool IsShuttingDown()
    {
        return System.Web.Hosting.HostingEnvironment.ShutdownReason != ApplicationShutdownReason.None;
    }
}

private void ReportRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    if (!ApplicationPoolService.IsShuttingDown())
    {
        var str = $"Removed cached item with key {key} and count {(value as IDictionary)?.Count}, reason {reason}";
        LoggingService.Log(LogLevel.Info, str);
    }
}

HttpRuntime.Cache.Insert(CacheDictKey, dict, dependencies: null,
            absoluteExpiration: DateTime.Now.AddMinutes(absoluteExpiration),
            slidingExpiration: slidingExpiration <= 0 ? Cache.NoSlidingExpiration : TimeSpan.FromMinutes(slidingExpiration),
            priority: CacheItemPriority.NotRemovable,
            onRemoveCallback: ReportRemovedCallback);

Alternative

MemoryCache can be used as a good replacement for HttpRuntime.Cache. It provides very similar functionality. A full analysis can be read here.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164