2014-12-18 19:22:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using System.IO;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Wox.Infrastructure.Storage;
|
|
|
|
|
|
|
|
|
|
namespace Wox.ImageLoader
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class ImageCacheStroage : BinaryStorage<ImageCacheStroage>
|
|
|
|
|
{
|
2014-12-26 22:51:04 +08:00
|
|
|
|
private int counter = 0;
|
|
|
|
|
private const int maxCached = 200;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>();
|
|
|
|
|
|
2015-01-05 22:41:17 +08:00
|
|
|
|
protected override string ConfigFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
|
|
|
|
|
if (userProfilePath == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Environment variable USERPROFILE is empty");
|
|
|
|
|
}
|
|
|
|
|
return Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Config");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 19:22:47 +08:00
|
|
|
|
protected override string ConfigName
|
|
|
|
|
{
|
|
|
|
|
get { return "ImageCache"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(string path)
|
|
|
|
|
{
|
|
|
|
|
if (TopUsedImages.ContainsKey(path))
|
|
|
|
|
{
|
|
|
|
|
TopUsedImages[path] = TopUsedImages[path] + 1 ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TopUsedImages.Add(path, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TopUsedImages.Count > maxCached)
|
|
|
|
|
{
|
|
|
|
|
TopUsedImages = TopUsedImages.OrderByDescending(o => o.Value)
|
|
|
|
|
.Take(maxCached)
|
|
|
|
|
.ToDictionary(i => i.Key, i => i.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (++counter == 30)
|
|
|
|
|
{
|
|
|
|
|
counter = 0;
|
|
|
|
|
Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(string path)
|
|
|
|
|
{
|
|
|
|
|
if (TopUsedImages.ContainsKey(path))
|
|
|
|
|
{
|
|
|
|
|
TopUsedImages.Remove(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|