Skip to content

Commit 37f1a64

Browse files
committed
avoid duplicate removing
1 parent e8691c2 commit 37f1a64

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ImageCache
2626
private const int MaxCached = 50;
2727
public ConcurrentDictionary<string, ImageUsage> Data { get; private set; } = new ConcurrentDictionary<string, ImageUsage>();
2828
private const int permissibleFactor = 2;
29-
29+
3030
public void Initialization(Dictionary<string, int> usage)
3131
{
3232
foreach (var key in usage.Keys)
@@ -35,6 +35,8 @@ public void Initialization(Dictionary<string, int> usage)
3535
}
3636
}
3737

38+
private volatile bool removing;
39+
3840
public ImageSource this[string path]
3941
{
4042
get
@@ -62,11 +64,13 @@ public ImageSource this[string path]
6264

6365
// 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
6466
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
65-
if (Data.Count > permissibleFactor * MaxCached)
67+
if (Data.Count > permissibleFactor * MaxCached && !removing)
6668
{
69+
removing = true;
6770
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
6871
foreach (var key in Data.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
6972
Data.TryRemove(key, out _);
73+
removing = false;
7074
}
7175
}
7276
}

0 commit comments

Comments
 (0)