mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
Use cache type directly instead of a new class, decouple binarystorage and storage
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Wox.Infrastructure.Image
|
||||
{
|
||||
@@ -8,29 +10,35 @@ namespace Wox.Infrastructure.Image
|
||||
public class ImageCache
|
||||
{
|
||||
private const int MaxCached = 200;
|
||||
public ConcurrentDictionary<string, int> TopUsedImages = new ConcurrentDictionary<string, int>();
|
||||
public ConcurrentDictionary<string, int> Usage = new ConcurrentDictionary<string, int>();
|
||||
private readonly ConcurrentDictionary<string, ImageSource> _data = new ConcurrentDictionary<string, ImageSource>();
|
||||
|
||||
public void Add(string path)
|
||||
|
||||
public ImageSource this[string path]
|
||||
{
|
||||
if (TopUsedImages.ContainsKey(path))
|
||||
get
|
||||
{
|
||||
TopUsedImages[path] = TopUsedImages[path] + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
TopUsedImages[path] = 1;
|
||||
Usage.AddOrUpdate(path, 1, (k, v) => v + 1);
|
||||
var i = _data[path];
|
||||
return i;
|
||||
}
|
||||
set { _data[path] = value; }
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
if (TopUsedImages.Count > MaxCached)
|
||||
{
|
||||
var images = TopUsedImages.OrderByDescending(o => o.Value)
|
||||
.Take(MaxCached)
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
TopUsedImages = new ConcurrentDictionary<string, int>(images);
|
||||
}
|
||||
var images = Usage
|
||||
.OrderByDescending(o => o.Value)
|
||||
.Take(MaxCached)
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
Usage = new ConcurrentDictionary<string, int>(images);
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
var contains = _data.ContainsKey(key);
|
||||
return contains;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace Wox.Infrastructure.Image
|
||||
{
|
||||
public static class ImageLoader
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, ImageSource> ImageSources = new ConcurrentDictionary<string, ImageSource>();
|
||||
private static readonly ImageCache ImageCache = new ImageCache();
|
||||
private static readonly BinaryStorage<ConcurrentDictionary<string, int>> Storage;
|
||||
|
||||
|
||||
private static readonly string[] ImageExtions =
|
||||
@@ -29,19 +30,17 @@ namespace Wox.Infrastructure.Image
|
||||
".ico"
|
||||
};
|
||||
|
||||
private static readonly ImageCache _cache;
|
||||
private static readonly BinaryStorage<ImageCache> _storage;
|
||||
|
||||
static ImageLoader()
|
||||
{
|
||||
_storage = new BinaryStorage<ImageCache>();
|
||||
_cache = _storage.Load();
|
||||
Storage = new BinaryStorage<ConcurrentDictionary<string, int>> ("ImageCache");
|
||||
ImageCache.Usage = Storage.TryLoad(new ConcurrentDictionary<string, int>());
|
||||
}
|
||||
|
||||
public static void Save()
|
||||
{
|
||||
_cache.Cleanup();
|
||||
_storage.Save();
|
||||
ImageCache.Cleanup();
|
||||
Storage.Save(ImageCache.Usage);
|
||||
}
|
||||
|
||||
private static ImageSource ShellIcon(string fileName)
|
||||
@@ -78,7 +77,7 @@ namespace Wox.Infrastructure.Image
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Exception(e);
|
||||
return ImageSources[Constant.ErrorIcon];
|
||||
return ImageCache[Constant.ErrorIcon];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,22 +87,22 @@ namespace Wox.Infrastructure.Image
|
||||
{
|
||||
ImageSource img = new BitmapImage(new Uri(icon));
|
||||
img.Freeze();
|
||||
ImageSources[icon] = img;
|
||||
ImageCache[icon] = img;
|
||||
}
|
||||
Task.Run(() =>
|
||||
{
|
||||
Stopwatch.Normal("Preload images from cache", () =>
|
||||
{
|
||||
_cache.TopUsedImages.AsParallel().Where(i => !ImageSources.ContainsKey(i.Key)).ForAll(i =>
|
||||
ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i =>
|
||||
{
|
||||
var img = Load(i.Key);
|
||||
if (img != null)
|
||||
{
|
||||
ImageSources[i.Key] = img;
|
||||
ImageCache[i.Key] = img;
|
||||
}
|
||||
});
|
||||
});
|
||||
Log.Info($"Preload {_cache.TopUsedImages.Count} images from cache");
|
||||
Log.Info($"Preload {ImageCache.Usage.Count} images from cache");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -112,13 +111,11 @@ namespace Wox.Infrastructure.Image
|
||||
ImageSource image;
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
image = ImageSources[Constant.ErrorIcon];
|
||||
_cache.Add(Constant.ErrorIcon);
|
||||
image = ImageCache[Constant.ErrorIcon];
|
||||
}
|
||||
else if (ImageSources.ContainsKey(path))
|
||||
else if (ImageCache.ContainsKey(path))
|
||||
{
|
||||
image = ImageSources[path];
|
||||
_cache.Add(path);
|
||||
image = ImageCache[path];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -146,7 +143,7 @@ namespace Wox.Infrastructure.Image
|
||||
}
|
||||
else
|
||||
{
|
||||
image = ImageSources[Constant.ErrorIcon];
|
||||
image = ImageCache[Constant.ErrorIcon];
|
||||
path = Constant.ErrorIcon;
|
||||
}
|
||||
}
|
||||
@@ -159,12 +156,11 @@ namespace Wox.Infrastructure.Image
|
||||
}
|
||||
else
|
||||
{
|
||||
image = ImageSources[Constant.ErrorIcon];
|
||||
image = ImageCache[Constant.ErrorIcon];
|
||||
path = Constant.ErrorIcon;
|
||||
}
|
||||
}
|
||||
ImageSources[path] = image;
|
||||
_cache.Add(path);
|
||||
ImageCache[path] = image;
|
||||
image.Freeze();
|
||||
}
|
||||
return image;
|
||||
|
||||
Reference in New Issue
Block a user