2022-12-29 03:17:29 +00:00
|
|
|
|
using System;
|
2017-01-13 15:40:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-18 11:22:47 +00:00
|
|
|
|
using System.Linq;
|
2024-05-29 22:57:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-01-13 15:40:32 +00:00
|
|
|
|
using System.Windows.Media;
|
2024-05-29 22:57:26 +00:00
|
|
|
|
using BitFaster.Caching.Lfu;
|
2014-12-18 11:22:47 +00:00
|
|
|
|
|
2020-08-05 09:57:23 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure.Image
|
2014-12-18 11:22:47 +00:00
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
public class ImageCache
|
2014-12-18 11:22:47 +00:00
|
|
|
|
{
|
2024-05-30 05:48:56 +00:00
|
|
|
|
private const int MaxCached = 150;
|
2024-05-29 22:57:26 +00:00
|
|
|
|
|
|
|
|
|
|
private ConcurrentLfu<(string, bool), ImageSource> CacheManager { get; set; } = new(MaxCached);
|
2021-05-23 14:54:52 +00:00
|
|
|
|
|
2024-05-29 22:57:26 +00:00
|
|
|
|
public void Initialize(IEnumerable<(string, bool)> usage)
|
2020-11-07 16:01:33 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
foreach (var key in usage)
|
2020-11-07 16:01:33 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
CacheManager.AddOrUpdate(key, null);
|
2020-11-07 16:01:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-30 19:39:57 +00:00
|
|
|
|
public ImageSource this[string path, bool isFullImage = false]
|
2014-12-18 11:22:47 +00:00
|
|
|
|
{
|
2017-01-13 15:40:32 +00:00
|
|
|
|
get
|
2014-12-18 11:22:47 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
return CacheManager.TryGet((path, isFullImage), out var value) ? value : null;
|
2014-12-18 11:22:47 +00:00
|
|
|
|
}
|
2020-08-05 09:30:45 +00:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
CacheManager.AddOrUpdate((path, isFullImage), value);
|
2020-08-05 09:30:45 +00:00
|
|
|
|
}
|
2016-05-22 04:30:38 +00:00
|
|
|
|
}
|
2014-12-18 11:22:47 +00:00
|
|
|
|
|
2024-05-29 22:57:26 +00:00
|
|
|
|
public async ValueTask<ImageSource> GetOrAddAsync(string key,
|
|
|
|
|
|
Func<(string, bool), Task<ImageSource>> valueFactory,
|
|
|
|
|
|
bool isFullImage = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await CacheManager.GetOrAddAsync((key, isFullImage), valueFactory);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-30 19:39:57 +00:00
|
|
|
|
public bool ContainsKey(string key, bool isFullImage)
|
2017-01-13 15:40:32 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
return CacheManager.TryGet((key, isFullImage), out _);
|
2014-12-18 11:22:47 +00:00
|
|
|
|
}
|
2020-01-03 19:16:17 +00:00
|
|
|
|
|
2022-12-29 03:43:40 +00:00
|
|
|
|
public bool TryGetValue(string key, bool isFullImage, out ImageSource image)
|
|
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
if (CacheManager.TryGet((key, isFullImage), out var value))
|
2022-12-29 03:43:40 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
image = value;
|
2024-01-22 18:06:14 +00:00
|
|
|
|
return image != null;
|
2022-12-29 03:43:40 +00:00
|
|
|
|
}
|
2024-01-22 18:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
image = null;
|
|
|
|
|
|
return false;
|
2022-12-29 03:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-03 19:16:17 +00:00
|
|
|
|
public int CacheSize()
|
|
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
return CacheManager.Count;
|
2020-01-03 19:16:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// return the number of unique images in the cache (by reference not by checking images content)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int UniqueImagesInCache()
|
|
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
return CacheManager.Select(x => x.Value)
|
2024-01-22 18:06:14 +00:00
|
|
|
|
.Distinct()
|
|
|
|
|
|
.Count();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 22:57:26 +00:00
|
|
|
|
public IEnumerable<KeyValuePair<(string, bool), ImageSource>> EnumerateEntries()
|
2024-01-22 18:06:14 +00:00
|
|
|
|
{
|
2024-05-29 22:57:26 +00:00
|
|
|
|
return CacheManager;
|
2020-01-03 19:16:17 +00:00
|
|
|
|
}
|
2014-12-18 11:22:47 +00:00
|
|
|
|
}
|
2022-10-30 19:39:57 +00:00
|
|
|
|
}
|