public void FireTriggerRemovesItem() { var cache = CreateCache(); string key = "myKey"; var value = new object(); var callbackInvoked = new ManualResetEvent(false); var trigger = new TestTrigger() { ActiveExpirationCallbacks = true }; cache.Set(key, value, new MemoryCacheEntryOptions() .AddExpirationTrigger(trigger) .RegisterPostEvictionCallback((subkey, subValue, reason, state) => { // TODO: Verify params var localCallbackInvoked = (ManualResetEvent)state; localCallbackInvoked.Set(); }, state: callbackInvoked)); trigger.Fire(); var found = cache.TryGetValue(key, out value); Assert.False(found); Assert.True(callbackInvoked.WaitOne(100), "Callback"); }
public void TriggerExpires_LinkedEntry() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var trigger = new TestTrigger() { ActiveExpirationCallbacks = true }; using (var link = cache.CreateLinkingScope()) { cache.Set(key, obj, new MemoryCacheEntryOptions().AddExpirationTrigger(trigger)); cache.Set(key1, obj, new MemoryCacheEntryOptions().AddEntryLink(link)); } Assert.StrictEqual(obj, cache.Get(key)); Assert.StrictEqual(obj, cache.Get(key1)); trigger.Fire(); object value; Assert.False(cache.TryGetValue(key1, out value)); Assert.False(cache.TryGetValue(key, out value)); }
public void TriggerExpiresLinkedEntry() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var link = new EntryLink(); var trigger = new TestTrigger() { ActiveExpirationCallbacks = true }; cache.Set(key, link, context => { context.AddExpirationTrigger(trigger); return(obj); }); cache.Set(key1, context => { context.AddEntryLink(link); return(obj); }); Assert.StrictEqual(obj, cache.Get(key)); Assert.StrictEqual(obj, cache.Get(key1)); trigger.Fire(); object value; Assert.False(cache.TryGetValue(key1, out value)); Assert.False(cache.TryGetValue(key, out value)); }