refactor: add versioning to cf cache

Add a version number field to the custom format cache object. This is an
attempt at future-proofing the cache in case I ever need to update the
schema in a non-backward compatible way.
recyclarr
Robert Dailey 4 years ago
parent 77917aa08d
commit 47fa66bb79

@ -118,5 +118,36 @@ namespace Trash.Tests.Radarr.CustomFormat
ctx.Persister.Update(new List<ProcessedCustomFormatData>()); ctx.Persister.Update(new List<ProcessedCustomFormatData>());
ctx.Persister.CfCache.Should().NotBeNull(); ctx.Persister.CfCache.Should().NotBeNull();
} }
[Test]
public void Accept_loaded_cache_when_versions_match()
{
var ctx = new Context();
var testCfObj = new CustomFormatCache
{
Version = CustomFormatCache.LatestVersion,
TrashIdMappings = new List<TrashIdMapping> {new("", "", 5)}
};
ctx.ServiceCache.Load<CustomFormatCache>().Returns(testCfObj);
ctx.Persister.Load();
ctx.Persister.CfCache.Should().NotBeNull();
}
[TestCase(CustomFormatCache.LatestVersion-1)]
[TestCase(CustomFormatCache.LatestVersion+1)]
public void Set_loaded_cache_to_null_if_versions_mismatch(int versionToTest)
{
var ctx = new Context();
var testCfObj = new CustomFormatCache
{
Version = versionToTest,
TrashIdMappings = new List<TrashIdMapping> {new("", "", 5)}
};
ctx.ServiceCache.Load<CustomFormatCache>().Returns(testCfObj);
ctx.Persister.Load();
ctx.Persister.CfCache.Should().BeNull();
}
} }
} }

@ -27,6 +27,15 @@ namespace Trash.Radarr.CustomFormat
if (CfCache != null) if (CfCache != null)
{ {
Log.Debug("Loaded Cache"); Log.Debug("Loaded Cache");
// If the version is higher OR lower, we invalidate the cache. It means there's an
// incompatibility that we do not support.
if (CfCache.Version != CustomFormatCache.LatestVersion)
{
Log.Information("Cache version mismatch ({OldVersion} vs {LatestVersion}); ignoring cache data",
CfCache.Version, CustomFormatCache.LatestVersion);
CfCache = null;
}
} }
else else
{ {

@ -6,6 +6,9 @@ namespace Trash.Radarr.CustomFormat.Models.Cache
[CacheObjectName("custom-format-cache")] [CacheObjectName("custom-format-cache")]
public class CustomFormatCache public class CustomFormatCache
{ {
public const int LatestVersion = 1;
public int Version { get; init; } = LatestVersion;
public List<TrashIdMapping> TrashIdMappings { get; init; } = new(); public List<TrashIdMapping> TrashIdMappings { get; init; } = new();
} }

Loading…
Cancel
Save