You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/Recyclarr.TrashLib/Cache/ServiceCache.cs

89 lines
2.6 KiB

using System.IO.Abstractions;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Recyclarr.Common.Extensions;
using Recyclarr.TrashLib.Config.Services;
namespace Recyclarr.TrashLib.Cache;
public class ServiceCache : IServiceCache
{
private static readonly Regex AllowedObjectNameCharacters = new(@"^[\w-]+$", RegexOptions.Compiled);
private readonly ICacheStoragePath _storagePath;
private readonly JsonSerializerSettings _jsonSettings;
public ServiceCache(ICacheStoragePath storagePath, ILogger log)
{
_storagePath = storagePath;
Log = log;
_jsonSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
}
private ILogger Log { get; }
public T? Load<T>(IServiceConfiguration config) where T : class
{
var path = PathFromAttribute<T>(config);
if (!path.Exists)
{
return null;
}
using var stream = path.OpenText();
var json = stream.ReadToEnd();
try
{
return JsonConvert.DeserializeObject<T>(json, _jsonSettings);
}
catch (JsonException e)
{
Log.Error("Failed to read cache data, will proceed without cache. Reason: {Msg}", e.Message);
}
return null;
}
public void Save<T>(T obj, IServiceConfiguration config) where T : class
{
var path = PathFromAttribute<T>(config);
path.CreateParentDirectory();
var serializer = JsonSerializer.Create(_jsonSettings);
using var stream = new JsonTextWriter(path.CreateText());
serializer.Serialize(stream, obj);
}
private static string GetCacheObjectNameAttribute<T>()
{
var attribute = typeof(T).GetCustomAttribute<CacheObjectNameAttribute>();
if (attribute == null)
{
throw new ArgumentException($"{nameof(CacheObjectNameAttribute)} is missing on type {nameof(T)}");
}
return attribute.Name;
}
private IFileInfo PathFromAttribute<T>(IServiceConfiguration config)
{
var objectName = GetCacheObjectNameAttribute<T>();
if (!AllowedObjectNameCharacters.IsMatch(objectName))
{
throw new ArgumentException($"Object name '{objectName}' has unacceptable characters");
}
return _storagePath.CalculatePath(config, objectName);
}
}