예제 #1
0
        public MethodCacheStrategy <TResult> CreateCacheStrategy <T, TResult>(Cache <T> cache, Expression <Func <T, TResult> > method)
        {
            Func <T, TResult> compiledMethod = method.Compile();
            Func <TResult>    retrieve       = () =>
            {
                return(compiledMethod(cache.Source));
            };

            CacheStrategyIncomplete incompleteStrat = null;

            if (method.Body is MethodCallExpression methodCallExpression)
            {
                incompleteStrat = CreateCacheStrategyFromMethod(cache, methodCallExpression);
            }

            if (method.Body is MemberExpression memberExpression)
            {
                incompleteStrat = CreateCacheStrategyFromMember(cache, memberExpression);
            }

            if (incompleteStrat == null)
            {
                throw UnsupportedExpression(method.Body);
            }

            MethodCacheStrategy <TResult> methodStrat = new MethodCacheStrategy <TResult>(incompleteStrat.Cache, retrieve, incompleteStrat.BaseKey);

            methodStrat.CopyFrom(incompleteStrat);

            return(methodStrat);
        }
예제 #2
0
        public AsyncMethodCacheStrategy <TResult> CreateAsyncCacheStrategy <T, TResult>(Cache <T> cache, Expression <Func <T, Task <TResult> > > method)
        {
            Func <T, Task <TResult> > compiledRetrieve = method.Compile();
            Func <Task <TResult> >    retrieve         = () => compiledRetrieve(cache.Source);

            CacheStrategyIncomplete incompleteStrat = null;

            if (method.Body is MethodCallExpression methodCallExpression)
            {
                incompleteStrat = CreateCacheStrategyFromMethod(cache, methodCallExpression);
            }

            //Note: we purposely do not support MemberExpressions here because a task-returning property would be a very strange design...

            if (incompleteStrat == null)
            {
                throw UnsupportedExpression(method.Body);
            }

            AsyncMethodCacheStrategy <TResult> methodStrat = new AsyncMethodCacheStrategy <TResult>(incompleteStrat.Cache, retrieve, incompleteStrat.BaseKey);

            methodStrat.CopyFrom(incompleteStrat);

            return(methodStrat);
        }
예제 #3
0
        public void CacheMethod_Method_FieldParameter()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("DownloadTextAsync")
                                               .WithParameters(TestField);

            CacheStrategy cacheStrategy = cache.Method(t => t.DownloadTextAsync(t.TestField));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #4
0
        private void DoTestCacheMethod_KeyAndRegion_ArgumentParameter(string argument)
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithParameters(argument);

            CacheStrategy cacheStrategy = cache.Method(r => r.MethodAsync(argument));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #5
0
        private void DoTestCacheMethod_KeyAndRegion_ArgumentParameter(string url)
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("DownloadTextAsync")
                                               .WithParameters(url);

            CacheStrategy cacheStrategy = cache.Method(t => t.DownloadTextAsync(url));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #6
0
        public void CacheMethod_Method_StaticPropertyParameter()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithParameters(MethodTestsArguments.StaticProperty);

            CacheStrategy cacheStrategy = cache.Method(r => r.MethodAsync(MethodTestsArguments.StaticProperty));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #7
0
        public void CacheMethod_Method_IgnoreParameter()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("DownloadTextAsync")
                                               .WithRegion("MethodTests");

            CacheStrategy cacheStrategy = cache.Method(t => t.DownloadTextAsync(Parameter.DoNotCache <string>()));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
            Assert.AreEqual(expected.Region, cacheStrategy.Region);
        }
예제 #8
0
        public void CacheMethod_Member()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("TestProperty")
                                               .WithRegion("MethodTests");

            CacheStrategy cacheStrategy = cache.Method(t => t.TestProperty);

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
            Assert.AreEqual(expected.Region, cacheStrategy.Region);
        }
예제 #9
0
        public void CacheMethod_Member_Property()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("Property")
                                               .WithRegion(nameof(MethodTestsRepository));

            CacheStrategy cacheStrategy = cache.Method(r => r.Property);

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
            Assert.AreEqual(expected.Region, cacheStrategy.Region);
        }
예제 #10
0
        public void CacheMethod_Method_MethodParameter()
        {
            var args  = new MethodTestsArguments();
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithParameters(args.Method());

            CacheStrategy cacheStrategy = cache.Method(t => t.MethodAsync(args.Method()));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #11
0
        public void CacheMethod_Method_IgnoreParameter()
        {
            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithRegion(nameof(MethodTestsRepository));

            CacheStrategy cacheStrategy = cache.Method(r => r.MethodAsync(Parameter.DoNotCache <string>()));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
            Assert.AreEqual(expected.Region, cacheStrategy.Region);
        }
예제 #12
0
        public void CacheMethod_Method_ClosureParameter()
        {
            const string url = TestConstant;

            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("DownloadTextAsync")
                                               .WithParameters(url);

            CacheStrategy cacheStrategy = cache.Method(t => t.DownloadTextAsync(url));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #13
0
        public void CacheMethod_Method_ClosureParameter()
        {
            const string localParameter = MethodTestsArguments.Constant;

            var cache = CreateCache();

            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithParameters(localParameter);

            CacheStrategy cacheStrategy = cache.Method(t => t.MethodAsync(localParameter));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }
예제 #14
0
        public void CacheMethod_Method_LambdaParameter()
        {
            var cache = CreateCache();

            Func <string> testLambda = () => MethodTestsArguments.Constant;


            CacheStrategyIncomplete expected = cache.WithKey("MethodAsync")
                                               .WithParameters(testLambda());

            CacheStrategy cacheStrategy = cache.Method(t => t.MethodAsync(testLambda()));

            Assert.AreEqual(expected.Key, cacheStrategy.Key);
        }