// intercept the method invocation public override void OnInvoke(MethodInterceptionArgs eventArgs) { // get the arguments that were passed to the method object[] args = eventArgs.Arguments.ToArray(); // start building a key based on the method name // because it wouldn't help to return the same value // every time "lulu" was passed to any method StringBuilder keyBuilder = new StringBuilder(eventArgs.Method.Name); // append the hashcode of each arg to the key // this limits us to value types (and strings) // i need a better way to do this (and preferably // a faster one) for (int i = 0; i < args.Length; i++) { keyBuilder.Append(args[i].GetHashCode()); } string key = keyBuilder.ToString(); // if the key doesn't exist, invoke the original method // passing the original arguments and store the result if (!memos.ContainsKey(key)) { memos[key] = eventArgs.Invoke(eventArgs.Arguments); } // return the memo eventArgs.ReturnValue = memos[key]; }
public override void OnInvoke(MethodInterceptionArgs args) { var key = GenerateKey(args); MemoryCache cache = CacheManager.GetCache(this.CacheName); object cachedValue = cache.Get(key); if (cachedValue != null) { args.ReturnValue = cachedValue; } else { lock (syncRoot) { if (cachedValue == null) { var returnVal = args.Invoke(args.Arguments); args.ReturnValue = returnVal; if (returnVal != null) { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromSeconds(TimeoutSeconds); cache.Add(key, returnVal, policy); } } else { args.ReturnValue = cachedValue; } } } }
public override void OnInvoke(MethodInterceptionArgs args) { var key = BuildCacheKey(args.Arguments); if (_cache[key] != null) { args.ReturnValue = _cache[key]; } else { lock (syncRoot) { if (_cache[key] == null) { var returnVal = args.Invoke(args.Arguments); args.ReturnValue = returnVal; _cache[key] = returnVal; } else { args.ReturnValue = _cache[key]; } } } }
public override void OnInvoke(MethodInterceptionArgs args) { if (ThreadingService == null) { ThreadingService = new ThreadingService(); } ThreadingService.QueueUserWorkItem(d => args.Invoke(args.Arguments)); }
protected void RunMethod(MethodInterceptionArgs args) { try { args.ReturnValue = args.Invoke(args.Arguments); } catch { ReturnWithoutRunning(args); } }
public override void OnInvoke(MethodInterceptionArgs args) { var key = GetCacheKeyForArgs(args.Arguments); object obj; if (cache_.Get(key, out obj)) { args.ReturnValue = obj; } else { var @return = args.Invoke(args.Arguments); args.ReturnValue = @return; cache_.Set(key, @return, (long) Lifetime.TotalSeconds, TimeUnit.Seconds); } }
public override void OnInvoke(MethodInterceptionArgs args) { var t = new Task<object>(() => args.Invoke(args.Arguments)); t.Start(); //because we need to set ReturnValue, block until task completes //again, terrible example, but it's a demo :P while (!t.IsCompleted) { Thread.Sleep(10); } args.ReturnValue = t.Result; }
public override void OnInvoke(MethodInterceptionArgs args) { var key = this.BuildCacheKey(args.Arguments); if (Cache[key] != null) { args.ReturnValue = Cache[key]; } else { var returnVal = args.Invoke(args.Arguments); args.ReturnValue = returnVal; Cache[key] = returnVal; } }
//check in-memory dictionary for cached value, if not found execute method and add to dictionary public override void OnInvoke(MethodInterceptionArgs args) { var key = new CacheKey(args); if (cache.ContainsKey(key)) { args.ReturnValue = cache[key]; } else { var result = args.Invoke(args.Arguments); cache.Add(key, result); args.ReturnValue = result; } }
public override void OnInvoke(MethodInterceptionArgs args) { var key = BuildCacheKey(args.Arguments); if (_cache.ContainsKey(key)) { args.ReturnValue = _cache[key]; } else { var returnVal = args.Invoke(args.Arguments); args.ReturnValue = returnVal; _cache[key] = returnVal; } }
public override void OnInvoke(MethodInterceptionArgs args) { var key = GetCacheKeyForArgs(args.Arguments); object obj; if (cache_.Get(key, out obj)) { args.ReturnValue = obj; } else { var @return = args.Invoke(args.Arguments); args.ReturnValue = @return; cache_.Set(key, @return, (long)Lifetime.TotalSeconds, TimeUnit.Seconds); } }
/// <summary> /// Method invoked <i>instead</i> of the method to which the aspect has been applied. /// </summary> /// <param name="args">Advice arguments.</param> public override void OnInvoke(MethodInterceptionArgs args) { ((IUow)this).Create(); try { args.Invoke(args.Arguments); ((IUow)this).Commit(); } catch (Exception e) { ((IUow)this).Rollback(); throw; } }
/// <summary> /// This method implements thread-safe access for cached objects. Double-checked locking is used. /// </summary> public override void OnInvoke(MethodInterceptionArgs args) { if (IsIgnorable && (bool)args.Arguments.Last()) { base.OnInvoke(args); return; } KeyObject keyObject = new KeyObject(_methodFullName, args.Arguments); object valueFromCache = _cache.Get(keyObject); if (valueFromCache != null) { args.ReturnValue = GetReturnValue(valueFromCache); return; } lock (_syncRoot) { valueFromCache = _cache.Get(keyObject); if (valueFromCache != null) { args.ReturnValue = GetReturnValue(valueFromCache); return; } object methodResult = args.Invoke(args.Arguments); EntityResponse response = methodResult as EntityResponse; if (response == null) { _cache.Insert(keyObject, methodResult, _slidingExpiration); } else if (response.IsSuccess) { _cache.Insert(keyObject, response.Entity, _slidingExpiration); } args.ReturnValue = methodResult; } }
/// <summary> /// Invoke on method call. /// </summary> /// <param name="methodInterceptionArgs"></param> public override void OnInvoke(MethodInterceptionArgs methodInterceptionArgs) { if (Action == CacheAction.Add) { var cacheKey = BuildCacheKey(methodInterceptionArgs); if (Cache[cacheKey] != null) { methodInterceptionArgs.ReturnValue = Cache[cacheKey]; } else { lock (_syncRoot) { if (Cache[cacheKey] == null) { var returnVal = methodInterceptionArgs.Invoke(methodInterceptionArgs.Arguments); methodInterceptionArgs.ReturnValue = returnVal; Cache[cacheKey] = returnVal; } else { methodInterceptionArgs.ReturnValue = Cache[cacheKey]; } } } } else { var typeName = GetTypeName(methodInterceptionArgs.Binding.GetType()); lock (_syncRoot) { Cache.Remove(typeName); } methodInterceptionArgs.ReturnValue = methodInterceptionArgs.Invoke(methodInterceptionArgs.Arguments); } }
public override void OnInvoke(MethodInterceptionArgs args) { ThreadPool.QueueUserWorkItem(d => args.Invoke(args.Arguments)); }
public override void OnInvoke(MethodInterceptionArgs args) { if(ThreadingService == null) ThreadingService = new ThreadingService(); ThreadingService.QueueUserWorkItem(d => args.Invoke(args.Arguments)); }