Improve Library load and refresh performance

This commit is contained in:
MBucari 2023-03-10 18:17:24 -07:00
parent ef1edf1136
commit e1cd8b8f94
4 changed files with 45 additions and 25 deletions

View file

@ -6,10 +6,11 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileManager
{
public class LogArchiver : IDisposable
public sealed class LogArchiver : IAsyncDisposable
{
public Encoding Encoding { get; set; }
public string FileName { get; }
@ -38,21 +39,30 @@ namespace FileManager
e.Delete();
}
public void AddFile(string name, JObject contents, string comment = null)
=> AddFile(name, Encoding.GetBytes(contents.ToString(Newtonsoft.Json.Formatting.Indented)), comment);
public async Task AddFileAsync(string name, JObject contents, string comment = null)
{
ArgumentValidator.EnsureNotNull(contents, nameof(contents));
await AddFileAsync(name, Encoding.GetBytes(contents.ToString(Newtonsoft.Json.Formatting.Indented)), comment);
}
public void AddFile(string name, string contents, string comment = null)
=> AddFile(name, Encoding.GetBytes(contents), comment);
public async Task AddFileAsync(string name, string contents, string comment = null)
{
ArgumentValidator.EnsureNotNull(contents, nameof(contents));
await AddFileAsync(name, Encoding.GetBytes(contents), comment);
}
private readonly object lockOob = new();
public void AddFile(string name, ReadOnlySpan<byte> contents, string comment = null)
public Task AddFileAsync(string name, ReadOnlyMemory<byte> contents, string comment = null)
{
ArgumentValidator.EnsureNotNull(name, nameof(name));
name = ReplacementCharacters.Barebones.ReplaceFilenameChars(name);
return Task.Run(() => AddfileInternal(name, contents.Span, comment));
}
lock (lockOob)
private readonly object lockObj = new();
private void AddfileInternal(string name, ReadOnlySpan<byte> contents, string comment)
{
lock (lockObj)
{
var entry = archive.CreateEntry(name, CompressionLevel.SmallestSize);
@ -62,6 +72,6 @@ namespace FileManager
}
}
public void Dispose() => archive.Dispose();
public async ValueTask DisposeAsync() => await Task.Run(archive.Dispose);
}
}