public void TokenExpires_LinkedEntry() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; using (var entry = cache.CreateEntry(key)) { entry.SetValue(obj); cache.Set(key1, obj, new MemoryCacheEntryOptions().AddExpirationToken(expirationToken)); } Assert.Same(obj, cache.Get(key)); Assert.Same(obj, cache.Get(key1)); expirationToken.Fire(); Assert.False(cache.TryGetValue(key1, out object value)); Assert.False(cache.TryGetValue(key, out value)); }
public void FireTokenRemovesItem() { var cache = CreateCache(); string key = "myKey"; var value = new object(); var callbackInvoked = new ManualResetEvent(false); var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; cache.Set(key, value, new MemoryCacheEntryOptions() .AddExpirationToken(expirationToken) .RegisterPostEvictionCallback((subkey, subValue, reason, state) => { // TODO: Verify params var localCallbackInvoked = (ManualResetEvent)state; localCallbackInvoked.Set(); }, state: callbackInvoked)); expirationToken.Fire(); var found = cache.TryGetValue(key, out value); Assert.False(found); Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)), "Callback"); }
public void TokenExpires_ParentScopeEntry_WithFactory() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; cache.GetOrCreate(key, entry => { cache.GetOrCreate(key1, entry1 => { entry1.AddExpirationToken(expirationToken); return(obj); }); return(obj); }); Assert.Same(obj, cache.Get(key)); Assert.Same(obj, cache.Get(key1)); expirationToken.Fire(); Assert.False(cache.TryGetValue(key1, out object value)); Assert.False(cache.TryGetValue(key, out value)); }
public void TokenExpires_ParentScopeEntry(bool trackLinkedCacheEntries) { var cache = CreateCache(trackLinkedCacheEntries); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; using (var entry = cache.CreateEntry(key)) { entry.SetValue(obj); using (var entry1 = cache.CreateEntry(key1)) { entry1.SetValue(obj); entry1.AddExpirationToken(expirationToken); } } Assert.Same(obj, cache.Get(key)); Assert.Same(obj, cache.Get(key1)); expirationToken.Fire(); Assert.False(cache.TryGetValue(key1, out object value)); Assert.Equal(!trackLinkedCacheEntries, cache.TryGetValue(key, out value)); }
public void TokenExpires_GetInLinkedEntry() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; cache.GetOrCreate(key1, e => { e.AddExpirationToken(expirationToken); return(obj); }); using (var entry = cache.CreateEntry(key)) { entry.SetValue(cache.Get(key1)); } Assert.Same(obj, cache.Get(key)); Assert.Same(obj, cache.Get(key1)); expirationToken.Fire(); object value; Assert.False(cache.TryGetValue(key1, out value)); Assert.False(cache.TryGetValue(key, out value)); }
public void TokenDoesntExpire_SiblingScopeEntry() { var cache = CreateCache(); var obj = new object(); string key = "myKey"; string key1 = "myKey1"; string key2 = "myKey2"; var expirationToken = new TestExpirationToken() { ActiveChangeCallbacks = true }; using (var entry = cache.CreateEntry(key)) { entry.SetValue(obj); using (var entry1 = cache.CreateEntry(key1)) { entry1.SetValue(obj); entry1.AddExpirationToken(expirationToken); } using (var entry2 = cache.CreateEntry(key2)) { entry2.SetValue(obj); } } Assert.Same(obj, cache.Get(key)); Assert.Same(obj, cache.Get(key1)); Assert.Same(obj, cache.Get(key2)); expirationToken.Fire(); object value; Assert.False(cache.TryGetValue(key1, out value)); Assert.False(cache.TryGetValue(key, out value)); Assert.True(cache.TryGetValue(key2, out value)); }
public async Task ExpiringEntryDecreasesCacheSize() { var cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.Zero, SizeLimit = 10 }); var entryOptions = new MemoryCacheEntryOptions { Size = 5 }; var changeToken = new TestExpirationToken(); var sem = new SemaphoreSlim(0, 1); entryOptions.ExpirationTokens.Add(changeToken); entryOptions.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration { EvictionCallback = (k, v, r, s) => sem.Release(), State = null }); cache.Set("key", "value", entryOptions); Assert.Equal(5, cache.Size); // Expire entry changeToken.Fire(); // Trigger compaction Assert.Null(cache.Get("key")); // Wait for compaction to complete Assert.True(await sem.WaitAsync(TimeSpan.FromSeconds(10))); Assert.Equal(0, cache.Size); }