11using System ;
22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
4+ using System . Collections . Specialized ;
45using System . Diagnostics ;
56using System . Linq ;
67using System . Threading ;
1011using Flow . Launcher . Infrastructure . Storage ;
1112using Flow . Launcher . Plugin . Program . Programs ;
1213using Flow . Launcher . Plugin . Program . Views ;
14+ using Microsoft . Extensions . Caching . Memory ;
15+ using Microsoft . Extensions . Configuration ;
16+ using Microsoft . Extensions . Primitives ;
1317using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
1418
1519namespace Flow . Launcher . Plugin . Program
@@ -27,10 +31,17 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
2731 private static BinaryStorage < Win32 [ ] > _win32Storage ;
2832 private static BinaryStorage < UWP . Application [ ] > _uwpStorage ;
2933
30- private static readonly ConcurrentDictionary < string , List < Result > > cache = new ( ) ;
31-
3234 private static readonly List < Result > emptyResults = new ( ) ;
3335
36+ private static readonly MemoryCacheOptions cacheOptions = new ( )
37+ {
38+ SizeLimit = 1560
39+ } ;
40+ private static MemoryCache cache = new ( cacheOptions ) ;
41+
42+ static Main ( )
43+ {
44+ }
3445
3546 public void Save ( )
3647 {
@@ -40,31 +51,29 @@ public void Save()
4051
4152 public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
4253 {
54+
4355 if ( IsStartupIndexProgramsRequired )
4456 _ = IndexPrograms ( ) ;
4557
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 ( ) ) ;
4669
47- if ( ! cache . TryGetValue ( query . Search , out var result ) )
48- {
49- result = await Task . Run ( delegate
50- {
51- return _win32s . Cast < IProgram > ( )
52- . Concat ( _uwps )
53- . AsParallel ( )
54- . WithCancellation ( token )
55- . Where ( p => p . Enabled )
56- . Select ( p => p . Result ( query . Search , _context . API ) )
57- . Where ( r => r ? . Score > 0 )
58- . ToList ( ) ;
59- } , token ) . ConfigureAwait ( false ) ;
60-
61- if ( result . Count == 0 )
62- result = emptyResults ;
63-
64- cache [ query . Search ] = result ;
65- }
70+ resultList = resultList . Any ( ) ? resultList : emptyResults ;
71+
72+ entry . SetSize ( resultList . Count ) ;
73+ entry . SetSlidingExpiration ( TimeSpan . FromHours ( 8 ) ) ;
6674
67- token . ThrowIfCancellationRequested ( ) ;
75+ return resultList ;
76+ } ) ;
6877
6978 return result ;
7079 }
@@ -138,7 +147,9 @@ public static async Task IndexPrograms()
138147 var t1 = Task . Run ( IndexWin32Programs ) ;
139148 var t2 = Task . Run ( IndexUwpPrograms ) ;
140149 await Task . WhenAll ( t1 , t2 ) . ConfigureAwait ( false ) ;
141- cache . Clear ( ) ;
150+ var oldCache = cache ;
151+ cache = new MemoryCache ( cacheOptions ) ;
152+ oldCache . Dispose ( ) ;
142153 _settings . LastIndexTime = DateTime . Today ;
143154 }
144155
0 commit comments