private async Task CacheIntercept(InterceptMethodAttribute attribute, string key, ICacheInvocation invocation, string l2Key, bool enableL2Cache) { ICacheProvider cacheProvider = null; switch (attribute.Mode) { case CacheTargetType.Redis: { cacheProvider = CacheContainer.GetService<ICacheProvider>(string.Format("{0}.{1}", attribute.CacheSectionType.ToString(), CacheTargetType.Redis.ToString())); break; } case CacheTargetType.MemoryCache: { cacheProvider = CacheContainer.GetService<ICacheProvider>(CacheTargetType.MemoryCache.ToString()); break; } } if (cacheProvider != null && !enableL2Cache) { await Invoke(cacheProvider, attribute, key, invocation); } else if (cacheProvider != null && enableL2Cache) { var l2CacheProvider = CacheContainer.GetService<ICacheProvider>(CacheTargetType.MemoryCache.ToString()); if (l2CacheProvider != null) await Invoke(cacheProvider, l2CacheProvider, l2Key, attribute, key, invocation); } }
private async Task Invoke(ICacheProvider cacheProvider, InterceptMethodAttribute attribute, string key, ICacheInvocation invocation) { switch (attribute.Method) { case CachingMethod.Get: { var retrunValue = await cacheProvider.GetFromCacheFirst(key, async () => { await invocation.Proceed(); if (invocation.RemoteInvokeResultMessage.StatusCode == CPlatform.Exceptions.StatusCode.Success) { return invocation.RemoteInvokeResultMessage.Result; } else { throw invocation.RemoteInvokeResultMessage.GetExceptionByStatusCode(); } }, invocation.ReturnType, attribute.Time); if (retrunValue != default) { invocation.ReturnValue = retrunValue; invocation.RemoteInvokeResultMessage = new RemoteInvokeResultMessage() { Result = retrunValue }; } break; } default: { await invocation.Proceed(); var keys = attribute.CorrespondingKeys.Select(correspondingKey => string.Format(correspondingKey, invocation.CacheKey)).ToList(); keys.ForEach(key => { cacheProvider.RemoveAsync(key); }); break; } } }