Ejemplo n.º 1
0
        public void SlidingExpirationTest()
        {
            NFileCache      target = new NFileCache("SlidingExpirationTest");
            CacheItemPolicy policy = new CacheItemPolicy();

            // Add an item and have it expire 500 ms from now
            policy.SlidingExpiration = new TimeSpan(0, 0, 0, 0, 500);
            target.Set("test", "test", policy);

            // Sleep for 200
            Thread.Sleep(200);

            // Then try to access the item
            object result = target.Get("test");

            Assert.AreEqual("test", result);

            // Sleep for another 350
            Thread.Sleep(350);

            // Then try to access the item
            result = target.Get("test");
            Assert.AreEqual("test", result);

            // Then sleep for more than 500 ms. Should be gone
            Thread.Sleep(600);
            result = target.Get("test");
            Assert.IsNull(result);
        }
Ejemplo n.º 2
0
        public void CustomObjectSaveTest()
        {
            NFileCache target = new NFileCache("CustomObjectSaveTest");

            // Create custom object
            CustomObjB custom = new CustomObjB
            {
                Num = 5,
                Obj = new CustomObjA
                {
                    Name = "test"
                }
            };

            CacheItem item = new CacheItem("foo")
            {
                Value      = custom,
                RegionName = "foobar"
            };

            // Set it
            target.Set(item, new CacheItemPolicy());

            // Now get it back
            CacheItem fromCache = target.GetCacheItem("foo", "foobar");

            // Pulling twice increases code coverage
            fromCache = target.GetCacheItem("foo", "foobar");
            custom    = fromCache.Value as CustomObjB;

            Assert.IsNotNull(custom);
            Assert.IsNotNull(custom.Obj);
            Assert.AreEqual(custom.Num, 5);
            Assert.AreEqual(custom.Obj.Name, "test");
        }
Ejemplo n.º 3
0
        public void PolicySaveTest()
        {
            NFileCache      target = new NFileCache("PolicySaveTest");
            CacheItemPolicy policy = new CacheItemPolicy();

            policy.SlidingExpiration = new TimeSpan(1, 0, 0, 0, 0);
            target.Set("sliding", "test", policy);

            CacheItemPolicy returnPolicy = target.GetPolicy("sliding");

            Assert.AreEqual(policy.SlidingExpiration, returnPolicy.SlidingExpiration);

            policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
            target.Set("absolute", "test", policy.AbsoluteExpiration);

            returnPolicy = target.GetPolicy("absolute");
            Assert.AreEqual(policy.AbsoluteExpiration, returnPolicy.AbsoluteExpiration);
        }
Ejemplo n.º 4
0
        public void AbsoluteExpirationTest()
        {
            NFileCache target = new NFileCache("AbsoluteExpirationTest");
            CacheItemPolicy policy = new CacheItemPolicy();

            // Add an item and have it expire yesterday
            policy.AbsoluteExpiration = DateTime.Now.AddDays(-1);
            target.Set("test", "test", policy);

            // Then try to access the item
            object result = target.Get("test");
            Assert.IsNull(result);
        }
Ejemplo n.º 5
0
        public void AbsoluteExpirationTest()
        {
            NFileCache      target = new NFileCache("AbsoluteExpirationTest");
            CacheItemPolicy policy = new CacheItemPolicy();

            // Add an item and have it expire yesterday
            policy.AbsoluteExpiration = DateTime.Now.AddDays(-1);
            target.Set("test", "test", policy);

            // Then try to access the item
            object result = target.Get("test");

            Assert.IsNull(result);
        }
Ejemplo n.º 6
0
        public void RemoveTest()
        {
            NFileCache target = new NFileCache("RemoveTest");

            target.Set("test", "test", DateTime.Now.AddDays(3));
            object result = target.Get("test");

            Assert.AreEqual("test", result);

            // Check file system to be sure item was created
            string itemPath = target.GetItemPath("test");

            Assert.IsTrue(File.Exists(itemPath));

            // Now delete
            target.Remove("test");
            result = target["test"];
            Assert.IsNull(result);

            // Check file system to be sure item was removed
            Assert.IsFalse(File.Exists(itemPath));
        }
Ejemplo n.º 7
0
        public void SlidingExpirationTest()
        {
            NFileCache target = new NFileCache("SlidingExpirationTest");
            CacheItemPolicy policy = new CacheItemPolicy();

            // Add an item and have it expire 500 ms from now
            policy.SlidingExpiration = new TimeSpan(0, 0, 0, 0, 500);
            target.Set("test", "test", policy);

            // Sleep for 200
            Thread.Sleep(200);

            // Then try to access the item
            object result = target.Get("test");
            Assert.AreEqual("test", result);

            // Sleep for another 350
            Thread.Sleep(350);

            // Then try to access the item
            result = target.Get("test");
            Assert.AreEqual("test", result);

            // Then sleep for more than 500 ms. Should be gone
            Thread.Sleep(600);
            result = target.Get("test");
            Assert.IsNull(result);
        }
Ejemplo n.º 8
0
        public void RemoveTest()
        {
            NFileCache target = new NFileCache("RemoveTest");
            target.Set("test", "test", DateTime.Now.AddDays(3));
            object result = target.Get("test");
            Assert.AreEqual("test", result);

            // Check file system to be sure item was created
            string itemPath = target.GetItemPath("test");
            Assert.IsTrue(File.Exists(itemPath));

            // Now delete
            target.Remove("test");
            result = target["test"];
            Assert.IsNull(result);

            // Check file system to be sure item was removed
            Assert.IsFalse(File.Exists(itemPath));
        }
Ejemplo n.º 9
0
        public void PolicySaveTest()
        {
            NFileCache target = new NFileCache("PolicySaveTest");
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.SlidingExpiration = new TimeSpan(1, 0, 0, 0, 0);
            target.Set("sliding", "test", policy);

            CacheItemPolicy returnPolicy = target.GetPolicy("sliding");
            Assert.AreEqual(policy.SlidingExpiration, returnPolicy.SlidingExpiration);

            policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
            target.Set("absolute", "test", policy.AbsoluteExpiration);

            returnPolicy = target.GetPolicy("absolute");
            Assert.AreEqual(policy.AbsoluteExpiration, returnPolicy.AbsoluteExpiration);
        }
Ejemplo n.º 10
0
        public void CustomObjectSaveTest()
        {
            NFileCache target = new NFileCache("CustomObjectSaveTest");

            // Create custom object
            CustomObjB custom = new CustomObjB
            {
                Num = 5,
                Obj = new CustomObjA
                {
                    Name = "test"
                }
            };

            CacheItem item = new CacheItem("foo")
            {
                Value = custom,
                RegionName = "foobar"
            };

            // Set it
            target.Set(item, new CacheItemPolicy());

            // Now get it back
            CacheItem fromCache = target.GetCacheItem("foo", "foobar");

            // Pulling twice increases code coverage
            fromCache = target.GetCacheItem("foo", "foobar");
            custom = fromCache.Value as CustomObjB;

            Assert.IsNotNull(custom);
            Assert.IsNotNull(custom.Obj);
            Assert.AreEqual(custom.Num, 5);
            Assert.AreEqual(custom.Obj.Name, "test");
        }