PowerToys/Wox.Infrastructure/Image/ImageCache.cs

37 lines
1.0 KiB
C#
Raw Normal View History

2014-12-18 19:22:47 +08:00
using System;
using System.Collections.Concurrent;
2014-12-18 19:22:47 +08:00
using System.Linq;
namespace Wox.Infrastructure.Image
2014-12-18 19:22:47 +08:00
{
[Serializable]
public class ImageCache
2014-12-18 19:22:47 +08:00
{
private const int MaxCached = 200;
public ConcurrentDictionary<string, int> TopUsedImages = new ConcurrentDictionary<string, int>();
2014-12-18 19:22:47 +08:00
public void Add(string path)
{
if (TopUsedImages.ContainsKey(path))
{
TopUsedImages[path] = TopUsedImages[path] + 1;
2014-12-18 19:22:47 +08:00
}
else
{
TopUsedImages[path] = 1;
2014-12-18 19:22:47 +08:00
}
}
2014-12-18 19:22:47 +08:00
public void Cleanup()
{
if (TopUsedImages.Count > MaxCached)
2014-12-18 19:22:47 +08:00
{
var images = TopUsedImages.OrderByDescending(o => o.Value)
.Take(MaxCached)
.ToDictionary(i => i.Key, i => i.Value);
TopUsedImages = new ConcurrentDictionary<string, int>(images);
2014-12-18 19:22:47 +08:00
}
}
}
}