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); }
public void TestCacheConfigurationDocument() { var cache = new TestCacheClient(); cache.ClearCache(); var documentKey = new ConfigurationDocumentKey("Test", new Version(1, 0), null, ""); var cacheKey = ((IDefinesCacheKey)documentKey).GetCacheKey(); var service = new TestConfigurationService(); object request = new GetConfigurationDocumentRequest(documentKey); object response = new GetConfigurationDocumentResponse(documentKey, DateTime.Now, DateTime.Now, "Test"); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("GetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); Assert.AreEqual(response, cacheEntry); request = new SetConfigurationDocumentRequest(documentKey, "Test"); response = new SetConfigurationDocumentResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("SetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
public void TestCacheListSettingsGroups() { var cache = new TestCacheClient(); cache.ClearCache(); var service = new TestConfigurationService(); object request = new ListSettingsGroupsRequest(); object response = new ListSettingsGroupsResponse(new List <SettingsGroupDescriptor>()); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("ListSettingsGroups", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); request = new ImportSettingsGroupRequest( new SettingsGroupDescriptor("Test", new Version(1, 0), "Test", "Test", true), new List <SettingsPropertyDescriptor>(new[] { new SettingsPropertyDescriptor("Test", "Test", "Test", SettingScope.User, "Test") })); response = new ImportSettingsGroupResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("ImportSettingsGroup", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
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); }
public void Test_NonCacheableRequest_TypicalCacheDirective() { var target = new MyService(); var request = new NonCacheableRequest(); 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 }; // a non-null cache directive on a non-cacheable request type should throw var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); }
/// <summary> /// Implemented by the subclass to cache the response, based on the specified caching directive. /// </summary> protected override void CacheResponse(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region, ResponseCachingDirective directive) { // if site is server (e.g. callee), put it in our cache if (directive.CacheSite == ResponseCachingSite.Server) { PutResponseInCache(invocation, cacheClient, cacheKey, region, directive); return; } // if site is client (e.g. caller), send directive to client if (directive.CacheSite == ResponseCachingSite.Client) { // check if we have an op context (eg we are running as a WCF service) // if not, then this is not applicable if (OperationContext.Current == null) return; // send cache directive to client via headers WriteCachingDirectiveHeaders(directive, OperationContext.Current); return; } }
public void TestCacheListSettingsGroups() { var cache = new TestCacheClient(); cache.ClearCache(); var service = new TestConfigurationService(); object request = new ListSettingsGroupsRequest(); object response = new ListSettingsGroupsResponse(new List<SettingsGroupDescriptor>()); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("ListSettingsGroups", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); request = new ImportSettingsGroupRequest( new SettingsGroupDescriptor("Test", new Version(1,0), "Test", "Test", true), new List<SettingsPropertyDescriptor>(new[]{new SettingsPropertyDescriptor("Test", "Test", "Test", SettingScope.User, "Test") })); response = new ImportSettingsGroupResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("ImportSettingsGroup", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get("ListSettingsGroups", new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
protected override void CacheResponse(IInvocation invocation, object request, ICacheClient cacheClient, string region, ResponseCachingDirective directive) { PutResponseInCache(invocation, request, cacheClient, region, directive); }
public void TestCacheConfigurationDocument() { var cache = new TestCacheClient(); cache.ClearCache(); var documentKey = new ConfigurationDocumentKey("Test", new Version(1, 0), null, ""); var cacheKey = ((IDefinesCacheKey) documentKey).GetCacheKey(); var service = new TestConfigurationService(); object request = new GetConfigurationDocumentRequest(documentKey); object response = new GetConfigurationDocumentResponse(documentKey, DateTime.Now, DateTime.Now, "Test"); var invocation = new TestInvocation { Target = service, Method = typeof(IApplicationConfigurationReadService).GetMethod("GetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IApplicationConfigurationReadService), Request = request, Response = response }; var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server); var advice = new ConcreteResponseCachingAdvice(directive); advice.Intercept(invocation); var cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNotNull(cacheEntry); Assert.AreEqual(response, cacheEntry); request = new SetConfigurationDocumentRequest(documentKey, "Test"); response = new SetConfigurationDocumentResponse(); invocation = new TestInvocation { Target = service, Method = typeof(IConfigurationService).GetMethod("SetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public), TargetType = typeof(IConfigurationService), Request = request, Response = response }; advice = new ConcreteResponseCachingAdvice(null); advice.Intercept(invocation); cacheEntry = cache.Get(cacheKey, new CacheGetOptions("")); Assert.IsNull(cacheEntry); }
/// <summary> /// Writes the cache directive to the operation context. /// </summary> /// <param name="directive"></param> /// <param name="operationContext"></param> protected internal static void WriteCachingDirectiveHeaders(ResponseCachingDirective directive, OperationContext operationContext) { // add caching directive to WCF message headers so that we send it to the client var header = MessageHeader.CreateHeader(HeaderName, HeaderNamespace, directive); operationContext.OutgoingMessageHeaders.Add(header); }
public ConcreteResponseCachingAdvice(ResponseCachingDirective directive) { _directive = directive; }
/// <summary> /// Implemented by the subclass to cache the response, based on the specified caching directive. /// </summary> /// <param name="invocation"></param> /// <param name="cacheClient"></param> /// <param name="cacheKey"></param> /// <param name="region"></param> /// <param name="directive"></param> protected abstract void CacheResponse(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region, ResponseCachingDirective directive);
/// <summary> /// Puts the invocation response in the specified cache. /// </summary> /// <param name="invocation"></param> /// <param name="cacheClient"></param> /// <param name="cacheKey"></param> /// <param name="region"></param> /// <param name="directive"></param> protected static void PutResponseInCache(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region, ResponseCachingDirective directive) { // bail if the directive does not tell us to cache anything if (directive == null || !directive.EnableCaching || directive.TimeToLive == TimeSpan.Zero) return; // if we don't have a cache key, this is an error if (cacheKey == null) throw new InvalidOperationException( string.Format("{0} is cacheable but the request class does not implement IDefinesCacheKey.", invocation.GetType().FullName)); // put response in cache cacheClient.Put(cacheKey, invocation.ReturnValue, new CachePutOptions(region, directive.TimeToLive, false)); }
/// <summary> /// Implemented by the subclass to cache the response, based on the specified caching directive. /// </summary> protected override void CacheResponse(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region, ResponseCachingDirective directive) { // put the response in the local cache PutResponseInCache(invocation, cacheClient, cacheKey, region, directive); }
/// <summary> /// Implemented by the subclass to cache the response, based on the specified caching directive. /// </summary> protected override void CacheResponse(IInvocation invocation, ICacheClient cacheClient, string cacheKey, string region, ResponseCachingDirective directive) { // if site is server (e.g. callee), put it in our cache if (directive.CacheSite == ResponseCachingSite.Server) { PutResponseInCache(invocation, cacheClient, cacheKey, region, directive); return; } // if site is client (e.g. caller), send directive to client if (directive.CacheSite == ResponseCachingSite.Client) { // check if we have an op context (eg we are running as a WCF service) // if not, then this is not applicable if (OperationContext.Current == null) { return; } // send cache directive to client via headers WriteCachingDirectiveHeaders(directive, OperationContext.Current); return; } }
/// <summary> /// Puts the invocation response in the specified cache. /// </summary> /// <param name="invocation"></param> /// <param name="cacheClient"></param> /// <param name="request"> </param> /// <param name="region"></param> /// <param name="directive"></param> protected static void PutResponseInCache(IInvocation invocation, object request, ICacheClient cacheClient, string region, ResponseCachingDirective directive) { // bail if the directive does not tell us to cache anything if (directive == null || !directive.EnableCaching || directive.TimeToLive == TimeSpan.Zero) return; var strategy = ResponseDataCachingStrategy.Get(invocation.Method); var data = strategy.GetCacheDataToPut(request, invocation.ReturnValue); if (data == null || data.Length == 0) { throw new InvalidOperationException( string.Format("{0} is cacheable but the caching strategy didn't return any data to put in the cache.", invocation.GetType().FullName)); } foreach (var item in data) cacheClient.Put(item.CacheKey, item.Data, new CachePutOptions(region, directive.TimeToLive, false)); }