Skip to content

Commit 3574dba

Browse files
committed
Change Cache to LFU
1 parent b02c139 commit 3574dba

File tree

3 files changed

+35
-53
lines changed

3 files changed

+35
-53
lines changed

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<ItemGroup>
5151
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
52-
<PackageReference Include="FastCache.Cached" Version="1.8.2" />
52+
<PackageReference Include="BitFaster.Caching" Version="2.5.0" />
5353
<PackageReference Include="Fody" Version="6.5.5">
5454
<PrivateAssets>all</PrivateAssets>
5555
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,81 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Threading;
6+
using System.Threading.Tasks;
67
using System.Windows.Media;
7-
using FastCache;
8-
using FastCache.Services;
8+
using BitFaster.Caching.Lfu;
99

1010
namespace Flow.Launcher.Infrastructure.Image
1111
{
12-
public class ImageUsage
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-
2412
public class ImageCache
2513
{
26-
private const int MaxCached = 150;
14+
private const int MaxCached = 10;
15+
16+
private ConcurrentLfu<(string, bool), ImageSource> CacheManager { get; set; } = new(MaxCached);
2717

28-
public void Initialize(Dictionary<(string, bool), int> usage)
18+
public void Initialize(IEnumerable<(string, bool)> usage)
2919
{
30-
foreach (var key in usage.Keys)
20+
foreach (var key in usage)
3121
{
32-
Cached<ImageUsage>.Save(key, new ImageUsage(usage[key], null), TimeSpan.MaxValue, MaxCached);
22+
CacheManager.AddOrUpdate(key, null);
3323
}
3424
}
3525

3626
public ImageSource this[string path, bool isFullImage = false]
3727
{
3828
get
3929
{
40-
if (!Cached<ImageUsage>.TryGet((path, isFullImage), out var value))
41-
{
42-
return null;
43-
}
44-
45-
value.Value.usage++;
46-
return value.Value.imageSource;
30+
return CacheManager.TryGet((path, isFullImage), out var value) ? value : null;
4731
}
4832
set
4933
{
50-
if (Cached<ImageUsage>.TryGet((path, isFullImage), out var cached))
51-
{
52-
cached.Value.imageSource = value;
53-
cached.Value.usage++;
54-
}
55-
56-
Cached<ImageUsage>.Save((path, isFullImage), new ImageUsage(0, value), TimeSpan.MaxValue,
57-
MaxCached);
34+
CacheManager.AddOrUpdate((path, isFullImage), value);
5835
}
5936
}
6037

38+
public async ValueTask<ImageSource> GetOrAddAsync(string key,
39+
Func<(string, bool), Task<ImageSource>> valueFactory,
40+
bool isFullImage = false)
41+
{
42+
return await CacheManager.GetOrAddAsync((key, isFullImage), valueFactory);
43+
}
44+
6145
public bool ContainsKey(string key, bool isFullImage)
6246
{
63-
return Cached<ImageUsage>.TryGet((key, isFullImage), out _);
47+
return CacheManager.TryGet((key, isFullImage), out _);
6448
}
6549

6650
public bool TryGetValue(string key, bool isFullImage, out ImageSource image)
6751
{
68-
if (Cached<ImageUsage>.TryGet((key, isFullImage), out var value))
52+
if (CacheManager.TryGet((key, isFullImage), out var value))
6953
{
70-
image = value.Value.imageSource;
71-
value.Value.usage++;
54+
image = value;
7255
return image != null;
7356
}
7457

58+
7559
image = null;
7660
return false;
7761
}
7862

7963
public int CacheSize()
8064
{
81-
return CacheManager.TotalCount<(string, bool), ImageUsage>();
65+
return CacheManager.Count;
8266
}
8367

8468
/// <summary>
8569
/// return the number of unique images in the cache (by reference not by checking images content)
8670
/// </summary>
8771
public int UniqueImagesInCache()
8872
{
89-
return CacheManager.EnumerateEntries<(string, bool), ImageUsage>().Select(x => x.Value.imageSource)
73+
return CacheManager.Select(x => x.Value)
9074
.Distinct()
9175
.Count();
9276
}
9377

94-
public IEnumerable<Cached<(string, bool), ImageUsage>> EnumerateEntries()
78+
public IEnumerable<KeyValuePair<(string, bool), ImageSource>> EnumerateEntries()
9579
{
96-
return CacheManager.EnumerateEntries<(string, bool), ImageUsage>();
80+
return CacheManager;
9781
}
9882
}
9983
}

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using System.Windows.Documents;
89
using System.Windows.Media;
910
using System.Windows.Media.Imaging;
1011
using Flow.Launcher.Infrastructure.Logger;
@@ -17,7 +18,7 @@ public static class ImageLoader
1718
{
1819
private static readonly ImageCache ImageCache = new();
1920
private static SemaphoreSlim storageLock { get; } = new SemaphoreSlim(1, 1);
20-
private static BinaryStorage<Dictionary<(string, bool), int>> _storage;
21+
private static BinaryStorage<List<(string, bool)>> _storage;
2122
private static readonly ConcurrentDictionary<string, string> GuidToKey = new();
2223
private static IImageHashGenerator _hashGenerator;
2324
private static readonly bool EnableImageHash = true;
@@ -31,12 +32,12 @@ public static class ImageLoader
3132

3233
public static async Task InitializeAsync()
3334
{
34-
_storage = new BinaryStorage<Dictionary<(string, bool), int>>("Image");
35+
_storage = new BinaryStorage<List<(string, bool)>>("Image");
3536
_hashGenerator = new ImageHashGenerator();
3637

3738
var usage = await LoadStorageToConcurrentDictionaryAsync();
3839

39-
ImageCache.Initialize(usage.ToDictionary(x => x.Key, x => x.Value));
40+
ImageCache.Initialize(usage);
4041

4142
foreach (var icon in new[] { Constant.DefaultIcon, Constant.MissingImgIcon })
4243
{
@@ -49,7 +50,7 @@ public static async Task InitializeAsync()
4950
{
5051
await Stopwatch.NormalAsync("|ImageLoader.Initialize|Preload images cost", async () =>
5152
{
52-
foreach (var ((path, isFullImage), _) in usage)
53+
foreach (var (path, isFullImage) in usage)
5354
{
5455
await LoadAsync(path, isFullImage);
5556
}
@@ -66,24 +67,21 @@ public static async Task Save()
6667
try
6768
{
6869
await _storage.SaveAsync(ImageCache.EnumerateEntries()
69-
.ToDictionary(
70-
x => x.Key,
71-
x => x.Value.usage));
70+
.Select(x => x.Key)
71+
.ToList());
7272
}
7373
finally
7474
{
7575
storageLock.Release();
7676
}
7777
}
7878

79-
private static async Task<ConcurrentDictionary<(string, bool), int>> LoadStorageToConcurrentDictionaryAsync()
79+
private static async Task<List<(string, bool)>> LoadStorageToConcurrentDictionaryAsync()
8080
{
8181
await storageLock.WaitAsync();
8282
try
8383
{
84-
var loaded = await _storage.TryLoadAsync(new Dictionary<(string, bool), int>());
85-
86-
return new ConcurrentDictionary<(string, bool), int>(loaded);
84+
return await _storage.TryLoadAsync(new List<(string, bool)>());
8785
}
8886
finally
8987
{

0 commit comments

Comments
 (0)