public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation) { IMethodInvocationResult result = null; CacheAttribute attribute = methodInvocation.InstanceMethodInfo.GetCustomAttribute <CacheAttribute>(); if (attribute != null) { string key = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheName, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray()); string area = KeyBuilder.BuildCacheKey(attribute.CacheSetting, attribute.CacheArea, methodInvocation.TargetInstance.ToString(), methodInvocation.InstanceMethodInfo.Name, attribute.PropertyName, methodInvocation.Arguments.ToArray()); CachedItemDTO cachedItem = CacheManager.GetValue(area, key); object cachedValue = null; if (cachedItem != null) { cachedValue = cachedItem.CacheValue; } if (cachedValue != null) { MethodInfo methodInfo = methodInvocation.InstanceMethodInfo; Type typeFoo = methodInfo.ReturnType; var returnData = Convert.ChangeType(cachedValue, typeFoo); return(methodInvocation.CreateResult(returnData)); } else { result = methodInvocation.InvokeNext(); CacheManager.AddValue(area, key, result.ReturnValue, attribute.TimeoutSeconds); return(result); } } else { result = methodInvocation.InvokeNext(); return(result); } }
public IMethodInvocationResult Intercept(ISyncMethodInvocation methodInvocation) { IEnumerable <CacheInvalidateAttribute> invalidateAttributeList = methodInvocation.InstanceMethodInfo.GetCustomAttributes <CacheInvalidateAttribute>(); foreach (CacheInvalidateAttribute attribute in invalidateAttributeList) { //string key = KeyBuilder.BuildKey(attribute.CacheSetting, attribute.CacheName, attribute.PropertyName, methodInvocation.Arguments.ToArray()); string area = KeyBuilder.BuildInvalidateCacheKey(attribute.CacheSetting, attribute.CacheArea, attribute.PropertyName, methodInvocation.Arguments.ToArray()); //CachedItemDTO cachedItem = CacheManager.GetValue(attribute.CacheArea, key); //if (cachedItem != null) //{ CacheManager.RemoveCacheArea(ca => ca._CacheAreaName.StartsWith(area)); //} } return(methodInvocation.InvokeNext()); }