private static void CacheEntryUpdateCallback(CacheEntryUpdateArguments arguments) { Console.WriteLine("Received {0} notification for {1}", arguments.RemovedReason, arguments.Key); if (!File.Exists(arguments.Key)) return; arguments.UpdatedCacheItem = _GetCacheItem(arguments.Key); arguments.UpdatedCacheItemPolicy = _GetCacheItemPolicy(arguments.Key); }
internal static void OnCacheEntryRemovedCallback(CacheEntryRemovedArguments arguments) { MemoryCache cache = arguments.Source as MemoryCache; SentinelEntry entry = arguments.CacheItem.Value as SentinelEntry; CacheEntryRemovedReason reason = arguments.RemovedReason; switch (reason) { case CacheEntryRemovedReason.Expired: break; case CacheEntryRemovedReason.ChangeMonitorChanged: if (entry.ExpensiveObjectDependency.HasChanged) { // If the expensiveObject has been removed explicitly by Cache.Remove, // return from the SentinelEntry removed callback // thus effectively removing the SentinelEntry from the cache. return; } break; case CacheEntryRemovedReason.Evicted: Debug.Fail("Reason should never be CacheEntryRemovedReason.Evicted since the entry was inserted as NotRemovable."); return; default: // do nothing if reason is Removed or CacheSpecificEviction return; } // invoke update callback try { CacheEntryUpdateArguments args = new CacheEntryUpdateArguments(cache, reason, entry.Key, null); entry.CacheEntryUpdateCallback(args); object expensiveObject = (args.UpdatedCacheItem != null) ? args.UpdatedCacheItem.Value : null; CacheItemPolicy policy = args.UpdatedCacheItemPolicy; // Only update the "expensive" object if the user returns a new object, // a policy with update callback, and the change monitors haven't changed. (Inserting // with change monitors that have already changed will cause recursion.) if (expensiveObject != null && IsPolicyValid(policy)) { cache.Set(entry.Key, expensiveObject, policy); } else { cache.Remove(entry.Key); } } catch { cache.Remove(entry.Key); // Review: What should we do with this exception? } }
public void Updated(MemoryCache owner, CacheEntryRemovedReason reason) { if (updateCallback == null) { return; } try { var args = new CacheEntryUpdateArguments(owner, reason, Key, null); updateCallback(args); } catch { // ignore - we don't care about the exceptions thrown inside the // handler } }
internal static void OnCacheEntryRemovedCallback(CacheEntryRemovedArguments arguments) { MemoryCache source = arguments.Source as MemoryCache; MemoryCache.SentinelEntry entry = arguments.CacheItem.Value as MemoryCache.SentinelEntry; CacheEntryRemovedReason removedReason = arguments.RemovedReason; switch (removedReason) { case CacheEntryRemovedReason.Expired: goto Label_004B; case CacheEntryRemovedReason.Evicted: break; case CacheEntryRemovedReason.ChangeMonitorChanged: if (!entry.ExpensiveObjectDependency.HasChanged) { goto Label_004B; } break; default: return; } return; Label_004B: try { CacheEntryUpdateArguments arguments2 = new CacheEntryUpdateArguments(source, removedReason, entry.Key, null); entry.CacheEntryUpdateCallback(arguments2); object obj2 = (arguments2.UpdatedCacheItem != null) ? arguments2.UpdatedCacheItem.Value : null; CacheItemPolicy updatedCacheItemPolicy = arguments2.UpdatedCacheItemPolicy; if ((obj2 != null) && IsPolicyValid(updatedCacheItemPolicy)) { source.Set(entry.Key, obj2, updatedCacheItemPolicy, null); } else { source.Remove(entry.Key, null); } } catch { source.Remove(entry.Key, null); } }
internal static void OnCacheEntryRemovedCallback(CacheEntryRemovedArguments arguments) { MemoryCache cache = arguments.Source as MemoryCache; SentinelEntry entry = arguments.CacheItem.Value as SentinelEntry; CacheEntryRemovedReason reason = arguments.RemovedReason; switch (reason) { case CacheEntryRemovedReason.Expired: break; case CacheEntryRemovedReason.ChangeMonitorChanged: if (entry.ExpensiveObjectDependency.HasChanged) { // If the expensiveObject has been removed explicitly by Cache.Remove, // return from the SentinelEntry removed callback // thus effectively removing the SentinelEntry from the cache. return; } break; case CacheEntryRemovedReason.Evicted: Dbg.Fail("Reason should never be CacheEntryRemovedReason.Evicted since the entry was inserted as NotRemovable."); return; default: // do nothing if reason is Removed or CacheSpecificEviction return; } // invoke update callback try { CacheEntryUpdateArguments args = new CacheEntryUpdateArguments(cache, reason, entry.Key, null); entry.CacheEntryUpdateCallback(args); Object expensiveObject = (args.UpdatedCacheItem != null) ? args.UpdatedCacheItem.Value : null; CacheItemPolicy policy = args.UpdatedCacheItemPolicy; // Dev10 861163 - Only update the "expensive" object if the user returns a new object, // a policy with update callback, and the change monitors haven't changed. (Inserting // with change monitors that have already changed will cause recursion.) if (expensiveObject != null && IsPolicyValid(policy)) { cache.Set(entry.Key, expensiveObject, policy); } else { cache.Remove(entry.Key); } } catch { cache.Remove(entry.Key); // Review: What should we do with this exception? } }
/// <summary> /// /// </summary> /// <param name="args"></param> private void CacheEntryUpdate(CacheEntryUpdateArguments args) { var cacheItem = MemCache.GetCacheItem(args.Key); var cacheObj = cacheItem.Value; cacheItem.Value = cacheObj; args.UpdatedCacheItem = cacheItem; var policy = new CacheItemPolicy { UpdateCallback = new CacheEntryUpdateCallback(CacheEntryUpdate), AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(RefreshInterval) }; args.UpdatedCacheItemPolicy = policy; }
public void Updated (MemoryCache owner, CacheEntryRemovedReason reason) { if (updateCallback == null) return; try { var args = new CacheEntryUpdateArguments (owner, reason, Key, null); updateCallback (args); } catch { // ignore - we don't care about the exceptions thrown inside the // handler } }