MemoryCache - prevent expiration of items
Asked Answered
B

1

11

In my application I use MemoryCache but I don't expect items to expire. Items are therefore inserted to the cache with default policy, without AbsoulteExpiration or SlidingExpiration being set.

Recently, on high server last, I experienced problems with cache, as it returned null values in place of desired values, inserted to the cache before. It turned out, that not only items eligible to expire (as those with expiration date explicitly set) are removed from the cache. Under memory pressure, where values of CacheMemoryLimit and/or PhysicalMemoryLimit are exceeded, MemoryCache removes other elements as well.

How to prevent this? How to be sure, that when element is set to the cache once, it can be safely fetched from it again?

I considered setting the PollingInterval to some huge value, but this only delays the potential problem (and the polling interval is referenced in documentation as maximal time, not the exact or minimal time). Setting PhysicalMemoryLimitPercentage to 100% also does not solve the problem since it references to the physically installed memory and not to the whole available virtual memory. Or am I wrong and it would indeed help?

Boudreau answered 27/11, 2017 at 16:26 Comment(5)
Your question directs us to suggest increasing RAM or remove some values from cache periodically. You can't fill the cache forever.Huerta
Perhaps you would want to use a static ConcurrentDictionary instead?Seddon
CacheItemPolicy has a Priority property which can be set to NotRemovable.Moramorabito
Thank you @EricB, I missed somehow this setting and this is exactly what I need.Boudreau
I've added an expanded version of my comment as an answer since it resolved your issue, and to help others find it in the future.Moramorabito
M
16

CacheItemPolicy has a Priority property which can be set to NotRemovable.

You do need to be aware of how much data you are adding to the cache with this setting, though. Continuously adding data to the cache and never removing it will eventually cause memory or overflow issues.

A cache is typically used where it's acceptable for an item to no longer exist in the cache, in which case the value is retrieved again from persistent storage (a database or file, for example).

In your case, it sounds like your code requires the item to exist, which may suggest looking for another approach (a static ConcurrentDictionary as mentioned in the comments, for example).

Moramorabito answered 1/12, 2017 at 20:0 Comment(1)
CacheItemPolicy only affects whether or not it is purged during eviction from memory pressure; not expiration.Dropping

© 2022 - 2024 — McMap. All rights reserved.