private string GetValueKey(IInvocation invocation, CacheAttribute cacheAttribute) { string retValue = null; switch (cacheAttribute.Strategy) { case CacheStrategy.TimeOut: retValue = GenValueKey(invocation); break; case CacheStrategy.Manual: switch (cacheAttribute.Method) { case CacheMethod.Get: case CacheMethod.Put: retValue = GenValueKey(invocation); break; } break; } return(retValue); }
private void SetRetValueFromCache(IInvocation invocation, CacheAttribute cacheAttribute, string key, string valKey) { object cachedItem = cacheProvider.Get(key, valKey); if (cacheAttribute is AutoUpdateCacheAttribute) { AutoUpdateCacheAttribute autoUpdateAttribute = (AutoUpdateCacheAttribute)cacheAttribute; object waitUpdateItem = cachedItem; if (!string.IsNullOrEmpty(autoUpdateAttribute.Path)) { waitUpdateItem = cachedItem.GetType().GetProperty(autoUpdateAttribute.Path).GetValue(cachedItem); } Type itemType = waitUpdateItem.GetType(); bool isEnumerable = typeof(IEnumerable).IsAssignableFrom(waitUpdateItem.GetType()); if (isEnumerable) { itemType = waitUpdateItem.GetType().GetGenericArguments()[0]; } //var updater = Activator.CreateInstance(autoUpdateAttribute.UpdaterType); var updater = this.iocResolver.Resolve(autoUpdateAttribute.UpdaterType); var updaterType = typeof(ICachedItemUpdater <>).MakeGenericType(itemType); var mi = updaterType.GetMethod("UpdateSource", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new[] { itemType }, null); if (isEnumerable) { foreach (var item in (IEnumerable)waitUpdateItem) { mi.Invoke(updater, new object[] { item }); } } else { mi.Invoke(updater, new object[] { waitUpdateItem }); } iocResolver.Release(updater); } invocation.ReturnValue = cachedItem; }
public void Intercept(IInvocation invocation) { CacheAttribute cacheAttribute = CacheAttribute.GetCacheAttribute(invocation.Method); bool isVoidReturnValue = invocation.Method.ReturnParameter.ParameterType.Equals(typeof(void)); if (cacheAttribute != null) { string key = string.Format("{0}{1}.{2}", cacheAttribute.Prefix, invocation.TargetType.FullName, invocation.Method.Name); string valKey = GetValueKey(invocation, cacheAttribute); switch (cacheAttribute.Strategy) { case CacheStrategy.TimeOut: if (cacheProvider.Exists(key, valKey)) { if (!isVoidReturnValue) { SetRetValueFromCache(invocation, cacheAttribute, key, valKey); } return; } else { invocation.Proceed(); if (!isVoidReturnValue) { cacheProvider.Put(key, valKey, invocation.ReturnValue, cacheAttribute); } } break; case CacheStrategy.Manual: switch (cacheAttribute.Method) { case CacheMethod.Get: if (cacheProvider.Exists(key, valKey)) { if (!isVoidReturnValue) { SetRetValueFromCache(invocation, cacheAttribute, key, valKey); } return; } else { invocation.Proceed(); if (!isVoidReturnValue) { cacheProvider.Put(key, valKey, invocation.ReturnValue, cacheAttribute); } } break; case CacheMethod.Put: invocation.Proceed(); if (!isVoidReturnValue) { cacheProvider.Put(key, valKey, invocation.ReturnValue, cacheAttribute); } break; case CacheMethod.Remove: if (string.IsNullOrEmpty(cacheAttribute.Prefix)) { cacheProvider.Remove(key); } else { cacheProvider.RemoveWithPrefix(cacheAttribute.Prefix); } invocation.Proceed(); break; } break; } } else { invocation.Proceed(); } }
public void Put(string key, string valKey, object value, CacheAttribute cacheAttribute) { Dictionary <string, CacheItem> items; if (cacheItems.ContainsKey(key)) { items = cacheItems[key]; } else { lock (cacheItems) { if (cacheItems.ContainsKey(key)) { items = cacheItems[key]; } else { items = new Dictionary <string, CacheItem>(); cacheItems.Add(key, items); } } } CacheItem item = new CacheItem(); item.Item = value; if (cacheAttribute.Strategy == CacheStrategy.TimeOut) { item.TimeOutCallback = new System.Threading.Timer( delegate(object state) { CacheItemIdentity identity = (CacheItemIdentity)state; lock (this) { if (cacheItems.ContainsKey(identity.Key)) { if (cacheItems[identity.Key].ContainsKey(identity.ValueKey)) { cacheItems[identity.Key][valKey].Dispose(); cacheItems[identity.Key].Remove(identity.ValueKey); } } } }, new CacheItemIdentity { Key = key, ValueKey = valKey }, (int)cacheAttribute.Timeout.TotalMilliseconds, 0 ); } if (items.ContainsKey(valKey)) { lock (items) { var oldItem = items[valKey]; if (oldItem.TimeOutCallback != null) { oldItem.Dispose(); } items.Remove(valKey); } } items.Add(valKey, item); }