Пример #1
0
        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 void WhenNotConnected_ShouldNotPublishMessages()
        {
            //test more disconnected scenarios
            InvalidationManager.Configure("blabblou", new InvalidationSettings());

            var published = InvalidationManager.InvalidateAsync("mykey").Result;

            Assert.Equal(0L, published);
        }
        public void InvalidHost_ShouldNotBeConnected()
        {
            //test more disconnected scenarios
            InvalidationManager.Configure("blabblou", new InvalidationSettings());
            Assert.False(InvalidationManager.IsConnected);

            var published = InvalidationManager.InvalidateAsync("mykey").Result;

            Assert.Equal(0L, published);
        }
Пример #5
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //add somewhere else
            InvalidationManager.Configure("localhost:6379", new InvalidationSettings());
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Invalidation Emitter");

            InvalidationManager.Configure("localhost:6379", new InvalidationSettings());

            Console.WriteLine("IsConnected : " + InvalidationManager.IsConnected);

            Console.WriteLine("enter a key to send invalidation (default is 'mynotifmessage'): ");
            var key  = Console.ReadLine();
            var task = InvalidationManager.InvalidateAsync(string.IsNullOrEmpty(key) ? "mynotifmessage": key);

            Console.WriteLine("message send to {0} clients", task.Result);
            Console.ReadLine();
        }
Пример #7
0
        public InvalidationTests(RedisServerFixture redisServer)
        {
            localCache = new MemoryCache(Guid.NewGuid().ToString());
            localCache.Trim(100);

            redisServer.Reset();
            redis = redisServer;

            InvalidationManager.NotificationBus = null;
            InvalidationManager.Configure(redis.GetEndpoint(), new InvalidationSettings {
                InvalidationStrategy        = InvalidationStrategyType.All,
                EnableKeySpaceNotifications = true,
                TargetCache = localCache
            });

            fixture = new Fixture();
        }
Пример #8
0
        public void MultiplesDeps_WhenImplicitRemoval_WhenInvalidation_ShouldRemoved()
        {
            var baseCacheKey = fixture.Create <string>();

            CreateCacheItemAndAdd(localCache, baseCacheKey + "1");
            CreateCacheItemAndAdd(localCache, baseCacheKey + "2");

            Assert.Equal(2, localCache.GetCount());

            // act
            InvalidationManager.InvalidateAsync(baseCacheKey + "1").Wait();
            InvalidationManager.InvalidateAsync(baseCacheKey + "2").Wait();

            Thread.Sleep(50);

            //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");
        }
Пример #9
0
        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());
        }
Пример #10
0
        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");
        }
 public void Invalidate_WhenInvalid_ShouldPublishToRedis()
 {
     InvalidationManager.NotificationBus = this._mockOfBus.Object;
     InvalidationManager.InvalidateAsync("mykey");
     this._mockOfBus.Verify(b => b.NotifyAsync("mykey"), Times.Once);
 }
 public void Configure_WhenTwice_ShouldNotThrowException()
 {
     InvalidationManager.Configure("dfsdf", new InvalidationSettings());
     InvalidationManager.Configure("dfsdf", new InvalidationSettings());
 }
        public void Configure_WhenInvalid_ShouldThrowException()
        {
            InvalidationManager.Configure("dfsdf", new InvalidationSettings());

            Assert.False(InvalidationManager.IsConnected);
        }
Пример #14
0
        public ActionResult Invalidate()
        {
            InvalidationManager.InvalidateAsync(parentkey);

            return(RedirectToAction("Index"));
        }