コード例 #1
0
        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            if (OperationCache == null)
            {
                return(InnerOperationInvoker.InvokeEnd(instance, out outputs, result));
            }

            OperationCachingInvokerAsyncResult asyncResult = result as OperationCachingInvokerAsyncResult;

            if (asyncResult == null)
            {
                throw new ArgumentException("Invalid AsyncResult", "result");
            }
            OperationCachingInvokerAsyncResult.End(asyncResult);

            if (asyncResult.IsNewResult)
            {
                OperationCacheKey   key   = new OperationCacheKey(asyncResult.Action, asyncResult.Inputs);
                OperationCacheValue value = new OperationCacheValue(asyncResult.Outputs, asyncResult.ReturnValue);
                OperationCache.Insert(key, value);
            }

            outputs = asyncResult.Outputs;
            return(asyncResult.ReturnValue);
        }
コード例 #2
0
ファイル: ioperationcache.cs プロジェクト: ruo2012/samples-1
        public override bool Equals(object obj)
        {
            OperationCacheKey key = obj as OperationCacheKey;

            if (key == null)
            {
                return(false);
            }

            if (this.action != key.action)
            {
                return(false);
            }

            if (this.inputs.Length != key.inputs.Length)
            {
                return(false);
            }

            for (int i = 0; i < this.inputs.Length; i++)
            {
                if (!this.inputs[i].Equals(key.inputs[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #3
0
        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            if (OperationCache == null)
            {
                return(InnerOperationInvoker.Invoke(instance, inputs, out outputs));
            }

            OperationCacheKey   key   = new OperationCacheKey(action, inputs);
            OperationCacheValue value = OperationCache.Lookup(key);

            // if it's not in the cache, invoke and then add to the cache.
            if (value == null)
            {
                Console.WriteLine("Not in the cache. Creating new and caching.");
                object returnValue = InnerOperationInvoker.Invoke(instance, inputs, out outputs);
                value = new OperationCacheValue(outputs, returnValue);
                OperationCache.Insert(key, value);
                return(returnValue);
            }

            // otherwise, just return the data
            Console.WriteLine("In cache; returning cache instance.");
            outputs = value.Outputs;
            return(value.ReturnValue);
        }
コード例 #4
0
        public OperationCacheValue Lookup(OperationCacheKey key)
        {
            OperationCacheValue value;

            if (!this.cache.TryGetValue(key, out value))
            {
                return(null);
            }

            return(value);
        }
コード例 #5
0
        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            if (OperationCache == null)
            {
                return(InnerOperationInvoker.InvokeBegin(instance, inputs, callback, state));
            }

            OperationCacheKey   key   = new OperationCacheKey(action, inputs);
            OperationCacheValue value = OperationCache.Lookup(key);

            // if it's not in the cache, let the async invoke and let the end handle caching
            if (value == null)
            {
                return(new OperationCachingInvokerAsyncResult(instance, action, inputs, innerOperationInvoker, callback, state));
            }

            // otherwise, just pass all the data to the async result and let it complete synchronously
            return(new OperationCachingInvokerAsyncResult(instance, action, inputs, value.ReturnValue, value.Outputs, callback, state));
        }
コード例 #6
0
 public void Remove(OperationCacheKey key)
 {
     this.cache.Remove(key);
 }
コード例 #7
0
 public void Insert(OperationCacheKey key, OperationCacheValue value)
 {
     this.cache.Add(key, value);
 }