aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Providers/DirectoryService.cs
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2025-03-27 18:16:54 -0600
committerGitHub <noreply@github.com>2025-03-27 18:16:54 -0600
commit88ceaa39b0347c7b7626d38a48baa64923c66eeb (patch)
tree89e09f1095f11168d1bd5bf81a98f9366eed186b /MediaBrowser.Controller/Providers/DirectoryService.cs
parente9331fe9d73469bb04ae549ceaa9ea6f1ed7aa6a (diff)
Implement limiting caches (#13605)
* Implement basic expiring cache for LibraryManager * Add expiring cache to more places * Rider why * Make DirectoryService caches static * Use FastConcurrentLru * Reduce default cache size * Simplify DirectoryService caches * Make directory service cache size at least 128
Diffstat (limited to 'MediaBrowser.Controller/Providers/DirectoryService.cs')
-rw-r--r--MediaBrowser.Controller/Providers/DirectoryService.cs17
1 files changed, 8 insertions, 9 deletions
diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs
index 474f09dc5..4fca94477 100644
--- a/MediaBrowser.Controller/Providers/DirectoryService.cs
+++ b/MediaBrowser.Controller/Providers/DirectoryService.cs
@@ -1,22 +1,21 @@
#pragma warning disable CS1591
using System;
-using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
+using BitFaster.Caching.Lru;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Providers
{
public class DirectoryService : IDirectoryService
{
- private readonly IFileSystem _fileSystem;
-
- private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(StringComparer.Ordinal);
+ // These caches are primarily used for scanning so no reason to have them be large.
+ private static readonly FastConcurrentLru<string, FileSystemMetadata[]> _cache = new(Environment.ProcessorCount, Math.Max(128, Environment.ProcessorCount * 10), StringComparer.Ordinal);
+ private static readonly FastConcurrentLru<string, FileSystemMetadata> _fileCache = new(Environment.ProcessorCount, Math.Max(128, Environment.ProcessorCount * 10), StringComparer.Ordinal);
+ private static readonly FastConcurrentLru<string, List<string>> _filePathCache = new(Environment.ProcessorCount, Math.Max(128, Environment.ProcessorCount * 10), StringComparer.Ordinal);
- private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new(StringComparer.Ordinal);
-
- private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new(StringComparer.Ordinal);
+ private readonly IFileSystem _fileSystem;
public DirectoryService(IFileSystem fileSystem)
{
@@ -74,13 +73,13 @@ namespace MediaBrowser.Controller.Providers
public FileSystemMetadata? GetFileSystemEntry(string path)
{
- if (!_fileCache.TryGetValue(path, out var result))
+ if (!_fileCache.TryGet(path, out var result))
{
var file = _fileSystem.GetFileSystemInfo(path);
if (file?.Exists ?? false)
{
result = file;
- _fileCache.TryAdd(path, result);
+ _fileCache.AddOrUpdate(path, result);
}
}