Cache Expires Although Expiration Not Explicitly

I have a lot of bar available (about 25 GB of free memory), and I don’t want the cache to expire, and I just delete and repeat the elements when there is a change. Since my site is in the process of testing, it has 1 or 2 Kbytes of cached items, but when I check the cache after a while (for example, half an hour), I see that they expired. I use this code to insert into the cache:

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

This is my first experience using a cache. Does anyone know what is wrong with the code or cache?

+4
source share
3 answers

Most likely, if you leave it for a while, then your application domain is disconnected due to lack of use, and if so, it makes it in the memory cache.

ASP.NET - .

+1

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

, , , :

Cache.Insert ASP.NET

+1

. , HttpRuntime.Cache , "", . , CacheItemPriority.NotRemovable / ( ).

HttpRuntime.Cacheprovides a remote callback to use when deleting an item. Of course, to filter out normal evictions at the end of the application pool, you need to check System.Web.Hosting.HostingEnvironment.ShutdownReason.

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. Full analysis can be read here .

0
source

All Articles