Example #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);
        }
Example #2
0
        public void AutoShrinkTest()
        {
            NFileCache target = new NFileCache("AutoShrinkTest");

            target.DefaultPolicy = new CacheItemPolicy {
                SlidingExpiration = TimeSpan.FromMinutes(1D)
            };

            target.MaxCacheSize  = 20000;
            target.CacheResized += (object sender, FileCacheEventArgs args) =>
            {
                Assert.IsNotNull(target["foo10"]);
                Assert.IsNotNull(target["foo40"]);
            };

            for (int i = 0; i < 100; i++)
            {
                target["foo" + i] = "bar";

                // Test to make sure it leaves items that have been recently accessed.
                if (i % 5 == 0 && i != 0)
                {
                    var foo10 = target.Get("foo10");
                    var foo40 = target.Get("foo40");
                }
            }
        }
Example #3
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);
        }
Example #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);
        }
Example #5
0
        public void DefaultRegionTest()
        {
            NFileCache cacheWithDefaultRegion = new NFileCache("DefaultRegionTest");

            cacheWithDefaultRegion.DefaultRegion = "foo";

            NFileCache defaultCache = new NFileCache("DefaultRegionTest");

            cacheWithDefaultRegion["foo"] = "bar";

            string pull = defaultCache.Get("foo", "foo") as string;

            Assert.AreEqual("bar", pull);
        }
Example #6
0
        public void TestCount()
        {
            NFileCache target = new NFileCache("TestCount");

            target["test"] = "test";
            target["test"] = "bar";

            Assert.AreEqual(0, target.GetCount("bar"));

            object result = target.Get("test");

            Assert.AreEqual("bar", result);
            Assert.AreEqual(1, target.GetCount());
        }
Example #7
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));
        }
Example #8
0
        public void AutoShrinkTest()
        {
            NFileCache target = new NFileCache("AutoShrinkTest");
            target.DefaultPolicy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(1D) };

            target.MaxCacheSize = 20000;
            target.CacheResized += (object sender, FileCacheEventArgs args) =>
            {
                Assert.IsNotNull(target["foo10"]);
                Assert.IsNotNull(target["foo40"]);
            };

            for (int i = 0; i < 100; i++)
            {
                target["foo" + i] = "bar";

                // Test to make sure it leaves items that have been recently accessed.
                if (i % 5 == 0 && i != 0)
                {
                    var foo10 = target.Get("foo10");
                    var foo40 = target.Get("foo40");
                }
            }
        }
Example #9
0
        public void TestCount()
        {
            NFileCache target = new NFileCache("TestCount");

            target["test"] = "test";
            target["test"] = "bar";

            Assert.AreEqual(0, target.GetCount("bar"));

            object result = target.Get("test");
            Assert.AreEqual("bar", result);
            Assert.AreEqual(1, target.GetCount());
        }
Example #10
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);
        }
Example #11
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));
        }
Example #12
0
        public void DefaultRegionTest()
        {
            NFileCache cacheWithDefaultRegion = new NFileCache("DefaultRegionTest");
            cacheWithDefaultRegion.DefaultRegion = "foo";

            NFileCache defaultCache = new NFileCache("DefaultRegionTest");
            cacheWithDefaultRegion["foo"] = "bar";

            string pull = defaultCache.Get("foo", "foo") as string;
            Assert.AreEqual("bar", pull);
        }