Exemple #1
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfAbsoluteTimeExpired()
        {
            var cachedltm = new CachedLifetime()
                .ExpiresOn(DateTime.UtcNow.AddSeconds(2));

            var container = new Container();

            var ireg = container.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            var result1 = container.Resolve<IFoo>();
            var result2 = container.Resolve<IFoo>();

            // simulate expiry
            Thread.Sleep(3000);

            var result3 = container.Resolve<IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.IsNotNull(result3);

            Assert.AreSame(result1, result2);       // cache not expired
            Assert.AreNotSame(result2, result3);    // cache expired
        }
Exemple #2
0
        public void CallbackIsCalledWhenItemRemovedFromCache()
        {
            var cachedltm = new CachedLifetime()
                .ExpiresAfterNotAccessedFor(new TimeSpan(0, 0, 2))
                .CallbackOnRemoval(RemovedCallback);

            var container = new Container();

            var ireg = container.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            itemRemoved = false;
            var result1 = container.Resolve<IFoo>();

            // simulate expiry
            Thread.Sleep(3000);
            var result2 = container.Resolve<IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.AreNotSame(result1, result2);
            Assert.IsTrue(itemRemoved);
        }
Exemple #3
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfSlidingTimeExpired()
        {
            var cachedltm = new CachedLifetime()
                .ExpiresAfterNotAccessedFor(new TimeSpan(0,0,2));

            var container = new Container();

            var ireg = container.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            var result1 = container.Resolve<IFoo>();
            var result2 = container.Resolve<IFoo>();

            // simulate expiry
            Thread.Sleep(3000);

            var result3 = container.Resolve<IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.IsNotNull(result3);

            Assert.AreSame(result1, result2);       // cache not expired
            Assert.AreNotSame(result2, result3);    // cache expired
        }
Exemple #4
0
        public void CachedLifetimeManagerReturnsSameObjectIfCacheNotExpired()
        {
            var cachedltm = new CachedLifetime();

            var container = new Container();
            container.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            var result1 = container.Resolve<IFoo>();
            var result2 = container.Resolve<IFoo>();

            var result3 = container.Resolve<IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.IsNotNull(result3);

            Assert.AreSame(result1, result2);       // same request
            Assert.AreSame(result2, result3);    // different request
        }
Exemple #5
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfCacheExpired()
        {
            var cachedltm = new CachedLifetime();

            var container = new Container();
            var ireg = container.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            var result1 = container.Resolve<IFoo>();
            var result2 = container.Resolve<IFoo>();

            // simulate expiry
            HttpRuntime.Cache.Remove(ireg.Id);

            var result3 = container.Resolve<IFoo>();

            Assert.IsNotNull(result1);
            Assert.IsNotNull(result2);
            Assert.IsNotNull(result3);

            Assert.AreSame(result1, result2);       // cache not expired
            Assert.AreNotSame(result2, result3);    // cache expired
        }