diff --git a/MarketData/MarketDataLib/Cache/GBPriceCache.cs b/MarketData/MarketDataLib/Cache/GBPriceCache.cs index 167c4ee..2affb9b 100755 --- a/MarketData/MarketDataLib/Cache/GBPriceCache.cs +++ b/MarketData/MarketDataLib/Cache/GBPriceCache.cs @@ -43,6 +43,8 @@ namespace MarketData.Cache public class GBPriceCache : IDisposable { + private static readonly int EVICTION_DAYCOUNT=252; // upon eviction trigger remove all data older than maxdate - evictionPolicyThreshholdDays + private Thread cacheMonitorThread = null; private volatile bool threadRun = true; private Object thisLock = new Object(); @@ -52,7 +54,7 @@ namespace MarketData.Cache private DateGenerator dateGenerator = new DateGenerator(); private static GBPriceCache priceCacheInstance = null; private int cacheRefreshAfter = 120000; // 2 minutes - private SemaphoreSlim fetchSemaphore = new SemaphoreSlim(8); // max 8 concurrent DB fetches + private SemaphoreSlim fetchSemaphore = new SemaphoreSlim(8); // max 8 concurrent DB fetches public IPricingDataAccess PricingDataAccess { get; set; } = new RealPricingDA(); protected GBPriceCache() @@ -330,9 +332,13 @@ namespace MarketData.Cache DateTime maxDate = snapshot.PriceCache.Values.SelectMany(p => p.Keys).DefaultIfEmpty(DateTime.MinValue).Max(); if (maxDate != DateTime.MinValue) { - DateTime evictBefore = dateGenerator.GenerateHistoricalDates(maxDate, 252).Min(); - MDTrace.WriteLine(LogLevel.DEBUG, $"GBPriceCache, clearing cache on or before {evictBefore.ToShortDateString()}"); - UpdateSnapshot(BuildEvictedPriceCache(evictBefore), new Dictionary(), new Dictionary()); + DateTime evictBefore = dateGenerator.GenerateHistoricalDates(maxDate, EVICTION_DAYCOUNT).Min(); + int beforeCount = snapshot.PriceCache.Values.Sum(p => p.Count); + Dictionary newCache = BuildEvictedPriceCache(evictBefore); + int afterCount = newCache.Values.Sum(p => p.Count); + int removed = beforeCount - afterCount; + MDTrace.WriteLine(LogLevel.DEBUG, $"GBPriceCache eviction: removed {removed} prices (before={beforeCount}, after={afterCount}) on or before {evictBefore.ToShortDateString()}"); + UpdateSnapshot(newCache, new Dictionary(), new Dictionary()); GC.Collect(); } }