11using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
4+ using System . Collections . Specialized ;
35using System . Diagnostics ;
46using System . Linq ;
57using System . Threading ;
911using Flow . Launcher . Infrastructure . Storage ;
1012using Flow . Launcher . Plugin . Program . Programs ;
1113using Flow . Launcher . Plugin . Program . Views ;
14+ using Microsoft . Extensions . Caching . Memory ;
15+ using Microsoft . Extensions . Configuration ;
16+ using Microsoft . Extensions . Primitives ;
1217using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
1318
1419namespace Flow . Launcher . Plugin . Program
@@ -26,9 +31,16 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
2631 private static BinaryStorage < Win32 [ ] > _win32Storage ;
2732 private static BinaryStorage < UWP . Application [ ] > _uwpStorage ;
2833
29- public Main ( )
34+ private static readonly List < Result > emptyResults = new ( ) ;
35+
36+ private static readonly MemoryCacheOptions cacheOptions = new ( )
37+ {
38+ SizeLimit = 1560
39+ } ;
40+ private static MemoryCache cache = new ( cacheOptions ) ;
41+
42+ static Main ( )
3043 {
31-
3244 }
3345
3446 public void Save ( )
@@ -39,28 +51,29 @@ public void Save()
3951
4052 public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
4153 {
54+
4255 if ( IsStartupIndexProgramsRequired )
4356 _ = IndexPrograms ( ) ;
4457
45- Win32 [ ] win32 ;
46- UWP . Application [ ] uwps ;
58+ var result = await cache . GetOrCreateAsync ( query . Search , async entry =>
59+ {
60+ var resultList = await Task . Run ( ( ) =>
61+ _win32s . Cast < IProgram > ( )
62+ . Concat ( _uwps )
63+ . AsParallel ( )
64+ . WithCancellation ( token )
65+ . Where ( p => p . Enabled )
66+ . Select ( p => p . Result ( query . Search , _context . API ) )
67+ . Where ( r => r ? . Score > 0 )
68+ . ToList ( ) ) ;
4769
48- win32 = _win32s ;
49- uwps = _uwps ;
70+ resultList = resultList . Any ( ) ? resultList : emptyResults ;
5071
51- var result = await Task . Run ( delegate
52- {
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 ) ;
62-
63- token . ThrowIfCancellationRequested ( ) ;
72+ entry . SetSize ( resultList . Count ) ;
73+ entry . SetSlidingExpiration ( TimeSpan . FromHours ( 8 ) ) ;
74+
75+ return resultList ;
76+ } ) ;
6477
6578 return result ;
6679 }
@@ -110,7 +123,7 @@ public async Task InitAsync(PluginInitContext context)
110123 if ( indexedWinApps && indexedUWPApps )
111124 _settings . LastIndexTime = DateTime . Today ;
112125 } ) ;
113-
126+
114127 if ( ! ( _win32s . Any ( ) && _uwps . Any ( ) ) )
115128 await indexTask ;
116129 }
@@ -134,6 +147,9 @@ public static async Task IndexPrograms()
134147 var t1 = Task . Run ( IndexWin32Programs ) ;
135148 var t2 = Task . Run ( IndexUwpPrograms ) ;
136149 await Task . WhenAll ( t1 , t2 ) . ConfigureAwait ( false ) ;
150+ var oldCache = cache ;
151+ cache = new MemoryCache ( cacheOptions ) ;
152+ oldCache . Dispose ( ) ;
137153 _settings . LastIndexTime = DateTime . Today ;
138154 }
139155
0 commit comments