104 lines
2.7 KiB
C#
104 lines
2.7 KiB
C#
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;
|
|
using System.Diagnostics;
|
|
|
|
namespace TradeBlotter.Cache
|
|
{
|
|
public class SymbolCache : IDisposable
|
|
{
|
|
private List<String> symbolCache=new List<String>();
|
|
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
|
|
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<string>();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
lock(thisLock)
|
|
{
|
|
if(null==symbolCacheInstance || false==threadRun)return;
|
|
threadRun=false;
|
|
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(thisLock)
|
|
{
|
|
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;
|
|
int quantumInterval=1000;
|
|
while(threadRun)
|
|
{
|
|
Thread.Sleep(quantumInterval);
|
|
if(!threadRun)break;
|
|
quantums+=quantumInterval;
|
|
if(quantums>cacheRefreshAfter)
|
|
{
|
|
quantums=0;
|
|
List<String> symbols = PricingDA.GetSymbols();
|
|
lock(thisLock)
|
|
{
|
|
symbolCache=new List<string>(symbols);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|