From 37f1a64495b848e5951cce83d57e375bb5fce5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 23 May 2021 22:54:52 +0800 Subject: [PATCH] avoid duplicate removing --- Flow.Launcher.Infrastructure/Image/ImageCache.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Image/ImageCache.cs b/Flow.Launcher.Infrastructure/Image/ImageCache.cs index bb7ec6817..4e95278c1 100644 --- a/Flow.Launcher.Infrastructure/Image/ImageCache.cs +++ b/Flow.Launcher.Infrastructure/Image/ImageCache.cs @@ -26,7 +26,7 @@ namespace Flow.Launcher.Infrastructure.Image private const int MaxCached = 50; public ConcurrentDictionary Data { get; private set; } = new ConcurrentDictionary(); private const int permissibleFactor = 2; - + public void Initialization(Dictionary usage) { foreach (var key in usage.Keys) @@ -35,6 +35,8 @@ namespace Flow.Launcher.Infrastructure.Image } } + private volatile bool removing; + public ImageSource this[string path] { get @@ -62,11 +64,13 @@ namespace Flow.Launcher.Infrastructure.Image // To prevent the dictionary from drastically increasing in size by caching images, the dictionary size is not allowed to grow more than the permissibleFactor * maxCached size // This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time - if (Data.Count > permissibleFactor * MaxCached) + if (Data.Count > permissibleFactor * MaxCached && !removing) { + removing = true; // To delete the images from the data dictionary based on the resizing of the Usage Dictionary. foreach (var key in Data.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key)) Data.TryRemove(key, out _); + removing = false; } } }