public void Test_CacheableRequest_TtlZeroCacheDirective() { var target = new MyService(); var request = new CacheableRequest(); var response = new object(); var directive = new ResponseCachingDirective(true, TimeSpan.Zero, ResponseCachingSite.Server); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); Assert.IsTrue(invocation.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); // check that response was not cached var cache = new TestCacheClient(); var cacheEntry = cache.Get(request.GetCacheKey(), new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
protected async ValueTask <ActionResult <TResult> > ExecuteRequest <TResult>(CacheableRequest <TResult> request) { HttpContext.AddCachePrefernces(request); var result = await ExecuteRequestImpl(request) .ConfigureAwait(false); HttpContext.AddFromCacheHeader(request); return(result); }
public void Test_CacheableRequest_TypicalCacheDirective() { var target = new MyService(); var request = new CacheableRequest(); var response = new object(); var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var invocation = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); // check invocation proceeded and return value set Assert.IsTrue(invocation.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); // check that response was cached var cache = new TestCacheClient(); var cacheEntry = cache.Get(request.GetCacheKey(), new CacheGetOptions("")); Assert.AreEqual(response, cacheEntry); // check that it was cached in the correct region var region = cache.GetRegion(request.GetCacheKey()); Assert.AreEqual(typeof(MyService).FullName + ".MyServiceOperation", region); // second invocation var invocation2 = new TestInvocation { Target = target, Method = target.GetType().GetMethod("MyServiceOperation"), Request = request, Response = response }; // check 2nd invocation did not proceed, but return value is still set correctly from cache Assert.IsFalse(invocation2.DidProceed); Assert.AreEqual(invocation.ReturnValue, response); }