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