4
4
using Flow . Launcher . Infrastructure . UserSettings ;
5
5
using MemoryPack ;
6
6
7
+ #nullable enable
8
+
7
9
namespace Flow . Launcher . Infrastructure . Storage
8
10
{
9
11
/// <summary>
@@ -15,48 +17,61 @@ namespace Flow.Launcher.Infrastructure.Storage
15
17
/// </remarks>
16
18
public class BinaryStorage < T >
17
19
{
20
+ protected T ? Data ;
21
+
18
22
public const string FileSuffix = ".cache" ;
19
23
24
+ protected string FilePath { get ; init ; } = null ! ;
25
+
26
+ protected string DirectoryPath { get ; init ; } = null ! ;
27
+
20
28
// Let the derived class to set the file path
21
- public BinaryStorage ( string filename , string directoryPath = null )
29
+ protected BinaryStorage ( )
22
30
{
23
- directoryPath ??= DataLocation . CacheDirectory ;
24
- Helper . ValidateDirectory ( directoryPath ) ;
25
-
26
- FilePath = Path . Combine ( directoryPath , $ "{ filename } { FileSuffix } ") ;
27
31
}
28
32
29
- public string FilePath { get ; }
33
+ public BinaryStorage ( string filename )
34
+ {
35
+ DirectoryPath = DataLocation . CacheDirectory ;
36
+ Helper . ValidateDirectory ( DirectoryPath ) ;
37
+
38
+ FilePath = Path . Combine ( DirectoryPath , $ "{ filename } { FileSuffix } ") ;
39
+ }
30
40
31
41
public async ValueTask < T > TryLoadAsync ( T defaultData )
32
42
{
43
+ if ( Data != null )
44
+ return Data ;
45
+
33
46
if ( File . Exists ( FilePath ) )
34
47
{
35
48
if ( new FileInfo ( FilePath ) . Length == 0 )
36
49
{
37
50
Log . Error ( $ "|BinaryStorage.TryLoad|Zero length cache file <{ FilePath } >") ;
38
- await SaveAsync ( defaultData ) ;
39
- return defaultData ;
51
+ Data = defaultData ;
52
+ await SaveAsync ( ) ;
40
53
}
41
54
42
55
await using var stream = new FileStream ( FilePath , FileMode . Open ) ;
43
56
var d = await DeserializeAsync ( stream , defaultData ) ;
44
- return d ;
57
+ Data = d ;
45
58
}
46
59
else
47
60
{
48
61
Log . Info ( "|BinaryStorage.TryLoad|Cache file not exist, load default data" ) ;
49
- await SaveAsync ( defaultData ) ;
50
- return defaultData ;
62
+ Data = defaultData ;
63
+ await SaveAsync ( ) ;
51
64
}
65
+
66
+ return Data ;
52
67
}
53
68
54
69
private static async ValueTask < T > DeserializeAsync ( Stream stream , T defaultData )
55
70
{
56
71
try
57
72
{
58
73
var t = await MemoryPackSerializer . DeserializeAsync < T > ( stream ) ;
59
- return t ;
74
+ return t ?? defaultData ;
60
75
}
61
76
catch ( System . Exception )
62
77
{
@@ -65,6 +80,27 @@ private static async ValueTask<T> DeserializeAsync(Stream stream, T defaultData)
65
80
}
66
81
}
67
82
83
+ public async ValueTask SaveAsync ( )
84
+ {
85
+ await using var stream = new FileStream ( FilePath , FileMode . Create ) ;
86
+ await MemoryPackSerializer . SerializeAsync ( stream , Data ) ;
87
+ }
88
+
89
+ // For SavePluginSettings function
90
+ public void Save ( )
91
+ {
92
+ var serialized = MemoryPackSerializer . Serialize ( Data ) ;
93
+
94
+ File . WriteAllBytes ( FilePath , serialized ) ;
95
+ }
96
+
97
+ // ImageCache need to be converted into concurrent dictionary, so it does not need to cache loading results into Data
98
+ public void ClearData ( )
99
+ {
100
+ Data = default ;
101
+ }
102
+
103
+ // ImageCache storages data in its class, so it needs to pass it to SaveAsync
68
104
public async ValueTask SaveAsync ( T data )
69
105
{
70
106
await using var stream = new FileStream ( FilePath , FileMode . Create ) ;
0 commit comments