Skip to content

Commit 55fb553

Browse files
committed
change struct to class to use reference type
1 parent bf271b9 commit 55fb553

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@
88
namespace Flow.Launcher.Infrastructure.Image
99
{
1010
[Serializable]
11+
public class ImageUsage
12+
{
13+
14+
public int usage;
15+
public ImageSource imageSource;
16+
17+
public ImageUsage(int usage, ImageSource image)
18+
{
19+
this.usage = usage;
20+
imageSource = image;
21+
}
22+
}
23+
1124
public class ImageCache
1225
{
1326
private const int MaxCached = 50;
14-
public ConcurrentDictionary<string, (int usage, ImageSource imageSource)> Data { get; private set; } = new ConcurrentDictionary<string, (int, ImageSource)>();
27+
public ConcurrentDictionary<string, ImageUsage> Data { get; private set; } = new ConcurrentDictionary<string, ImageUsage>();
1528
private const int permissibleFactor = 2;
1629

1730
public void Initialization(Dictionary<string, int> usage)
1831
{
1932
foreach (var key in usage.Keys)
2033
{
21-
Data[key] = (usage[key], null);
34+
Data[key] = new ImageUsage(usage[key], null);
2235
}
2336
}
2437

@@ -35,20 +48,20 @@ public ImageSource this[string path]
3548
}
3649
set
3750
{
38-
Data.AddOrUpdate(path, (1, value), (k, v) =>
39-
{
40-
v.imageSource = value;
41-
v.usage++;
42-
return v;
43-
});
51+
Data.AddOrUpdate(path, new ImageUsage(0, value), (k, v) =>
52+
{
53+
v.imageSource = value;
54+
v.usage++;
55+
return v;
56+
});
4457

4558
// 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
4659
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
4760
if (Data.Count > permissibleFactor * MaxCached)
4861
{
4962
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
5063

51-
64+
5265
foreach (var key in Data.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
5366
{
5467
if (!(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon)))

0 commit comments

Comments
 (0)