PowerToys/Wox/ImageLoader/ImageCache.cs
bao-qian 03051a95cf Refactoring image cache
use parallel linq to preload images, should be faster
2016-04-22 01:42:37 +01:00

36 lines
1.0 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Wox.Infrastructure.Storage;
namespace Wox.ImageLoader
{
[Serializable]
public class ImageCache
{
private const int MaxCached = 200;
public ConcurrentDictionary<string, int> TopUsedImages = new ConcurrentDictionary<string, int>();
public void Add(string path)
{
if (TopUsedImages.ContainsKey(path))
{
TopUsedImages[path] = TopUsedImages[path] + 1;
}
else
{
TopUsedImages[path] = 1;
}
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);
}
}
}
}