From 47fa66bb79566e7bb827376eb234e6281ae8d476 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 15 May 2021 19:15:20 -0500 Subject: [PATCH] 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. --- .../Radarr/CustomFormat/CachePersisterTest.cs | 31 +++++++++++++++++++ .../Radarr/CustomFormat/CachePersister.cs | 9 ++++++ .../Models/Cache/CustomFormatCache.cs | 3 ++ 3 files changed, 43 insertions(+) diff --git a/src/Trash.Tests/Radarr/CustomFormat/CachePersisterTest.cs b/src/Trash.Tests/Radarr/CustomFormat/CachePersisterTest.cs index f57c4483..8ef50611 100644 --- a/src/Trash.Tests/Radarr/CustomFormat/CachePersisterTest.cs +++ b/src/Trash.Tests/Radarr/CustomFormat/CachePersisterTest.cs @@ -118,5 +118,36 @@ namespace Trash.Tests.Radarr.CustomFormat ctx.Persister.Update(new List()); 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 {new("", "", 5)} + }; + ctx.ServiceCache.Load().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 {new("", "", 5)} + }; + ctx.ServiceCache.Load().Returns(testCfObj); + ctx.Persister.Load(); + ctx.Persister.CfCache.Should().BeNull(); + } } } diff --git a/src/Trash/Radarr/CustomFormat/CachePersister.cs b/src/Trash/Radarr/CustomFormat/CachePersister.cs index 4852de7f..53df22e4 100644 --- a/src/Trash/Radarr/CustomFormat/CachePersister.cs +++ b/src/Trash/Radarr/CustomFormat/CachePersister.cs @@ -27,6 +27,15 @@ namespace Trash.Radarr.CustomFormat if (CfCache != null) { 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 { diff --git a/src/Trash/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs b/src/Trash/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs index 46b5aea0..9ac3ae46 100644 --- a/src/Trash/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs +++ b/src/Trash/Radarr/CustomFormat/Models/Cache/CustomFormatCache.cs @@ -6,6 +6,9 @@ namespace Trash.Radarr.CustomFormat.Models.Cache [CacheObjectName("custom-format-cache")] public class CustomFormatCache { + public const int LatestVersion = 1; + + public int Version { get; init; } = LatestVersion; public List TrashIdMappings { get; init; } = new(); }