diff options
| author | Shadowghost <Ghost_of_Stone@web.de> | 2026-09-04 19:28:59 +0200 |
|---|---|---|
| committer | Shadowghost <Ghost_of_Stone@web.de> | 2026-09-04 19:28:59 +0200 |
| commit | 46dd7d8e99ff7167d2d175878896294ed823927f (patch) | |
| tree | b7073a3dbbe5c4a367b44d58537086b38c1e9127 /MediaBrowser.Controller | |
| parent | 0d9c9c9eccf2f547f824ad47f2038c378930fdd2 (diff) | |
Invalidate the singleton DirectoryService cache on filesystem changes
Diffstat (limited to 'MediaBrowser.Controller')
| -rw-r--r-- | MediaBrowser.Controller/MediaBrowser.Controller.csproj | 1 | ||||
| -rw-r--r-- | MediaBrowser.Controller/Providers/DirectoryService.cs | 52 |
2 files changed, 17 insertions, 36 deletions
diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 06188ad511..73cdf18e91 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -18,7 +18,6 @@ </PropertyGroup> <ItemGroup> - <PackageReference Include="BitFaster.Caching" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> </ItemGroup> diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index 613433391b..684e247f86 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -1,31 +1,33 @@ #pragma warning disable CS1591 using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.CompilerServices; -using BitFaster.Caching.Lru; using MediaBrowser.Model.IO; namespace MediaBrowser.Controller.Providers { public class DirectoryService : IDirectoryService { - private static readonly ConditionalWeakTable<IFileSystem, DirectoryCache> _caches = []; + // TODO make static and switch to FastConcurrentLru. + private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(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; - private readonly DirectoryCache _cache; public DirectoryService(IFileSystem fileSystem) { _fileSystem = fileSystem; - _cache = _caches.GetValue(fileSystem, static _ => new DirectoryCache()); } public FileSystemMetadata[] GetFileSystemEntries(string path) { - return _cache.Entries.GetOrAdd( + return _cache.GetOrAdd( path, static (p, fileSystem) => { @@ -87,15 +89,13 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata? GetFileSystemEntry(string path) { - if (!_cache.Files.TryGet(path, out var result)) + if (!_fileCache.TryGetValue(path, out var result)) { var file = _fileSystem.GetFileSystemInfo(path); - - // Only cache hits: a missing file can turn up later. if (file?.Exists ?? false) { result = file; - _cache.Files.AddOrUpdate(path, result); + _fileCache.TryAdd(path, result); } } @@ -109,11 +109,10 @@ namespace MediaBrowser.Controller.Providers { if (clearCache) { - // Not Invalidate(), which would also drop the parent listing for no reason here. - Forget(path); + _filePathCache.TryRemove(path, out _); } - return _cache.FilePaths.GetOrAdd( + var filePaths = _filePathCache.GetOrAdd( path, static (p, fileSystem) => { @@ -127,6 +126,8 @@ namespace MediaBrowser.Controller.Providers } }, _fileSystem); + + return filePaths; } public void Invalidate(string path) @@ -147,28 +148,9 @@ namespace MediaBrowser.Controller.Providers private void Forget(string path) { - _cache.Entries.TryRemove(path, out _); - _cache.Files.TryRemove(path, out _); - _cache.FilePaths.TryRemove(path, out _); - } - - private sealed class DirectoryCache - { - private const int DirectoryCacheSize = 2048; - private const int FileCacheSize = 8192; - - // The cache outlives the DirectoryService instances reading it, so entries need their - // own staleness bound. A long refresh can outlive it and re-read a directory partway. - private static readonly TimeSpan _entryLifetime = TimeSpan.FromMinutes(1); - - public ConcurrentTLru<string, FileSystemMetadata[]> Entries { get; } - = new(Environment.ProcessorCount, DirectoryCacheSize, StringComparer.Ordinal, _entryLifetime); - - public ConcurrentTLru<string, FileSystemMetadata> Files { get; } - = new(Environment.ProcessorCount, FileCacheSize, StringComparer.Ordinal, _entryLifetime); - - public ConcurrentTLru<string, List<string>> FilePaths { get; } - = new(Environment.ProcessorCount, DirectoryCacheSize, StringComparer.Ordinal, _entryLifetime); + _cache.TryRemove(path, out _); + _fileCache.TryRemove(path, out _); + _filePathCache.TryRemove(path, out _); } } } |
