public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) { string cacheKey = this.CreateCacheKey(inputs); CachedOperationResult cachedItem = CacheServiceHelper.Current.Get <CachedOperationResult>(cacheKey); CachingUserState cachingUserState = new CachingUserState { CacheItem = cachedItem, CacheKey = cacheKey, Callback = callback, State = state }; IAsyncResult originalAsyncResult; if (cachedItem != null) { InvokerDelegate invoker = cachedItem.GetValue; object[] notUsed; originalAsyncResult = invoker.BeginInvoke(inputs, out notUsed, this.InvokerCallback, cachingUserState); } else { originalAsyncResult = _invoker.InvokeBegin(instance, inputs, this.InvokerCallback, cachingUserState); } return(new CachingAsyncResult(originalAsyncResult, cachingUserState)); }
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult asyncResult) { CachingAsyncResult result = asyncResult as CachingAsyncResult; CachingUserState state = result.CachingUserState; if (state.CacheItem == null) { object data = _invoker.InvokeEnd(instance, out outputs, result.OriginalAsyncResult); state.CacheItem = new CachedOperationResult { Data = data, Outputs = outputs }; CacheServiceHelper.Current.Add(state.CacheKey, state.CacheItem, TimeSpan.FromSeconds(_cacheDuration)); return(data); } else { InvokerDelegate invoker = ((AsyncResult)result.OriginalAsyncResult).AsyncDelegate as InvokerDelegate; invoker.EndInvoke(out outputs, result.OriginalAsyncResult); return(state.CacheItem.Data); } }
public CachingAsyncResult(IAsyncResult result, CachingUserState state) { _result = result; _state = state; }
private void InvokerCallback(IAsyncResult asyncResult) { CachingUserState state = asyncResult.AsyncState as CachingUserState; state.Callback(new CachingAsyncResult(asyncResult, state)); }