1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
4
+ using System . Collections . Specialized ;
3
5
using System . Diagnostics ;
4
6
using System . Linq ;
5
7
using System . Threading ;
9
11
using Flow . Launcher . Infrastructure . Storage ;
10
12
using Flow . Launcher . Plugin . Program . Programs ;
11
13
using Flow . Launcher . Plugin . Program . Views ;
14
+ using Microsoft . Extensions . Caching . Memory ;
15
+ using Microsoft . Extensions . Configuration ;
16
+ using Microsoft . Extensions . Primitives ;
12
17
using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
13
18
14
19
namespace Flow . Launcher . Plugin . Program
@@ -26,9 +31,16 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
26
31
private static BinaryStorage < Win32 [ ] > _win32Storage ;
27
32
private static BinaryStorage < UWP . Application [ ] > _uwpStorage ;
28
33
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 ( )
30
43
{
31
-
32
44
}
33
45
34
46
public void Save ( )
@@ -39,28 +51,29 @@ public void Save()
39
51
40
52
public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
41
53
{
54
+
42
55
if ( IsStartupIndexProgramsRequired )
43
56
_ = IndexPrograms ( ) ;
44
57
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 ( ) ) ;
47
69
48
- win32 = _win32s ;
49
- uwps = _uwps ;
70
+ resultList = resultList . Any ( ) ? resultList : emptyResults ;
50
71
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
+ } ) ;
64
77
65
78
return result ;
66
79
}
@@ -110,7 +123,7 @@ public async Task InitAsync(PluginInitContext context)
110
123
if ( indexedWinApps && indexedUWPApps )
111
124
_settings . LastIndexTime = DateTime . Today ;
112
125
} ) ;
113
-
126
+
114
127
if ( ! ( _win32s . Any ( ) && _uwps . Any ( ) ) )
115
128
await indexTask ;
116
129
}
@@ -134,6 +147,9 @@ public static async Task IndexPrograms()
134
147
var t1 = Task . Run ( IndexWin32Programs ) ;
135
148
var t2 = Task . Run ( IndexUwpPrograms ) ;
136
149
await Task . WhenAll ( t1 , t2 ) . ConfigureAwait ( false ) ;
150
+ var oldCache = cache ;
151
+ cache = new MemoryCache ( cacheOptions ) ;
152
+ oldCache . Dispose ( ) ;
137
153
_settings . LastIndexTime = DateTime . Today ;
138
154
}
139
155
0 commit comments