|
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Linq; |
5 | 5 | using System.Threading; |
| 6 | +using System.Threading.Tasks; |
6 | 7 | using System.Windows.Media; |
7 | | -using FastCache; |
8 | | -using FastCache.Services; |
| 8 | +using BitFaster.Caching.Lfu; |
9 | 9 |
|
10 | 10 | namespace Flow.Launcher.Infrastructure.Image |
11 | 11 | { |
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 | | - |
24 | 12 | public class ImageCache |
25 | 13 | { |
26 | 14 | private const int MaxCached = 150; |
27 | 15 |
|
28 | | - public void Initialize(Dictionary<(string, bool), int> usage) |
| 16 | + private ConcurrentLfu<(string, bool), ImageSource> CacheManager { get; set; } = new(MaxCached); |
| 17 | + |
| 18 | + public void Initialize(IEnumerable<(string, bool)> usage) |
29 | 19 | { |
30 | | - foreach (var key in usage.Keys) |
| 20 | + foreach (var key in usage) |
31 | 21 | { |
32 | | - Cached<ImageUsage>.Save(key, new ImageUsage(usage[key], null), TimeSpan.MaxValue, MaxCached); |
| 22 | + CacheManager.AddOrUpdate(key, null); |
33 | 23 | } |
34 | 24 | } |
35 | 25 |
|
36 | 26 | public ImageSource this[string path, bool isFullImage = false] |
37 | 27 | { |
38 | 28 | get |
39 | 29 | { |
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; |
47 | 31 | } |
48 | 32 | set |
49 | 33 | { |
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); |
58 | 35 | } |
59 | 36 | } |
60 | 37 |
|
| 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 | + |
61 | 45 | public bool ContainsKey(string key, bool isFullImage) |
62 | 46 | { |
63 | | - return Cached<ImageUsage>.TryGet((key, isFullImage), out _); |
| 47 | + return CacheManager.TryGet((key, isFullImage), out _); |
64 | 48 | } |
65 | 49 |
|
66 | 50 | public bool TryGetValue(string key, bool isFullImage, out ImageSource image) |
67 | 51 | { |
68 | | - if (Cached<ImageUsage>.TryGet((key, isFullImage), out var value)) |
| 52 | + if (CacheManager.TryGet((key, isFullImage), out var value)) |
69 | 53 | { |
70 | | - image = value.Value.imageSource; |
71 | | - value.Value.usage++; |
| 54 | + image = value; |
72 | 55 | return image != null; |
73 | 56 | } |
74 | 57 |
|
| 58 | + |
75 | 59 | image = null; |
76 | 60 | return false; |
77 | 61 | } |
78 | 62 |
|
79 | 63 | public int CacheSize() |
80 | 64 | { |
81 | | - return CacheManager.TotalCount<(string, bool), ImageUsage>(); |
| 65 | + return CacheManager.Count; |
82 | 66 | } |
83 | 67 |
|
84 | 68 | /// <summary> |
85 | 69 | /// return the number of unique images in the cache (by reference not by checking images content) |
86 | 70 | /// </summary> |
87 | 71 | public int UniqueImagesInCache() |
88 | 72 | { |
89 | | - return CacheManager.EnumerateEntries<(string, bool), ImageUsage>().Select(x => x.Value.imageSource) |
| 73 | + return CacheManager.Select(x => x.Value) |
90 | 74 | .Distinct() |
91 | 75 | .Count(); |
92 | 76 | } |
93 | 77 |
|
94 | | - public IEnumerable<Cached<(string, bool), ImageUsage>> EnumerateEntries() |
| 78 | + public IEnumerable<KeyValuePair<(string, bool), ImageSource>> EnumerateEntries() |
95 | 79 | { |
96 | | - return CacheManager.EnumerateEntries<(string, bool), ImageUsage>(); |
| 80 | + return CacheManager; |
97 | 81 | } |
98 | 82 | } |
99 | 83 | } |
0 commit comments