I'm having a difficult time using the CacheEntryUpdateCallback delegate of the System.Runtime.Caching library. Whenever I define and set the callback, I get an ArgumentException that the "CacheItemUpdateCallback must be null". Why must it be null? I should be able to set this and then get the callback.
I do not get this when using the CacheEntryRemovedCallback delegate. I can reliably reproduce this in all of my projects. Am I doing something wrong? Here's a small sample application:
using System.Runtime.Caching;
class Program {
static void Main(string[] args) {
var policy = new CacheItemPolicy();
policy.SlidingExpiration = TimeSpan.FromSeconds(10);
// this works
//policy.RemovedCallback = Removed;
// this creates the exception
policy.UpdateCallback = Update;
MemoryCache.Default.Add("test", "123", policy);
Console.Read();
}
static void Update(CacheEntryUpdateArguments arguments) { }
static void Removed(CacheEntryRemovedArugments arguments) { }
}
Runtime caching refers to gradually adding responses to a cache "as you go". While runtime caching doesn't help with the reliability of the current request, it can help make future requests for the same URL more reliable.
In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.
According to documentation you should be using Set
instead of Add
.
MemoryCache.Add:
The
Add
andAddOrGetExisting
method overloads do not support the UpdateCallback property. Therefore, to set the UpdateCallback property for a cache entry, use theSet
method overloads instead.
Following indeed work without problems:
MemoryCache.Default.Set("test", "123", policy);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With