mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
* CA1052: Static holder types should be Static or NotInheritable * CA1041: Provide ObsoleteAttribute message * CA1062: Validate arguments of public methods * CA1304: Specify CultureInfo / CA1305: Specify IFormatProvider / CA1307: Specify StringComparison for clarity * CA1802: Use Literals Where Appropriate * CA1820: Test for empty strings using string length * CA1707: Identifiers should not contain underscores * CA1805: Do not initialize unnecessarily. * CA1822: Mark members as static * CA2227: Collection properties should be read only * CA1054: URI parameters should not be strings * CA1031: Do not catch general exception types * CA1060: Move P/Invokes to NativeMethods class * CA1308: Normalize strings to uppercase * CA2000: Dispose objects before losing scope / CA2234: Pass System.Uri objects instead of strings * CA2234: Pass System.Uri objects instead of strings * CA1044: Properties should not be write only * CA1716: Identifiers should not match keywords * CA2007: Do not directly await a Task * CA2007: Do not directly await a Task (Suppressed) * CA5350: Do Not Use Weak Cryptographic Algorithms (Suppressed) * CA1724: Type names should not match namespaces (renamed Settings.cs to PowerToysRunSettings.cs) * CA1033: Interface methods should be callable by child types (Added sealed modifier to class) * CA1724: Type names should not match namespaces (Renamed Plugin.cs to RunPlugin.cs) * CA1724: Type names should not match namespaces (Renamed Http.cs to HttpClient.cs) * CA5364: Do not use deprecated security protocols (Remove unused code) * Enabled FxCopAnalyzer for Wox.Infrastructure * fixed comment * Addressed comments - Changed Ordinal to InvariantCulture - Added comments - Removed unused obsolete code - Removed unused method (CA2007: Do not directly await a Task) * Addressed comments - fixed justification for CA1031 suppression * Addressed comments - Fixed justification for CA1031 suppression in Wox.Core/Wox.Plugin
96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows.Media;
|
|
using Wox.Plugin;
|
|
|
|
namespace Wox.Infrastructure.Image
|
|
{
|
|
[Serializable]
|
|
public class ImageCache
|
|
{
|
|
private const int MaxCached = 50;
|
|
private const int PermissibleFactor = 2;
|
|
|
|
private readonly ConcurrentDictionary<string, ImageSource> _data = new ConcurrentDictionary<string, ImageSource>();
|
|
|
|
public ConcurrentDictionary<string, int> Usage { get; private set; } = new ConcurrentDictionary<string, int>();
|
|
|
|
public ImageSource this[string path]
|
|
{
|
|
get
|
|
{
|
|
Usage.AddOrUpdate(path, 1, (k, v) => v + 1);
|
|
var i = _data[path];
|
|
return i;
|
|
}
|
|
|
|
set
|
|
{
|
|
_data[path] = value;
|
|
|
|
// To prevent the dictionary from drastically increasing in size by caching images, the dictionary size is not allowed to grow more than the permissibleFactor * maxCached size
|
|
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
|
|
if (_data.Count > PermissibleFactor * MaxCached)
|
|
{
|
|
// This function resizes the Usage dictionary, taking the top 'maxCached' number of items and filtering the image icons that are not accessed frequently.
|
|
Cleanup();
|
|
|
|
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
|
|
foreach (var key in _data.Keys)
|
|
{
|
|
// Using Ordinal since this is internal
|
|
if (!Usage.TryGetValue(key, out _) && !(key.Equals(Constant.ErrorIcon, StringComparison.Ordinal) || key.Equals(Constant.DefaultIcon, StringComparison.Ordinal) || key.Equals(Constant.LightThemedErrorIcon, StringComparison.Ordinal) || key.Equals(Constant.LightThemedDefaultIcon, StringComparison.Ordinal)))
|
|
{
|
|
_data.TryRemove(key, out _);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Cleanup()
|
|
{
|
|
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;
|
|
}
|
|
|
|
public int CacheSize()
|
|
{
|
|
return _data.Count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// return the number of unique images in the cache (by reference not by checking images content)
|
|
/// </summary>
|
|
public int UniqueImagesInCache()
|
|
{
|
|
return _data.Values.Distinct().Count();
|
|
}
|
|
|
|
public Dictionary<string, int> GetUsageAsDictionary()
|
|
{
|
|
return new Dictionary<string, int>(Usage);
|
|
}
|
|
|
|
public void SetUsageAsDictionary(Dictionary<string, int> usage)
|
|
{
|
|
Usage = new ConcurrentDictionary<string, int>(usage);
|
|
}
|
|
}
|
|
}
|