예제 #1
0
        public async Task Method_ExpireAfter_VersionReset(Cache <Example> cache)
        {
            DateTime now  = DateTime.UtcNow;
            TimeSpan wait = TimeSpan.FromSeconds(.5);

            CacheStrategy <double> strategy = cache.Method(c => c.CalculateSomeWork())
                                              .ExpireAfter(wait);

            strategy.ClearValue();

            CachedValue <double> result0 = strategy.Get();

            await Task.Delay(wait + wait);

            CachedValue <double> result1 = strategy.Get();

            Assert.AreEqual(0, result0.Version, "The first call should be for a new value with version 0");
            Assert.AreNotEqual(result0.CachedDate, result1.CachedDate, "The 2nd call should have expired and caused a new item to be inserted");
            Assert.AreEqual(0, result1.Version, "The 2nd call should have expired. Expired items are removed, so we don't expect the version to have incremented");
        }
예제 #2
0
        public void CacheMethod_Method_ClearCache()
        {
            var cache = CreateCache();

            int           calculationCount = 0;
            Func <double> doSomeWork       = () =>
            {
                double result = calculationCount > 0 ? 0 : Math.Sqrt(100000);
                calculationCount++;
                return(result);
            };

            CacheStrategy <double> cacheStrategy = cache.Method(t => t.CalculateSomeWork())
                                                   .RetrieveUsing(doSomeWork);

            Assert.AreNotEqual(default(double), cacheStrategy.GetValue());
            Assert.AreEqual(1, calculationCount, "Should have executed doSomeWork() once");

            cacheStrategy.ClearValue();

            Assert.AreEqual(0, cacheStrategy.GetValue());
            Assert.AreEqual(2, calculationCount, "Should have executed doSomeWork() again");
        }