mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
Refactoring image cache
use parallel linq to preload images, should be faster
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Wox.Infrastructure.Storage;
|
||||
@@ -8,39 +9,26 @@ namespace Wox.ImageLoader
|
||||
[Serializable]
|
||||
public class ImageCache
|
||||
{
|
||||
private int counter;
|
||||
private const int maxCached = 200;
|
||||
public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>();
|
||||
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 ;
|
||||
TopUsedImages[path] = TopUsedImages[path] + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
TopUsedImages.Add(path, 1);
|
||||
TopUsedImages[path] = 1;
|
||||
}
|
||||
|
||||
if (TopUsedImages.Count > maxCached)
|
||||
if (TopUsedImages.Count > MaxCached)
|
||||
{
|
||||
TopUsedImages = TopUsedImages.OrderByDescending(o => o.Value)
|
||||
.Take(maxCached)
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
}
|
||||
|
||||
if (++counter == 30)
|
||||
{
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(string path)
|
||||
{
|
||||
if (TopUsedImages.ContainsKey(path))
|
||||
{
|
||||
TopUsedImages.Remove(path);
|
||||
var images = TopUsedImages.OrderByDescending(o => o.Value)
|
||||
.Take(MaxCached)
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
TopUsedImages = new ConcurrentDictionary<string, int>(images);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user