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

171 lines
5.2 KiB
C#
Raw Permalink 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>
2025-08-13 06:19:52 +00:00
/// Storage 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 T TryLoad(T defaultData)
{
if (Data != null) return Data;
if (File.Exists(FilePath))
{
if (new FileInfo(FilePath).Length == 0)
{
Log.Error(ClassName, $"Zero length cache file <{FilePath}>");
Data = defaultData;
Save();
}
var bytes = File.ReadAllBytes(FilePath);
Data = Deserialize(bytes, defaultData);
}
else
{
Log.Info(ClassName, "Cache file not exist, load default data");
Data = defaultData;
Save();
}
return Data;
}
private T Deserialize(ReadOnlySpan<byte> bytes, T defaultData)
{
try
{
var t = MemoryPackSerializer.Deserialize<T>(bytes);
return t ?? defaultData;
}
catch (System.Exception e)
{
Log.Exception(ClassName, $"Deserialize error for file <{FilePath}>", e);
return defaultData;
}
}
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;
}
private 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;
}
catch (System.Exception e)
{
Log.Exception(ClassName, $"Deserialize error for file <{FilePath}>", e);
return defaultData;
}
}
2025-04-01 06:16:32 +00:00
public void Save()
{
Save(Data.NonNull());
}
public void Save(T data)
2025-04-01 06:16:32 +00:00
{
2025-04-11 08:48:02 +00:00
// User may delete the directory, so we need to check it
FilesFolders.ValidateDirectory(DirectoryPath);
var serialized = MemoryPackSerializer.Serialize(data);
2025-04-01 06:16:32 +00:00
File.WriteAllBytes(FilePath, serialized);
}
2025-04-05 03:11:14 +00:00
public async ValueTask SaveAsync()
{
await SaveAsync(Data.NonNull());
}
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);
}
// ImageCache need to convert data into concurrent dictionary for usage,
// so we would better to clear the data
public void ClearData()
{
Data = default;
}
}
}