/// <summary> /// Initialize the eviction policy to run on the specified cache /// </summary> /// <param name="cache"></param> public void Initialize(AbstractCache cache) { foreach (var entry in cache.ListEntries()) { NotifyInsert(entry.Key, entry.Size, entry.Priority); } }
/// <summary> /// Runs this policy against the specified cache until a target number of bytes have been evicted from the cache /// </summary> /// <param name="cache">The cache to run against</param> /// <param name="target">The target number of bytes to be evicted from the cache</param> public void Run(AbstractCache cache, long target) { lock (this) { if (_running) { #if SILVERLIGHT || PORTABLE _evictionCompleted.WaitOne(); #else _evictionCompleted.Wait(); #endif } _running = true; _evictionCompleted.Reset(); } long evictedBytes = 0; TimestampedCacheEntry removed; foreach (var lpEntry in _normalPriorityEntries.Values.OrderBy(e => e.Timestamp)) { if (_normalPriorityEntries.TryRemove(lpEntry.Key, out removed)) { evictedBytes += cache.EvictEntry(lpEntry.Key); } if (evictedBytes >= target) { break; } } if (evictedBytes < target) { foreach (var npEntry in _highPriorityEntries.Values.OrderBy(e => e.Timestamp)) { if (_highPriorityEntries.TryRemove(npEntry.Key, out removed)) { evictedBytes += cache.EvictEntry(npEntry.Key); } if (evictedBytes >= target) { break; } } } _evictionCompleted.Set(); }