1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Diagnostics ;
4
5
using System . Linq ;
@@ -26,10 +27,12 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
26
27
private static BinaryStorage < Win32 [ ] > _win32Storage ;
27
28
private static BinaryStorage < UWP . Application [ ] > _uwpStorage ;
28
29
29
- public Main ( )
30
- {
31
-
32
- }
30
+ private static readonly ConcurrentDictionary < string , List < Result > > cache = new ( ) ;
31
+
32
+ private static readonly List < Result > emptyResults = new ( ) ;
33
+
34
+ private int reg_count ;
35
+ private int query_count ;
33
36
34
37
public void Save ( )
35
38
{
@@ -42,23 +45,27 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
42
45
if ( IsStartupIndexProgramsRequired )
43
46
_ = IndexPrograms ( ) ;
44
47
45
- Win32 [ ] win32 ;
46
- UWP . Application [ ] uwps ;
48
+ query_count ++ ;
47
49
48
- win32 = _win32s ;
49
- uwps = _uwps ;
50
-
51
- var result = await Task . Run ( delegate
50
+ if ( ! cache . TryGetValue ( query . Search , out var result ) )
52
51
{
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 ) ;
52
+ result = await Task . Run ( delegate
53
+ {
54
+ return _win32s . Cast < IProgram > ( )
55
+ . Concat ( _uwps )
56
+ . AsParallel ( )
57
+ . WithCancellation ( token )
58
+ . Where ( p => p . Enabled )
59
+ . Select ( p => p . Result ( query . Search , _context . API ) )
60
+ . Where ( r => r ? . Score > 0 )
61
+ . ToList ( ) ;
62
+ } , token ) . ConfigureAwait ( false ) ;
63
+
64
+ if ( result . Count == 0 )
65
+ result = emptyResults ;
66
+
67
+ cache [ query . Search ] = result ;
68
+ }
62
69
63
70
token . ThrowIfCancellationRequested ( ) ;
64
71
@@ -110,7 +117,7 @@ public async Task InitAsync(PluginInitContext context)
110
117
if ( indexedWinApps && indexedUWPApps )
111
118
_settings . LastIndexTime = DateTime . Today ;
112
119
} ) ;
113
-
120
+
114
121
if ( ! ( _win32s . Any ( ) && _uwps . Any ( ) ) )
115
122
await indexTask ;
116
123
}
@@ -134,6 +141,7 @@ public static async Task IndexPrograms()
134
141
var t1 = Task . Run ( IndexWin32Programs ) ;
135
142
var t2 = Task . Run ( IndexUwpPrograms ) ;
136
143
await Task . WhenAll ( t1 , t2 ) . ConfigureAwait ( false ) ;
144
+ cache . Clear ( ) ;
137
145
_settings . LastIndexTime = DateTime . Today ;
138
146
}
139
147
0 commit comments