public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { if (CachingService.Cache == null) { return(getNext()(input, getNext)); } var cacheKey = CacheUtils.GetCacheKey(input.MethodBase); var cacheValue = CachingService.Cache[cacheKey]; if (cacheValue != null) { return(input.CreateMethodReturn(cacheValue)); } var currentLock = _locks.GetOrAdd(cacheKey, new object()); lock (currentLock) { cacheValue = CachingService.Cache[cacheKey]; if (cacheValue != null) { return(input.CreateMethodReturn(cacheValue)); } if (!NotBlock) { return(AddResultToCache(getNext, input, cacheKey)); } ThreadPoolEx.QueueUserWorkItemWithCatch(state => AddResultToCache(getNext, input, cacheKey)); var result = input.MethodBase.As <MethodInfo>().ReturnType.Create(); AddToCache(result, cacheKey); return(input.CreateMethodReturn(result)); } }
public void SetCache(string cacheKey, MethodBase methodBase) { if (!AddInQueue(cacheKey)) { return; } var cache = Cache; ThreadPoolEx.QueueUserWorkItemWithCatch( state => { var service = UnityContainer.Resolve(methodBase.DeclaringType); var result = methodBase.Invoke(service, new object[0]); var resultType = result.GetType(); if (resultType.IsGenericType && typeof(IQueryable <>) .MakeGenericType(resultType.GetGenericArguments()) .IsAssignableFrom(resultType) ) { var value = LoadQueryable(result); cache.Insert(cacheKey, value, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration); } RemoveFromQueue(cacheKey); }); }