add cache for program plugin

This commit is contained in:
弘韬 张 2021-05-24 10:21:39 +08:00
parent d813a4732d
commit 6931c8d86c

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -26,10 +27,12 @@ namespace Flow.Launcher.Plugin.Program
private static BinaryStorage<Win32[]> _win32Storage; private static BinaryStorage<Win32[]> _win32Storage;
private static BinaryStorage<UWP.Application[]> _uwpStorage; private static BinaryStorage<UWP.Application[]> _uwpStorage;
public Main() private static readonly ConcurrentDictionary<string, List<Result>> cache = new();
{
private static readonly List<Result> emptyResults = new();
}
private int reg_count;
private int query_count;
public void Save() public void Save()
{ {
@ -42,23 +45,27 @@ namespace Flow.Launcher.Plugin.Program
if (IsStartupIndexProgramsRequired) if (IsStartupIndexProgramsRequired)
_ = IndexPrograms(); _ = IndexPrograms();
Win32[] win32; query_count++;
UWP.Application[] uwps;
win32 = _win32s; if (!cache.TryGetValue(query.Search, out var result))
uwps = _uwps;
var result = await Task.Run(delegate
{ {
return win32.Cast<IProgram>() result = await Task.Run(delegate
.Concat(uwps) {
.AsParallel() return _win32s.Cast<IProgram>()
.WithCancellation(token) .Concat(_uwps)
.Where(p => p.Enabled) .AsParallel()
.Select(p => p.Result(query.Search, _context.API)) .WithCancellation(token)
.Where(r => r?.Score > 0) .Where(p => p.Enabled)
.ToList(); .Select(p => p.Result(query.Search, _context.API))
}, token).ConfigureAwait(false); .Where(r => r?.Score > 0)
.ToList();
}, token).ConfigureAwait(false);
if (result.Count == 0)
result = emptyResults;
cache[query.Search] = result;
}
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
@ -110,7 +117,7 @@ namespace Flow.Launcher.Plugin.Program
if (indexedWinApps && indexedUWPApps) if (indexedWinApps && indexedUWPApps)
_settings.LastIndexTime = DateTime.Today; _settings.LastIndexTime = DateTime.Today;
}); });
if (!(_win32s.Any() && _uwps.Any())) if (!(_win32s.Any() && _uwps.Any()))
await indexTask; await indexTask;
} }
@ -134,6 +141,7 @@ namespace Flow.Launcher.Plugin.Program
var t1 = Task.Run(IndexWin32Programs); var t1 = Task.Run(IndexWin32Programs);
var t2 = Task.Run(IndexUwpPrograms); var t2 = Task.Run(IndexUwpPrograms);
await Task.WhenAll(t1, t2).ConfigureAwait(false); await Task.WhenAll(t1, t2).ConfigureAwait(false);
cache.Clear();
_settings.LastIndexTime = DateTime.Today; _settings.LastIndexTime = DateTime.Today;
} }