From 54607eb2e07bc7b66db32354313d36e7b47643be Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 28 Jul 2024 16:58:16 -0700 Subject: [PATCH] Cache root folders for artist paths (cherry picked from commit 63fdf8ca8ff9b22ce4cf8764cc05aad5d1d0ae62) --- .../RootFolders/RootFolderService.cs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Core/RootFolders/RootFolderService.cs b/src/NzbDrone.Core/RootFolders/RootFolderService.cs index 1b5c57f31..9282510c2 100644 --- a/src/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/src/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using NLog; using NzbDrone.Common; +using NzbDrone.Common.Cache; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Core.MediaFiles; @@ -33,15 +34,20 @@ public class RootFolderService : IRootFolderService private readonly IManageCommandQueue _commandQueueManager; private readonly Logger _logger; + private readonly ICached _cache; + public RootFolderService(IRootFolderRepository rootFolderRepository, IDiskProvider diskProvider, IManageCommandQueue commandQueueManager, + ICacheManager cacheManager, Logger logger) { _rootFolderRepository = rootFolderRepository; _diskProvider = diskProvider; _commandQueueManager = commandQueueManager; _logger = logger; + + _cache = cacheManager.GetCache(GetType()); } public List All() @@ -89,7 +95,7 @@ private void VerifyRootFolder(RootFolder rootFolder) if (!_diskProvider.FolderWritable(rootFolder.Path)) { - throw new UnauthorizedAccessException(string.Format("Root folder path '{0}' is not writable by user '{1}'", rootFolder.Path, Environment.UserName)); + throw new UnauthorizedAccessException($"Root folder path '{rootFolder.Path}' is not writable by user '{Environment.UserName}'"); } } @@ -107,6 +113,7 @@ public RootFolder Add(RootFolder rootFolder) _commandQueueManager.Push(new RescanFoldersCommand(new List { rootFolder.Path }, FilterFilesType.None, true, null)); GetDetails(rootFolder, true); + _cache.Clear(); return rootFolder; } @@ -118,6 +125,7 @@ public RootFolder Update(RootFolder rootFolder) _rootFolderRepository.Update(rootFolder); GetDetails(rootFolder, true); + _cache.Clear(); return rootFolder; } @@ -125,6 +133,7 @@ public RootFolder Update(RootFolder rootFolder) public void Remove(int id) { _rootFolderRepository.Delete(id); + _cache.Clear(); } public RootFolder Get(int id, bool timeout) @@ -148,16 +157,7 @@ public RootFolder GetBestRootFolder(string path) public string GetBestRootFolderPath(string path) { - var possibleRootFolder = GetBestRootFolder(path); - - if (possibleRootFolder == null) - { - var osPath = new OsPath(path); - - return osPath.Directory.ToString().TrimEnd(osPath.IsUnixPath ? '/' : '\\'); - } - - return possibleRootFolder?.Path; + return _cache.Get(path, () => GetBestRootFolderPathInternal(path), TimeSpan.FromDays(1)); } private void GetDetails(RootFolder rootFolder, bool timeout) @@ -172,5 +172,19 @@ private void GetDetails(RootFolder rootFolder, bool timeout) } }).Wait(timeout ? 5000 : -1); } + + private string GetBestRootFolderPathInternal(string path) + { + var possibleRootFolder = GetBestRootFolder(path); + + if (possibleRootFolder == null) + { + var osPath = new OsPath(path); + + return osPath.Directory.ToString().TrimEnd(osPath.IsUnixPath ? '/' : '\\'); + } + + return possibleRootFolder?.Path; + } } }