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.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Wox.Infrastructure.Storage;
|
|
|
|
|
|
|
|
|
|
namespace Wox.ImageLoader
|
|
|
|
|
{
|
|
|
|
|
[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-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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|