1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-22 07:42:28 +00:00

Cache root folders for artist paths

(cherry picked from commit 63fdf8ca8ff9b22ce4cf8764cc05aad5d1d0ae62)
This commit is contained in:
Mark McDowall 2024-07-28 16:58:16 -07:00 committed by Bogdan
parent 07b3ebb1aa
commit 54607eb2e0

View file

@ -5,6 +5,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
@ -33,15 +34,20 @@ public class RootFolderService : IRootFolderService
private readonly IManageCommandQueue _commandQueueManager; private readonly IManageCommandQueue _commandQueueManager;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<string> _cache;
public RootFolderService(IRootFolderRepository rootFolderRepository, public RootFolderService(IRootFolderRepository rootFolderRepository,
IDiskProvider diskProvider, IDiskProvider diskProvider,
IManageCommandQueue commandQueueManager, IManageCommandQueue commandQueueManager,
ICacheManager cacheManager,
Logger logger) Logger logger)
{ {
_rootFolderRepository = rootFolderRepository; _rootFolderRepository = rootFolderRepository;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_commandQueueManager = commandQueueManager; _commandQueueManager = commandQueueManager;
_logger = logger; _logger = logger;
_cache = cacheManager.GetCache<string>(GetType());
} }
public List<RootFolder> All() public List<RootFolder> All()
@ -89,7 +95,7 @@ private void VerifyRootFolder(RootFolder rootFolder)
if (!_diskProvider.FolderWritable(rootFolder.Path)) 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<string> { rootFolder.Path }, FilterFilesType.None, true, null)); _commandQueueManager.Push(new RescanFoldersCommand(new List<string> { rootFolder.Path }, FilterFilesType.None, true, null));
GetDetails(rootFolder, true); GetDetails(rootFolder, true);
_cache.Clear();
return rootFolder; return rootFolder;
} }
@ -118,6 +125,7 @@ public RootFolder Update(RootFolder rootFolder)
_rootFolderRepository.Update(rootFolder); _rootFolderRepository.Update(rootFolder);
GetDetails(rootFolder, true); GetDetails(rootFolder, true);
_cache.Clear();
return rootFolder; return rootFolder;
} }
@ -125,6 +133,7 @@ public RootFolder Update(RootFolder rootFolder)
public void Remove(int id) public void Remove(int id)
{ {
_rootFolderRepository.Delete(id); _rootFolderRepository.Delete(id);
_cache.Clear();
} }
public RootFolder Get(int id, bool timeout) public RootFolder Get(int id, bool timeout)
@ -148,16 +157,7 @@ public RootFolder GetBestRootFolder(string path)
public string GetBestRootFolderPath(string path) public string GetBestRootFolderPath(string path)
{ {
var possibleRootFolder = GetBestRootFolder(path); return _cache.Get(path, () => GetBestRootFolderPathInternal(path), TimeSpan.FromDays(1));
if (possibleRootFolder == null)
{
var osPath = new OsPath(path);
return osPath.Directory.ToString().TrimEnd(osPath.IsUnixPath ? '/' : '\\');
}
return possibleRootFolder?.Path;
} }
private void GetDetails(RootFolder rootFolder, bool timeout) private void GetDetails(RootFolder rootFolder, bool timeout)
@ -172,5 +172,19 @@ private void GetDetails(RootFolder rootFolder, bool timeout)
} }
}).Wait(timeout ? 5000 : -1); }).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;
}
} }
} }