Skip to content

Commit 2432753

Browse files
committed
Add plugin json storage
1 parent 0e4e95a commit 2432753

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static async Task InitializeAsync()
3737
_hashGenerator = new ImageHashGenerator();
3838

3939
var usage = await LoadStorageToConcurrentDictionaryAsync();
40+
_storage.ClearData();
4041

4142
ImageCache.Initialize(usage);
4243

Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Flow.Launcher.Infrastructure.UserSettings;
55
using MemoryPack;
66

7+
#nullable enable
8+
79
namespace Flow.Launcher.Infrastructure.Storage
810
{
911
/// <summary>
@@ -15,48 +17,61 @@ namespace Flow.Launcher.Infrastructure.Storage
1517
/// </remarks>
1618
public class BinaryStorage<T>
1719
{
20+
protected T? Data;
21+
1822
public const string FileSuffix = ".cache";
1923

24+
protected string FilePath { get; init; } = null!;
25+
26+
protected string DirectoryPath { get; init; } = null!;
27+
2028
// Let the derived class to set the file path
21-
public BinaryStorage(string filename, string directoryPath = null)
29+
protected BinaryStorage()
2230
{
23-
directoryPath ??= DataLocation.CacheDirectory;
24-
Helper.ValidateDirectory(directoryPath);
25-
26-
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
2731
}
2832

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+
}
3040

3141
public async ValueTask<T> TryLoadAsync(T defaultData)
3242
{
43+
if (Data != null)
44+
return Data;
45+
3346
if (File.Exists(FilePath))
3447
{
3548
if (new FileInfo(FilePath).Length == 0)
3649
{
3750
Log.Error($"|BinaryStorage.TryLoad|Zero length cache file <{FilePath}>");
38-
await SaveAsync(defaultData);
39-
return defaultData;
51+
Data = defaultData;
52+
await SaveAsync();
4053
}
4154

4255
await using var stream = new FileStream(FilePath, FileMode.Open);
4356
var d = await DeserializeAsync(stream, defaultData);
44-
return d;
57+
Data = d;
4558
}
4659
else
4760
{
4861
Log.Info("|BinaryStorage.TryLoad|Cache file not exist, load default data");
49-
await SaveAsync(defaultData);
50-
return defaultData;
62+
Data = defaultData;
63+
await SaveAsync();
5164
}
65+
66+
return Data;
5267
}
5368

5469
private static async ValueTask<T> DeserializeAsync(Stream stream, T defaultData)
5570
{
5671
try
5772
{
5873
var t = await MemoryPackSerializer.DeserializeAsync<T>(stream);
59-
return t;
74+
return t ?? defaultData;
6075
}
6176
catch (System.Exception)
6277
{
@@ -65,6 +80,27 @@ private static async ValueTask<T> DeserializeAsync(Stream stream, T defaultData)
6580
}
6681
}
6782

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
68104
public async ValueTask SaveAsync(T data)
69105
{
70106
await using var stream = new FileStream(FilePath, FileMode.Create);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.IO;
2+
3+
namespace Flow.Launcher.Infrastructure.Storage
4+
{
5+
public class PluginBinaryStorage<T> : BinaryStorage<T> where T : new()
6+
{
7+
public PluginBinaryStorage(string cacheName, string cacheDirectory)
8+
{
9+
DirectoryPath = cacheDirectory;
10+
Helper.ValidateDirectory(DirectoryPath);
11+
12+
FilePath = Path.Combine(DirectoryPath, $"{cacheName}{FileSuffix}");
13+
}
14+
}
15+
}

Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ namespace Flow.Launcher.Infrastructure.Storage
1010

1111
public PluginJsonStorage()
1212
{
13-
// C# related, add python related below
1413
var dataType = typeof(T);
1514
AssemblyName = dataType.Assembly.GetName().Name;
1615
DirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, AssemblyName);
1716
Helper.ValidateDirectory(DirectoryPath);
1817

1918
FilePath = Path.Combine(DirectoryPath, $"{dataType.Name}{FileSuffix}");
2019
}
21-
22-
public PluginJsonStorage(T data) : this()
23-
{
24-
Data = data;
25-
}
2620
}
2721
}

0 commit comments

Comments
 (0)