/// <summary> /// </summary> /// <param name="sender"> </param> /// <param name="e"> </param> protected internal virtual void OnItemExpired(object sender, ItemExpiredEventArgs <T> e) { if (sender == null) { throw new ArgumentNullException("sender"); } if (e == null) { throw new ArgumentNullException("e"); } if (_itemExpired != null) { _itemExpired(sender, e); } }
/// <summary> /// </summary> /// <param name="arguments"> </param> public static void ExpirationCallback(CacheEntryRemovedArguments arguments) { if (arguments == null) { throw new ArgumentNullException("arguments"); } // ItemExpiredEventArgs eventArgs = new ItemExpiredEventArgs(removedKey, expiredValue, removalReason); var eventArgs = new ItemExpiredEventArgs <T>(arguments.CacheItem.Key , (T)arguments.CacheItem.Value , arguments.RemovedReason); Trace.WriteLine(string.Format("Item {0} was {1} from cache." , arguments.CacheItem.Value.GetType().Name , arguments.RemovedReason.ToString() , TraceCategory.Caching.ToString())); if (arguments.RemovedReason == CacheEntryRemovedReason.Expired) { Trace.WriteLine(string.Format("Cache has {0} items before callback: {1} was {2} from cache." , Cache <string, T> .Count , arguments.CacheItem.Value.GetType().Name , arguments.RemovedReason.ToString() , TraceCategory.Caching.ToString())); lock (_itemExpiredLock) { Trace.WriteLine(string.Format("{0}: {1} {2} {3}" , arguments.RemovedReason.ToString() , arguments.CacheItem.Key , arguments.CacheItem.Value.GetType().Name , "callback started. ") , TraceCategory.Caching.ToString()); //Trace.WriteLine(string.Format("Cache has {0} items after pruning. ", Cache<string, object>.Count)); // testing, filter out critical cache values and refresh them // need to run tests to see if dependencies perform better (memory-wise) if (arguments.CacheItem.Value.GetType() == typeof(ChannelData)) { arguments.CacheItem.Value = new ChannelListLoader <IChannel>().LoadPlaylist(true); arguments.CacheItem.Key = arguments.CacheItem.Value.GetHashCode().ToString(CultureInfo.InvariantCulture); Cache <string, T> .Insert(arguments.CacheItem.Key, (T)arguments.CacheItem.Value); } } Trace.WriteLine(string.Format("{0}: {1} {2} {3}" , arguments.RemovedReason.ToString() , arguments.CacheItem.Key , arguments.CacheItem.Value.GetType().Name , "callback completed. "), TraceCategory.Caching.ToString()); Trace.WriteLine(string.Format("Cache has {0} items after callback. ", Cache <string, T> .Count) , TraceCategory.Caching.ToString()); // raise the event; can use this elsewhere to trigger callbacks, which YOU SHOULD DO! if (_itemExpired != null) { _itemExpired(default(T), eventArgs); } } }