PowerToys/Wox.Infrastructure/Image/ImageCache.cs

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