2014-12-18 19:22:47 +08:00
|
|
|
|
using System;
|
2016-04-21 23:37:40 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-01-13 15:40:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
using System.Linq;
|
2017-01-13 15:40:32 +00:00
|
|
|
|
using System.Windows.Media;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
|
2016-04-26 01:20:10 +01:00
|
|
|
|
namespace Wox.Infrastructure.Image
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
[Serializable]
|
2016-04-21 01:53:21 +01:00
|
|
|
|
public class ImageCache
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2018-03-31 09:19:55 +02:00
|
|
|
|
private const int MaxCached = 5000;
|
2017-01-13 15:40:32 +00:00
|
|
|
|
public ConcurrentDictionary<string, int> Usage = new ConcurrentDictionary<string, int>();
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, ImageSource> _data = new ConcurrentDictionary<string, ImageSource>();
|
2014-12-18 19:22:47 +08:00
|
|
|
|
|
2017-01-13 15:40:32 +00:00
|
|
|
|
|
|
|
|
|
|
public ImageSource this[string path]
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2017-01-13 15:40:32 +00:00
|
|
|
|
get
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2017-01-13 15:40:32 +00:00
|
|
|
|
Usage.AddOrUpdate(path, 1, (k, v) => v + 1);
|
|
|
|
|
|
var i = _data[path];
|
|
|
|
|
|
return i;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|
2017-01-13 15:40:32 +00:00
|
|
|
|
set { _data[path] = value; }
|
2016-05-22 05:30:38 +01:00
|
|
|
|
}
|
2014-12-18 19:22:47 +08:00
|
|
|
|
|
2016-05-22 05:30:38 +01:00
|
|
|
|
public void Cleanup()
|
|
|
|
|
|
{
|
2017-01-13 15:40:32 +00:00
|
|
|
|
var images = Usage
|
|
|
|
|
|
.OrderByDescending(o => o.Value)
|
|
|
|
|
|
.Take(MaxCached)
|
|
|
|
|
|
.ToDictionary(i => i.Key, i => i.Value);
|
|
|
|
|
|
Usage = new ConcurrentDictionary<string, int>(images);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool ContainsKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
var contains = _data.ContainsKey(key);
|
|
|
|
|
|
return contains;
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-01-13 15:40:32 +00:00
|
|
|
|
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|