Fix the symbol cache and image cache
This commit is contained in:
@@ -57,6 +57,7 @@ namespace PortfolioManager.Cache
|
||||
{
|
||||
lock (thisLock)
|
||||
{
|
||||
DisposeBitmaps();
|
||||
imageCache = new Dictionary<ImageType, Bitmap>();
|
||||
}
|
||||
}
|
||||
@@ -66,22 +67,30 @@ namespace PortfolioManager.Cache
|
||||
lock (thisLock)
|
||||
{
|
||||
if (null == imageCacheInstance) return;
|
||||
List<Bitmap> bitmaps = imageCache.Values.ToList();
|
||||
foreach (Bitmap bitmap in bitmaps)
|
||||
{
|
||||
bitmap.Dispose();
|
||||
}
|
||||
DisposeBitmaps();
|
||||
imageCache = null;
|
||||
imageCacheInstance = null;
|
||||
}
|
||||
}
|
||||
|
||||
public IImage GetImage(ImageCache.ImageType imageType)
|
||||
|
||||
private void DisposeBitmaps()
|
||||
{
|
||||
lock(this)
|
||||
if(null == imageCache)return;
|
||||
List<Bitmap> bitmaps = imageCache.Values.ToList();
|
||||
foreach (Bitmap bitmap in bitmaps)
|
||||
{
|
||||
return imageCache[imageType];
|
||||
bitmap.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public IImage GetImage(ImageType imageType)
|
||||
{
|
||||
lock(thisLock)
|
||||
{
|
||||
if (imageCache == null)throw new ObjectDisposedException(nameof(ImageCache));
|
||||
if (!imageCache.TryGetValue(imageType, out var bitmap))throw new KeyNotFoundException($"Image {imageType} not found in cache.");
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace PortfolioManager.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
|
||||
@@ -60,12 +61,15 @@ namespace PortfolioManager.Cache
|
||||
{
|
||||
if(symbolCache.Count>0)return new List<string>(symbolCache);
|
||||
}
|
||||
List<String> symbols = PricingDA.GetSymbols();
|
||||
lock(thisLock)
|
||||
lock(fetchLock)
|
||||
{
|
||||
if(symbolCache.Count>0)return new List<string>(symbolCache);
|
||||
symbolCache=new List<String>(symbols);
|
||||
return new List<string>(symbols);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +85,7 @@ namespace PortfolioManager.Cache
|
||||
if(quantums>cacheRefreshAfter)
|
||||
{
|
||||
quantums=0;
|
||||
List<String> symbols = PricingDA.GetSymbols();
|
||||
List<String> symbols = PricingDA.GetSymbols();
|
||||
lock(thisLock)
|
||||
{
|
||||
symbolCache=new List<string>(symbols);
|
||||
@@ -90,4 +94,4 @@ namespace PortfolioManager.Cache
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user