From 6931c8d86cfe4b6b2ab40103047db1cc6bcd6a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 24 May 2021 10:21:39 +0800 Subject: [PATCH 1/3] add cache for program plugin --- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 48 ++++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index 5175970fd41..b58422d2cd1 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -26,10 +27,12 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I private static BinaryStorage _win32Storage; private static BinaryStorage _uwpStorage; - public Main() - { - - } + private static readonly ConcurrentDictionary> cache = new(); + + private static readonly List emptyResults = new(); + + private int reg_count; + private int query_count; public void Save() { @@ -42,23 +45,27 @@ public async Task> QueryAsync(Query query, CancellationToken token) if (IsStartupIndexProgramsRequired) _ = IndexPrograms(); - Win32[] win32; - UWP.Application[] uwps; + query_count++; - win32 = _win32s; - uwps = _uwps; - - var result = await Task.Run(delegate + if (!cache.TryGetValue(query.Search, out var result)) { - return win32.Cast() - .Concat(uwps) - .AsParallel() - .WithCancellation(token) - .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, _context.API)) - .Where(r => r?.Score > 0) - .ToList(); - }, token).ConfigureAwait(false); + result = await Task.Run(delegate + { + return _win32s.Cast() + .Concat(_uwps) + .AsParallel() + .WithCancellation(token) + .Where(p => p.Enabled) + .Select(p => p.Result(query.Search, _context.API)) + .Where(r => r?.Score > 0) + .ToList(); + }, token).ConfigureAwait(false); + + if (result.Count == 0) + result = emptyResults; + + cache[query.Search] = result; + } token.ThrowIfCancellationRequested(); @@ -110,7 +117,7 @@ public async Task InitAsync(PluginInitContext context) if (indexedWinApps && indexedUWPApps) _settings.LastIndexTime = DateTime.Today; }); - + if (!(_win32s.Any() && _uwps.Any())) await indexTask; } @@ -134,6 +141,7 @@ public static async Task IndexPrograms() var t1 = Task.Run(IndexWin32Programs); var t2 = Task.Run(IndexUwpPrograms); await Task.WhenAll(t1, t2).ConfigureAwait(false); + cache.Clear(); _settings.LastIndexTime = DateTime.Today; } From 9b1180fc892914e62fb0f849c1f75fd8d2b7b2b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Mon, 24 May 2021 11:14:20 +0800 Subject: [PATCH 2/3] remove testing code --- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index b58422d2cd1..c954cab8eab 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -31,8 +31,6 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I private static readonly List emptyResults = new(); - private int reg_count; - private int query_count; public void Save() { @@ -45,7 +43,6 @@ public async Task> QueryAsync(Query query, CancellationToken token) if (IsStartupIndexProgramsRequired) _ = IndexPrograms(); - query_count++; if (!cache.TryGetValue(query.Search, out var result)) { From 8eecbfbed3fdba7fd531ff159fda392bb87b8cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 27 May 2021 19:28:17 +0800 Subject: [PATCH 3/3] Use MemoryCache to control size --- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 57 ++++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index c954cab8eab..b7fe1373f61 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Specialized; using System.Diagnostics; using System.Linq; using System.Threading; @@ -10,6 +11,9 @@ using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Plugin.Program.Programs; using Flow.Launcher.Plugin.Program.Views; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Primitives; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; namespace Flow.Launcher.Plugin.Program @@ -27,10 +31,17 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I private static BinaryStorage _win32Storage; private static BinaryStorage _uwpStorage; - private static readonly ConcurrentDictionary> cache = new(); - private static readonly List emptyResults = new(); + private static readonly MemoryCacheOptions cacheOptions = new() + { + SizeLimit = 1560 + }; + private static MemoryCache cache = new(cacheOptions); + + static Main() + { + } public void Save() { @@ -40,31 +51,29 @@ public void Save() public async Task> QueryAsync(Query query, CancellationToken token) { + if (IsStartupIndexProgramsRequired) _ = IndexPrograms(); + var result = await cache.GetOrCreateAsync(query.Search, async entry => + { + var resultList = await Task.Run(() => + _win32s.Cast() + .Concat(_uwps) + .AsParallel() + .WithCancellation(token) + .Where(p => p.Enabled) + .Select(p => p.Result(query.Search, _context.API)) + .Where(r => r?.Score > 0) + .ToList()); - if (!cache.TryGetValue(query.Search, out var result)) - { - result = await Task.Run(delegate - { - return _win32s.Cast() - .Concat(_uwps) - .AsParallel() - .WithCancellation(token) - .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, _context.API)) - .Where(r => r?.Score > 0) - .ToList(); - }, token).ConfigureAwait(false); - - if (result.Count == 0) - result = emptyResults; - - cache[query.Search] = result; - } + resultList = resultList.Any() ? resultList : emptyResults; + + entry.SetSize(resultList.Count); + entry.SetSlidingExpiration(TimeSpan.FromHours(8)); - token.ThrowIfCancellationRequested(); + return resultList; + }); return result; } @@ -138,7 +147,9 @@ public static async Task IndexPrograms() var t1 = Task.Run(IndexWin32Programs); var t2 = Task.Run(IndexUwpPrograms); await Task.WhenAll(t1, t2).ConfigureAwait(false); - cache.Clear(); + var oldCache = cache; + cache = new MemoryCache(cacheOptions); + oldCache.Dispose(); _settings.LastIndexTime = DateTime.Today; }