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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }