Exemplo n.º 1
0
        public void Populate_ForwardsToInnerCache()
        {
            CacheOptions options = CacheOptions.AbsoluteExpiration(DateTime.UtcNow);

            Populator populator = delegate(string key, out CacheOptions populatedOptions)
            {
                populatedOptions = options;
                Assert.AreEqual("key", key);
                return("value");
            };

            Expect.Call(mockInnerCache.Populate("partition:key", null))
            .Constraints(Is.Equal("partition:key"), Is.NotSame(populator))
            .Do((PopulateDelegate) delegate(string actualKey, Populator actualPopulator)
            {
                CacheOptions populatedOptions;
                object populatedValue = actualPopulator(actualKey, out populatedOptions);
                Assert.AreEqual("value", populatedValue);
                Assert.AreEqual(options, populatedOptions);
                return("value");
            });
            Mocks.ReplayAll();

            Assert.AreEqual("value", Cache.Populate("key", populator));
        }
Exemplo n.º 2
0
        public void CleanupPurgesExpiredEntries()
        {
            Cache.Set("key1", "value1", CacheOptions.AbsoluteExpiration(DateTime.UtcNow));
            Cache.Set("key2", "value2", CacheOptions.AbsoluteExpiration(DateTime.MinValue));
            Cache.Set("key3", "value3", CacheOptions.AbsoluteExpiration(DateTime.MaxValue));
            Cache.Set("key4", "value4", CacheOptions.SlidingExpiration(TimeSpan.Zero));
            Cache.Set("key5", "value5", CacheOptions.NoExpiration);
            Thread.Sleep(100); // Cause keys to expire

            Cache.Cleanup();

            // Note: Order of ContainsKey / Get matters because ContainsKey
            //       doesn't auto-expire values like Get does.
            Assert.IsFalse(Cache.ContainsKey("key1"));
            Assert.AreEqual(null, Cache.Get("key1"));

            Assert.IsFalse(Cache.ContainsKey("key2"));
            Assert.AreEqual(null, Cache.Get("key2"));

            Assert.IsTrue(Cache.ContainsKey("key3"));
            Assert.AreEqual("value3", Cache.Get("key3"));

            Assert.IsFalse(Cache.ContainsKey("key4"));
            Assert.AreEqual(null, Cache.Get("key4"));

            Assert.IsTrue(Cache.ContainsKey("key5"));
            Assert.AreEqual("value5", Cache.Get("key5"));
        }
Exemplo n.º 3
0
        public void AbsoluteExpirationFactory()
        {
            CacheOptions options = CacheOptions.AbsoluteExpiration(new DateTime(1970, 1, 3));

            Assert.AreEqual(new DateTime(1970, 1, 3), options.AbsoluteExpirationTime);
            Assert.IsNull(options.SlidingExpirationTimeSpan);
            Assert.AreEqual(0, options.CustomOptions.Count);
        }
Exemplo n.º 4
0
        public void Set_WithOptions_ForwardsToInnerCache()
        {
            CacheOptions options = CacheOptions.AbsoluteExpiration(DateTime.UtcNow);

            mockInnerCache.Set("partition:key", "value", options);
            Mocks.ReplayAll();

            Cache.Set("key", "value", options);
        }