One of the most useful caching configurations I use is a sliding expiration. This means that as long as an item in the cache is requested frequently, the item remains in the cache. This gives you a good balance between cache size and performance.
To configure this on the .NET MemoryCache you can use the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slidingExpirationPolicy = new CacheItemPolicy() | |
{ | |
SlidingExpiration = this.ExpirationTime | |
}; | |
object[] cacheValue = new object[] { value }; | |
MemoryCache.Default.Add( | |
key, | |
cacheValue, | |
slidingExpirationPolicy); |