Beispiel #1
0
        /// <summary>
        /// Updates a caching strategy to use the specified sliding expiration expiration callback
        /// </summary>
        public static CacheStrategyAsync <TResult> ExpireAfter <TResult>(this CacheStrategyAsync <TResult> source, Func <TResult, TimeSpan> expireAfterCallback)
        {
            Func <object, CacheExpiration> callback = obj => new CacheExpiration(expireAfterCallback((TResult)obj));

            source.ExpirationCallback = callback;
            return(source);
        }
        public async Task ErrorHandling_Async_NoErrorHandling()
        {
            CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.ThrowsExceptionImmediatelyAsync())
                                                     .IfRetrievalFailsUsePreviousValue();

            await cacheStrategy.GetValueAsync();
        }
Beispiel #3
0
        public async Task Method_Async_GetValue_NotDefault(Cache <Example> cache)
        {
            CacheStrategyAsync <double> strategy = cache.Method(c => c.CalculateSomeWorkAsync(3));

            double value = await strategy.GetValueAsync();

            Assert.AreNotEqual(0d, value);
        }
Beispiel #4
0
        public async Task CacheFailureOnSet_Async_Unhandled()
        {
            ICache <CacheMe> cache = CreateUnhandledCache(CacheOperation.Set);

            CacheStrategyAsync <double> cacheStrategy = cache.Method(m => m.DoWorkAsync());

            ICachedValue <double> result = await cacheStrategy.GetAsync();
        }
Beispiel #5
0
        public async Task Method_Async_Parameters(Cache <Example> cache)
        {
            CacheStrategyAsync <double> strategy = cache.Method(c => c.CalculateSomeWorkAsync(3));

            CachedValue <double> result1 = await strategy.GetAsync();

            CachedValue <double> result2 = await strategy.GetAsync();

            Assert.AreEqual(result1.Version, result2.Version);
        }
Beispiel #6
0
        public async Task CacheMethod_Method_GetValueAsync()
        {
            var cache = CreateCache();

            CacheStrategyAsync <string> strat = cache.Method(t => t.DownloadTextAsync(t.TestProperty));

            string result = await strat.GetValueAsync();

            Assert.AreNotEqual(null, result);
        }
Beispiel #7
0
        public async Task CacheMethod_Method_GetValueAsync_LocalArgument()
        {
            var cache = CreateCache();
            var arg   = new MethodTestsArguments().Property;

            CacheStrategyAsync <string> strat = cache.Method(t => t.MethodAsync(arg));

            string result = await strat.GetValueAsync();

            Assert.AreNotEqual(null, result);
        }
        public async Task ErrorHandling_Async_UsePreviousValueOrDefault()
        {
            int defaultValue = -1111;

            CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.ThrowsExceptionImmediatelyAsync())
                                                     .IfRetrievalFailsUsePreviousValueOrDefault(defaultValue);

            int value = await cacheStrategy.GetValueAsync();

            Assert.AreEqual(defaultValue, value, "The retrieval error handler should have kicked in and returned the default value");
        }
Beispiel #9
0
        public async Task CacheFailureOnSet_Async_Handled()
        {
            ICache <CacheMe> cache = CreateHandledCache(CacheOperation.Set);

            CacheStrategyAsync <double> cacheStrategy = cache.Method(m => m.DoWorkAsync());

            ICachedValue <double> result1 = await cacheStrategy.GetAsync();

            ICachedValue <double> result2 = await cacheStrategy.GetAsync();

            Assert.AreNotEqual(result1.Value, result2.Value);
        }
Beispiel #10
0
        /// <summary>
        /// Validates the item was cached after the specified date
        /// </summary>
        public static CacheStrategyAsync <T> ValidateCachedAfter <T>(this CacheStrategyAsync <T> source, DateTime afterDate)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            else if (afterDate.Kind != DateTimeKind.Utc)
            {
                throw new ArgumentException("afterDate must be in UTC", "afterDate");
            }

            return(source.InvalidateIf(c => c.CachedDate > afterDate));
        }
        public async Task ErrorHandling_Async_UsePreviousValue()
        {
            bool isInvalid = false;
            Func <CachedValue <double>, CacheValidationResult> validate = val => isInvalid ? CacheValidationResult.Invalid : CacheValidationResult.Unknown;

            CacheStrategyAsync <int> cacheStrategy = CreateCache().Method(c => c.RandomValueThatThrowsExceptionOnSecondTryAsync())
                                                     .IfRetrievalFailsUsePreviousValue();

            int previousValue = await cacheStrategy.GetValueAsync();

            isInvalid = true;   //force a second retrieval by invalidating the previous value

            int newValue = await cacheStrategy.GetValueAsync();

            Assert.AreEqual(previousValue, newValue, "The retrieval error handler should have kicked in");
        }
Beispiel #12
0
        public async Task CacheMethod_Method_GetValue_IgnoreParameter()
        {
            string url  = "http://www.google.com";
            string url2 = "http://www.cnn.com";

            var cache = CreateCache();

            CacheStrategyAsync <string> strat1 = cache.Method(t => t.DownloadTextAsync(Parameter.DoNotCache(url)));

            CacheStrategyAsync <string> strat2 = cache.Method(t => t.DownloadTextAsync(Parameter.DoNotCache(url2)));

            Assert.AreEqual(strat1.Key, strat2.Key, "Both strategies should have the same key because the parameter should be ignored");

            string result = await strat1.GetValueAsync();

            strat1.ClearValue();

            string result2 = await strat2.GetValueAsync();

            Assert.AreNotEqual(result, result2, "These should have returned different results because the parameter values were different");
        }
Beispiel #13
0
        public async Task CacheMethod_Method_GetValue_IgnoreParameter()
        {
            string arg1 = "hello";
            string arg2 = "world";

            var cache = CreateCache();

            CacheStrategyAsync <string> strat1 = cache.Method(t => t.MethodAsync(Parameter.DoNotCache(arg1)));

            CacheStrategyAsync <string> strat2 = cache.Method(t => t.MethodAsync(Parameter.DoNotCache(arg2)));

            Assert.AreEqual(strat1.Key, strat2.Key, "Both strategies should have the same key because the parameter should be ignored");

            string result = await strat1.GetValueAsync();

            strat1.ClearValue();

            string result2 = await strat2.GetValueAsync();

            Assert.AreNotEqual(result, result2, "These should have returned different results because the parameter values were different");
        }
Beispiel #14
0
 /// <summary>
 /// Use the previous cached value (or a default value if there is no previous value) if the retrieval mechanism failed
 /// </summary>
 public static CacheStrategyAsync <T> IfRetrievalFailsUsePreviousValueOrDefault <T>(this CacheStrategyAsync <T> source, T defaultValue)
 {
     return(source.IfRetrievalFails((ex, cachedValue) => RetrievalErrorHandlerResult <T> .UsePreviousCachedValueOrDefault(ex, cachedValue, defaultValue)));
 }
Beispiel #15
0
 /// <summary>
 /// Use the previous cached value if the retrieval mechanism failed
 /// </summary>
 public static CacheStrategyAsync <T> IfRetrievalFailsUsePreviousValue <T>(this CacheStrategyAsync <T> source)
 {
     return(source.IfRetrievalFails(RetrievalErrorHandlerResult <T> .UsePreviousCachedValue));
 }