Fix the symbol cache and image cache

This commit is contained in:
2026-02-22 14:04:44 -05:00
parent 9a7b09ef7c
commit 01c2516eaa
2 changed files with 30 additions and 17 deletions

View File

@@ -57,6 +57,7 @@ namespace PortfolioManager.Cache
{ {
lock (thisLock) lock (thisLock)
{ {
DisposeBitmaps();
imageCache = new Dictionary<ImageType, Bitmap>(); imageCache = new Dictionary<ImageType, Bitmap>();
} }
} }
@@ -66,21 +67,29 @@ namespace PortfolioManager.Cache
lock (thisLock) lock (thisLock)
{ {
if (null == imageCacheInstance) return; if (null == imageCacheInstance) return;
List<Bitmap> bitmaps = imageCache.Values.ToList(); DisposeBitmaps();
foreach (Bitmap bitmap in bitmaps)
{
bitmap.Dispose();
}
imageCache = null; imageCache = null;
imageCacheInstance = 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;
} }
} }
} }

View File

@@ -10,7 +10,8 @@ namespace PortfolioManager.Cache
public class SymbolCache : IDisposable public class SymbolCache : IDisposable
{ {
private List<String> symbolCache=new List<String>(); 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 Thread cacheMonitorThread=null;
private volatile bool threadRun=true; private volatile bool threadRun=true;
private int cacheRefreshAfter=60000; // Invalidate cache after private int cacheRefreshAfter=60000; // Invalidate cache after
@@ -60,6 +61,8 @@ namespace PortfolioManager.Cache
{ {
if(symbolCache.Count>0)return new List<string>(symbolCache); if(symbolCache.Count>0)return new List<string>(symbolCache);
} }
lock(fetchLock)
{
List<String> symbols = PricingDA.GetSymbols(); List<String> symbols = PricingDA.GetSymbols();
lock(thisLock) lock(thisLock)
{ {
@@ -68,6 +71,7 @@ namespace PortfolioManager.Cache
return new List<string>(symbols); return new List<string>(symbols);
} }
} }
}
private void ThreadProc() private void ThreadProc()
{ {