2014-12-18 19:22:47 +08:00
|
|
|
|
using System;
|
2016-04-22 06:37:40 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2016-04-26 08:20:10 +08:00
|
|
|
|
namespace Wox.Infrastructure.Image
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public class ImageCache
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2016-04-22 06:37:40 +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))
|
|
|
|
|
{
|
2016-04-22 06:37:40 +08:00
|
|
|
|
TopUsedImages[path] = TopUsedImages[path] + 1;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-22 06:37:40 +08:00
|
|
|
|
TopUsedImages[path] = 1;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|
2016-05-22 12:30:38 +08:00
|
|
|
|
}
|
2014-12-18 19:22:47 +08:00
|
|
|
|
|
2016-05-22 12:30:38 +08:00
|
|
|
|
public void Cleanup()
|
|
|
|
|
{
|
2016-04-22 06:37:40 +08:00
|
|
|
|
if (TopUsedImages.Count > MaxCached)
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2016-04-22 06:37:40 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|