예제 #1
0
        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));
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
 public CachingAsyncResult(IAsyncResult result, CachingUserState state)
 {
     _result = result;
     _state  = state;
 }
예제 #4
0
        private void InvokerCallback(IAsyncResult asyncResult)
        {
            CachingUserState state = asyncResult.AsyncState as CachingUserState;

            state.Callback(new CachingAsyncResult(asyncResult, state));
        }