1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . Specialized ;
4
5
using System . Diagnostics ;
5
6
using System . Linq ;
6
7
using System . Threading ;
10
11
using Flow . Launcher . Infrastructure . Storage ;
11
12
using Flow . Launcher . Plugin . Program . Programs ;
12
13
using Flow . Launcher . Plugin . Program . Views ;
14
+ using Microsoft . Extensions . Caching . Memory ;
15
+ using Microsoft . Extensions . Configuration ;
16
+ using Microsoft . Extensions . Primitives ;
13
17
using Stopwatch = Flow . Launcher . Infrastructure . Stopwatch ;
14
18
15
19
namespace Flow . Launcher . Plugin . Program
@@ -27,10 +31,17 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
27
31
private static BinaryStorage < Win32 [ ] > _win32Storage ;
28
32
private static BinaryStorage < UWP . Application [ ] > _uwpStorage ;
29
33
30
- private static readonly ConcurrentDictionary < string , List < Result > > cache = new ( ) ;
31
-
32
34
private static readonly List < Result > emptyResults = new ( ) ;
33
35
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
+ }
34
45
35
46
public void Save ( )
36
47
{
@@ -40,31 +51,29 @@ public void Save()
40
51
41
52
public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
42
53
{
54
+
43
55
if ( IsStartupIndexProgramsRequired )
44
56
_ = IndexPrograms ( ) ;
45
57
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 ( ) ) ;
46
69
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 ) ) ;
66
74
67
- token . ThrowIfCancellationRequested ( ) ;
75
+ return resultList ;
76
+ } ) ;
68
77
69
78
return result ;
70
79
}
@@ -138,7 +147,9 @@ public static async Task IndexPrograms()
138
147
var t1 = Task . Run ( IndexWin32Programs ) ;
139
148
var t2 = Task . Run ( IndexUwpPrograms ) ;
140
149
await Task . WhenAll ( t1 , t2 ) . ConfigureAwait ( false ) ;
141
- cache . Clear ( ) ;
150
+ var oldCache = cache ;
151
+ cache = new MemoryCache ( cacheOptions ) ;
152
+ oldCache . Dispose ( ) ;
142
153
_settings . LastIndexTime = DateTime . Today ;
143
154
}
144
155
0 commit comments