public void MultiplesDeps_WhenChangeMonitor_WhenInvalidation_ShouldRemoved() { var baseCacheKey = fixture.Create <string>(); var invalidationKey = fixture.Create <string>(); var monitor1 = InvalidationManager.CreateChangeMonitor(invalidationKey); var monitor2 = InvalidationManager.CreateChangeMonitor(invalidationKey); CreateCacheItemAndAdd(localCache, baseCacheKey + "1", monitor1); CreateCacheItemAndAdd(localCache, baseCacheKey + "2", monitor2); Assert.Equal(2, localCache.GetCount()); Assert.False(monitor1.IsDisposed, "should not be removed before notification"); Assert.False(monitor2.IsDisposed, "should not be removed before notification"); //act var subscriber = redis.GetSubscriber(); subscriber.Publish(Constants.DEFAULT_INVALIDATION_CHANNEL, Encoding.Default.GetBytes(invalidationKey)); // hack wait for notification Thread.Sleep(50); //assert Assert.False(localCache.Contains(baseCacheKey + "1"), "cache item should be removed"); Assert.False(localCache.Contains(baseCacheKey + "2"), "cache item should be removed"); Assert.True(monitor1.IsDisposed, "should be disposed"); Assert.True(monitor2.IsDisposed, "should be disposed"); }
public void CreateChangeMonitorBadArgs_ShouldThrowException() { Assert.Throws <InvalidOperationException>(() => { InvalidationManager.CreateChangeMonitor("rzer"); }); Assert.Throws <InvalidOperationException>(() => { InvalidationManager.CreateChangeMonitor(new CacheItem("rzesdqr")); }); Assert.Throws <InvalidOperationException>(() => { InvalidationManager.InvalidateAsync("rzaaer"); }); Assert.Throws <ArgumentNullException>(() => { InvalidationManager.CreateChangeMonitor((string)null); }); Assert.Throws <ArgumentNullException>(() => { InvalidationManager.CreateChangeMonitor((CacheItem)null); }); }
public ActionResult Index() { var cache = MemoryCache.Default; var cacheItem = new CacheItem("onekey", DateTime.Now); if (!cache.Contains(cacheItem.Key)) { var policy = new CacheItemPolicy(); policy.ChangeMonitors.Add(InvalidationManager.CreateChangeMonitor(parentkey)); policy.AbsoluteExpiration = DateTime.Now.AddYears(1); // just to create not expirable item MemoryCache.Default.Add(cacheItem, policy); } DateTime dt = (DateTime)cache[cacheItem.Key]; ViewBag.Message = string.Format("'{0}' was set at {1}", cacheItem.Key, dt.ToLongTimeString()); return(View()); }
public void MultiplesDeps_WhenSpaceNotification_ShouldBeRemoved() { var baseCacheKey = fixture.Create <string>(); var invalidationKey = fixture.Create <string>(); var monitor1 = InvalidationManager.CreateChangeMonitor(invalidationKey); var monitor2 = InvalidationManager.CreateChangeMonitor(invalidationKey); CreateCacheItemAndAdd(localCache, baseCacheKey + "1", monitor1); CreateCacheItemAndAdd(localCache, baseCacheKey + "2", monitor2); // act var db = redis.GetDatabase(0); db.StringSet(invalidationKey, "notused"); Thread.Sleep(200); //assert Assert.Equal(0, localCache.GetCount()); Assert.False(localCache.Contains(baseCacheKey + "1"), "cache item should be removed"); Assert.False(localCache.Contains(baseCacheKey + "2"), "cache item should be removed"); }