Skip to content

Commit 6931c8d

Browse files
committed
add cache for program plugin
1 parent d813a47 commit 6931c8d

File tree

1 file changed

+28
-20
lines changed
  • Plugins/Flow.Launcher.Plugin.Program

1 file changed

+28
-20
lines changed

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
@@ -26,10 +27,12 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
2627
private static BinaryStorage<Win32[]> _win32Storage;
2728
private static BinaryStorage<UWP.Application[]> _uwpStorage;
2829

29-
public Main()
30-
{
31-
32-
}
30+
private static readonly ConcurrentDictionary<string, List<Result>> cache = new();
31+
32+
private static readonly List<Result> emptyResults = new();
33+
34+
private int reg_count;
35+
private int query_count;
3336

3437
public void Save()
3538
{
@@ -42,23 +45,27 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
4245
if (IsStartupIndexProgramsRequired)
4346
_ = IndexPrograms();
4447

45-
Win32[] win32;
46-
UWP.Application[] uwps;
48+
query_count++;
4749

48-
win32 = _win32s;
49-
uwps = _uwps;
50-
51-
var result = await Task.Run(delegate
50+
if (!cache.TryGetValue(query.Search, out var result))
5251
{
53-
return win32.Cast<IProgram>()
54-
.Concat(uwps)
55-
.AsParallel()
56-
.WithCancellation(token)
57-
.Where(p => p.Enabled)
58-
.Select(p => p.Result(query.Search, _context.API))
59-
.Where(r => r?.Score > 0)
60-
.ToList();
61-
}, token).ConfigureAwait(false);
52+
result = await Task.Run(delegate
53+
{
54+
return _win32s.Cast<IProgram>()
55+
.Concat(_uwps)
56+
.AsParallel()
57+
.WithCancellation(token)
58+
.Where(p => p.Enabled)
59+
.Select(p => p.Result(query.Search, _context.API))
60+
.Where(r => r?.Score > 0)
61+
.ToList();
62+
}, token).ConfigureAwait(false);
63+
64+
if (result.Count == 0)
65+
result = emptyResults;
66+
67+
cache[query.Search] = result;
68+
}
6269

6370
token.ThrowIfCancellationRequested();
6471

@@ -110,7 +117,7 @@ public async Task InitAsync(PluginInitContext context)
110117
if (indexedWinApps && indexedUWPApps)
111118
_settings.LastIndexTime = DateTime.Today;
112119
});
113-
120+
114121
if (!(_win32s.Any() && _uwps.Any()))
115122
await indexTask;
116123
}
@@ -134,6 +141,7 @@ public static async Task IndexPrograms()
134141
var t1 = Task.Run(IndexWin32Programs);
135142
var t2 = Task.Run(IndexUwpPrograms);
136143
await Task.WhenAll(t1, t2).ConfigureAwait(false);
144+
cache.Clear();
137145
_settings.LastIndexTime = DateTime.Today;
138146
}
139147

0 commit comments

Comments
 (0)