From e7fe33e0e7883efa2f8ba2ceb051059a9c3a4186 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 16 May 2024 20:38:37 -0400 Subject: [PATCH] Add SymbolCache --- Cache/SymbolCache.cs | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Cache/SymbolCache.cs diff --git a/Cache/SymbolCache.cs b/Cache/SymbolCache.cs new file mode 100644 index 0000000..f103105 --- /dev/null +++ b/Cache/SymbolCache.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using MarketData.MarketDataModel; +using MarketData.Helper; +using MarketData; +using MarketData.Utils; +using System.Windows.Media.Animation; +using MarketData.DataAccess; + +namespace TradeBlotter.Cache +{ + public class SymbolCache : IDisposable + { + private List symbolCache=new List(); + private Object thisLock=new Object(); + private Thread cacheMonitorThread=null; + private volatile bool threadRun=true; + private int cacheRefreshAfter=60000; // Invalidate cache after + private static SymbolCache symbolCacheInstance=null; + + private SymbolCache() + { + cacheMonitorThread=new Thread(new ThreadStart(ThreadProc)); + cacheMonitorThread.Start(); + } + public static SymbolCache GetInstance() + { + lock(typeof(SymbolCache)) + { + if(null==symbolCacheInstance) symbolCacheInstance=new SymbolCache(); + return symbolCacheInstance; + } + } + public void Clear() + { + lock(thisLock) + { + symbolCache=new List(); + } + } + public void Dispose() + { + lock(thisLock) + { + if(null==symbolCacheInstance || false==threadRun)return; + threadRun=false; + if(null!=cacheMonitorThread) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[SymbolCache:Dispose]Thread state is {0}. Joining main thread...",Utility.ThreadStateToString(cacheMonitorThread))); + cacheMonitorThread.Join(5000); + cacheMonitorThread=null; + MDTrace.WriteLine(LogLevel.DEBUG,"[SymbolCache:Dispose] End."); + } + symbolCacheInstance=null; + } + } + + public List GetSymbols() + { + lock(this) + { + if(0==symbolCache.Count)symbolCache=PricingDA.GetSymbols(); + return symbolCache; + } + } + private void ThreadProc() + { + int quantums=0; + int quantumInterval=1000; + while(threadRun) + { + Thread.Sleep(quantumInterval); + quantums+=quantumInterval; + if(quantums>cacheRefreshAfter) + { + quantums=0; + lock(thisLock) + { + symbolCache.Clear(); + } + } + } + } + } +}