refactor!: Remove support for old cache directories

Old cache directories previously included the instance name in the hash
generation. Code to support migration from this old path has been
removed.

Relates to #202.
json-serializing-nullable-fields-issue
Robert Dailey 8 months ago
parent b6de1bf97e
commit 1bbafa6386

@ -24,7 +24,7 @@ public partial class ServiceCache : IServiceCache
public T? Load<T>(IServiceConfiguration config) where T : class
{
var path = PathFromAttribute<T>(config, true);
var path = PathFromAttribute<T>(config);
_log.Debug("Loading cache from path: {Path}", path.FullName);
if (!path.Exists)
{
@ -70,7 +70,7 @@ public partial class ServiceCache : IServiceCache
return attribute.Name;
}
private IFileInfo PathFromAttribute<T>(IServiceConfiguration config, bool migratePath = false)
private IFileInfo PathFromAttribute<T>(IServiceConfiguration config)
{
var objectName = GetCacheObjectNameAttribute<T>();
if (!AllowedObjectNameCharactersRegex().IsMatch(objectName))
@ -78,12 +78,6 @@ public partial class ServiceCache : IServiceCache
throw new ArgumentException($"Object name '{objectName}' has unacceptable characters");
}
if (migratePath)
{
// Only do this while loading the cache. Saving should always use the direct (latest) path.
_storagePath.MigrateOldPath(config, objectName);
}
return _storagePath.CalculatePath(config, objectName);
}

@ -10,16 +10,12 @@ namespace Recyclarr.Cli.Console.Helpers;
public class CacheStoragePath : ICacheStoragePath
{
private readonly ILogger _log;
private readonly IAppPaths _paths;
private readonly IFNV1a _hashOld;
private readonly IFNV1a _hash;
public CacheStoragePath(ILogger log, IAppPaths paths)
public CacheStoragePath(IAppPaths paths)
{
_log = log;
_paths = paths;
_hashOld = FNV1aFactory.Instance.Create(FNVConfig.GetPredefinedConfig(32));
_hash = FNV1aFactory.Instance.Create(FNVConfig.GetPredefinedConfig(64));
}
@ -29,13 +25,6 @@ public class CacheStoragePath : ICacheStoragePath
return _hash.ComputeHash(Encoding.ASCII.GetBytes(url)).AsHexString();
}
private string BuildOldUniqueServiceDir(IServiceConfiguration config)
{
var url = config.BaseUrl.OriginalString;
var hash = _hashOld.ComputeHash(Encoding.ASCII.GetBytes(url)).AsHexString();
return $"{config.InstanceName}_{hash}";
}
private IFileInfo CalculatePathInternal(IServiceConfiguration config, string cacheObjectName, string serviceDir)
{
return _paths.CacheDirectory
@ -48,40 +37,4 @@ public class CacheStoragePath : ICacheStoragePath
{
return CalculatePathInternal(config, cacheObjectName, BuildUniqueServiceDir(config));
}
public IFileInfo CalculateOldPath(IServiceConfiguration config, string cacheObjectName)
{
return CalculatePathInternal(config, cacheObjectName, BuildOldUniqueServiceDir(config));
}
public void MigrateOldPath(IServiceConfiguration config, string cacheObjectName)
{
var oldServiceDir = CalculateOldPath(config, cacheObjectName).Directory;
var newServiceDir = CalculatePath(config, cacheObjectName).Directory;
if (oldServiceDir is null || newServiceDir is null)
{
_log.Debug("Cache Migration: Unable to migrate cache dir due to null value for either old or new path");
return;
}
if (!oldServiceDir.Exists)
{
_log.Debug("Cache Migration: Old path doesn't exist; skipping");
return;
}
if (newServiceDir.Exists)
{
// New dir already exists, so we can't move. Delete it.
_log.Debug("Cache Migration: Deleting {OldDir}", oldServiceDir);
oldServiceDir.Delete(true);
}
else
{
// New dir doesn't exist yet; so rename old to new.
_log.Debug("Cache Migration: Moving from {OldDir} to {NewDir}", oldServiceDir, newServiceDir);
oldServiceDir.MoveTo(newServiceDir.FullName);
}
}
}

@ -6,5 +6,4 @@ namespace Recyclarr.TrashLib.Interfaces;
public interface ICacheStoragePath
{
IFileInfo CalculatePath(IServiceConfiguration config, string cacheObjectName);
void MigrateOldPath(IServiceConfiguration config, string cacheObjectName);
}

@ -20,72 +20,4 @@ public class CacheStoragePathTest
result.FullName.Should().MatchRegex(@".*[/\\][a-f0-9]+[/\\]obj\.json$");
}
[Test, AutoMockData]
public void Migration_old_path_moved_to_new_path(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
CacheStoragePath sut)
{
var config = new SonarrConfiguration
{
BaseUrl = new Uri("http://something"),
InstanceName = "thename"
};
var oldPath = sut.CalculateOldPath(config, "obj");
var newPath = sut.CalculatePath(config, "obj");
fs.AddEmptyFile(oldPath);
sut.MigrateOldPath(config, "obj");
fs.AllFiles.Should().Contain(newPath.FullName);
fs.AllFiles.Should().NotContain(oldPath.FullName);
}
[Test, AutoMockData]
public void Migration_old_path_deleted_when_new_path_already_exists(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
CacheStoragePath sut)
{
var config = new SonarrConfiguration
{
BaseUrl = new Uri("http://something"),
InstanceName = "thename"
};
var oldPath = sut.CalculateOldPath(config, "obj");
var newPath = sut.CalculatePath(config, "obj");
fs.AddEmptyFile(oldPath);
fs.AddFile(newPath, new MockFileData("something"));
sut.MigrateOldPath(config, "obj");
fs.AllFiles.Should().NotContain(oldPath.FullName);
var file = fs.GetFile(newPath);
file.Should().NotBeNull();
file.TextContents.Should().Be("something");
}
[Test, AutoMockData]
public void Migration_nothing_moved_if_old_path_not_exist(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
CacheStoragePath sut)
{
var config = new SonarrConfiguration
{
BaseUrl = new Uri("http://something"),
InstanceName = "thename"
};
var oldPath = sut.CalculateOldPath(config, "obj");
var newPath = sut.CalculatePath(config, "obj");
sut.MigrateOldPath(config, "obj");
fs.AllFiles.Should().NotContain(oldPath.FullName);
fs.AllFiles.Should().NotContain(newPath.FullName);
}
}

Loading…
Cancel
Save