Flow.Launcher/Flow.Launcher.Infrastructure/Image/ImageCache.cs
2020-11-16 13:04:16 +08:00

99 lines
No EOL
3.1 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
namespace Flow.Launcher.Infrastructure.Image
{
[Serializable]
public class ImageUsage
{
public int usage;
public ImageSource imageSource;
public ImageUsage(int usage, ImageSource image)
{
this.usage = usage;
imageSource = image;
}
}
public class ImageCache
{
private const int MaxCached = 50;
public ConcurrentDictionary<string, ImageUsage> Data { get; private set; } = new ConcurrentDictionary<string, ImageUsage>();
private const int permissibleFactor = 2;
public void Initialization(Dictionary<string, int> usage)
{
foreach (var key in usage.Keys)
{
Data[key] = new ImageUsage(usage[key], null);
}
}
public ImageSource this[string path]
{
get
{
if (Data.TryGetValue(path, out var value))
{
value.usage++;
return value.imageSource;
}
return null;
}
set
{
Data.AddOrUpdate(
path,
new ImageUsage(0, value),
(k, v) =>
{
v.imageSource = value;
v.usage++;
return v;
}
);
// 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
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
if (Data.Count > permissibleFactor * MaxCached)
{
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
foreach (var key in Data
.Where(x => x.Key != Constant.MissingImgIcon
&& x.Key != Constant.ErrorIcon
&& x.Key != Constant.DefaultIcon)
.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
{
Data.TryRemove(key, out _);
}
}
}
}
public bool ContainsKey(string key)
{
return Data.ContainsKey(key) && Data[key].imageSource != null;
}
public int CacheSize()
{
return Data.Count;
}
/// <summary>
/// return the number of unique images in the cache (by reference not by checking images content)
/// </summary>
public int UniqueImagesInCache()
{
return Data.Values.Select(x => x.imageSource).Distinct().Count();
}
}
}