Flow.Launcher/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs

129 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Threading.Tasks;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using MemoryPack;
2025-04-01 06:16:32 +00:00
#nullable enable
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure.Storage
{
/// <summary>
/// Stroage object using binary data
/// Normally, it has better performance, but not readable
/// </summary>
/// <remarks>
2025-04-08 11:42:59 +00:00
/// It utilizes MemoryPack, which means the object must be MemoryPackSerializable <see href="https://github.com/Cysharp/MemoryPack"/>
/// </remarks>
public class BinaryStorage<T> : ISavable
{
2025-04-13 09:11:36 +00:00
private static readonly string ClassName = "BinaryStorage";
2025-04-01 06:16:32 +00:00
protected T? Data;
2025-02-24 07:15:02 +00:00
public const string FileSuffix = ".cache";
2025-04-01 06:16:32 +00:00
protected string FilePath { get; init; } = null!;
protected string DirectoryPath { get; init; } = null!;
2025-02-24 07:15:02 +00:00
// Let the derived class to set the file path
2025-04-01 06:16:32 +00:00
protected BinaryStorage()
{
}
2025-04-01 06:16:32 +00:00
public BinaryStorage(string filename)
{
DirectoryPath = DataLocation.CacheDirectory;
FilesFolders.ValidateDirectory(DirectoryPath);
2025-04-01 06:16:32 +00:00
FilePath = Path.Combine(DirectoryPath, $"{filename}{FileSuffix}");
}
// Let the old Program plugin get this constructor
[Obsolete("This constructor is obsolete. Use BinaryStorage(string filename) instead.")]
public BinaryStorage(string filename, string directoryPath = null!)
{
2025-04-11 14:13:58 +00:00
DirectoryPath = directoryPath ?? DataLocation.CacheDirectory;
FilesFolders.ValidateDirectory(DirectoryPath);
2025-04-11 14:13:58 +00:00
FilePath = Path.Combine(DirectoryPath, $"{filename}{FileSuffix}");
}
public async ValueTask<T> TryLoadAsync(T defaultData)
{
if (Data != null) return Data;
2025-04-01 06:16:32 +00:00
if (File.Exists(FilePath))
{
if (new FileInfo(FilePath).Length == 0)
{
2025-04-13 09:11:36 +00:00
Log.Error(ClassName, $"Zero length cache file <{FilePath}>");
2025-04-01 06:16:32 +00:00
Data = defaultData;
await SaveAsync();
}
await using var stream = new FileStream(FilePath, FileMode.Open);
Data = await DeserializeAsync(stream, defaultData);
}
else
{
2025-04-13 09:11:36 +00:00
Log.Info(ClassName, "Cache file not exist, load default data");
2025-04-01 06:16:32 +00:00
Data = defaultData;
await SaveAsync();
}
2025-04-01 06:16:32 +00:00
return Data;
}
2025-02-24 07:15:02 +00:00
private static async ValueTask<T> DeserializeAsync(Stream stream, T defaultData)
{
try
{
var t = await MemoryPackSerializer.DeserializeAsync<T>(stream);
2025-04-01 06:16:32 +00:00
return t ?? defaultData;
}
2025-02-24 07:15:02 +00:00
catch (System.Exception)
{
// Log.Exception($"|BinaryStorage.Deserialize|Deserialize error for file <{FilePath}>", e);
return defaultData;
}
}
2025-04-01 06:16:32 +00:00
public void Save()
{
2025-04-11 08:48:02 +00:00
// User may delete the directory, so we need to check it
FilesFolders.ValidateDirectory(DirectoryPath);
2025-04-01 06:16:32 +00:00
var serialized = MemoryPackSerializer.Serialize(Data);
File.WriteAllBytes(FilePath, serialized);
}
2025-04-05 03:11:14 +00:00
public async ValueTask SaveAsync()
{
await SaveAsync(Data.NonNull());
}
// ImageCache need to convert data into concurrent dictionary for usage,
// so we would better to clear the data
2025-04-01 06:16:32 +00:00
public void ClearData()
{
Data = default;
}
2025-04-05 03:11:14 +00:00
// ImageCache storages data in its class,
// so we need to pass it to SaveAsync
public async ValueTask SaveAsync(T data)
{
2025-04-11 08:48:02 +00:00
// User may delete the directory, so we need to check it
FilesFolders.ValidateDirectory(DirectoryPath);
await using var stream = new FileStream(FilePath, FileMode.Create);
await MemoryPackSerializer.SerializeAsync(stream, data);
}
}
}