Merge pull request #451 from Flow-Launcher/cacheProgramResult

Cache program result
This commit is contained in:
Jeremy Wu 2021-09-23 12:21:25 +10:00 committed by GitHub
commit f3398c506b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +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;
@ -9,6 +11,9 @@ using Flow.Launcher.Infrastructure.Logger;
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
@ -26,9 +31,16 @@ namespace Flow.Launcher.Plugin.Program
private static BinaryStorage<Win32[]> _win32Storage;
private static BinaryStorage<UWP.Application[]> _uwpStorage;
public Main()
private static readonly List<Result> emptyResults = new();
private static readonly MemoryCacheOptions cacheOptions = new()
{
SizeLimit = 1560
};
private static MemoryCache cache = new(cacheOptions);
static Main()
{
}
public void Save()
@ -39,28 +51,29 @@ namespace Flow.Launcher.Plugin.Program
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
if (IsStartupIndexProgramsRequired)
_ = IndexPrograms();
Win32[] win32;
UWP.Application[] uwps;
var result = await cache.GetOrCreateAsync(query.Search, async entry =>
{
var resultList = await Task.Run(() =>
_win32s.Cast<IProgram>()
.Concat(_uwps)
.AsParallel()
.WithCancellation(token)
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API))
.Where(r => r?.Score > 0)
.ToList());
win32 = _win32s;
uwps = _uwps;
resultList = resultList.Any() ? resultList : emptyResults;
var result = await Task.Run(delegate
{
return win32.Cast<IProgram>()
.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);
entry.SetSize(resultList.Count);
entry.SetSlidingExpiration(TimeSpan.FromHours(8));
token.ThrowIfCancellationRequested();
return resultList;
});
return result;
}
@ -110,7 +123,7 @@ namespace Flow.Launcher.Plugin.Program
if (indexedWinApps && indexedUWPApps)
_settings.LastIndexTime = DateTime.Today;
});
if (!(_win32s.Any() && _uwps.Any()))
await indexTask;
}
@ -134,6 +147,9 @@ namespace Flow.Launcher.Plugin.Program
var t1 = Task.Run(IndexWin32Programs);
var t2 = Task.Run(IndexUwpPrograms);
await Task.WhenAll(t1, t2).ConfigureAwait(false);
var oldCache = cache;
cache = new MemoryCache(cacheOptions);
oldCache.Dispose();
_settings.LastIndexTime = DateTime.Today;
}