1 Commits

Author SHA1 Message Date
3bbd539af4 Changes to TradeBlotter cache 2026-02-22 17:27:35 -05:00
2 changed files with 27 additions and 13 deletions

View File

@@ -90,6 +90,7 @@ namespace TradeBlotter.Cache
while(threadRun)
{
Thread.Sleep(quantumInterval);
if(!threadRun)break;
quantums+=quantumInterval;
if(quantums>cacheRefreshAfter)
{

View File

@@ -16,7 +16,8 @@ namespace TradeBlotter.Cache
public class SymbolCache : IDisposable
{
private List<String> symbolCache=new List<String>();
private Object thisLock=new Object();
private readonly Object thisLock=new Object();
private readonly Object fetchLock = new Object();
private Thread cacheMonitorThread=null;
private volatile bool threadRun=true;
private int cacheRefreshAfter=60000; // Invalidate cache after
@@ -42,31 +43,42 @@ namespace TradeBlotter.Cache
symbolCache=new List<string>();
}
}
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;
}
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.");
}
}
public List<String> GetSymbols()
{
lock(this)
lock(thisLock)
{
if(0==symbolCache.Count)symbolCache=PricingDA.GetSymbols();
return symbolCache;
if(symbolCache.Count>0)return new List<string>(symbolCache);
}
lock(fetchLock)
{
List<String> symbols = PricingDA.GetSymbols();
lock(thisLock)
{
if(symbolCache.Count>0)return new List<string>(symbolCache);
symbolCache=new List<String>(symbols);
return new List<string>(symbols);
}
}
}
private void ThreadProc()
{
int quantums=0;
@@ -74,14 +86,15 @@ namespace TradeBlotter.Cache
while(threadRun)
{
Thread.Sleep(quantumInterval);
if(!threadRun)break;
quantums+=quantumInterval;
if(quantums>cacheRefreshAfter)
{
quantums=0;
List<String> symbols = PricingDA.GetSymbols();
lock(thisLock)
{
// symbolCache.Clear();
symbolCache=PricingDA.GetSymbols();
symbolCache=new List<string>(symbols);
}
}
}