diff options
| author | BaronGreenback <jimcartlidge@yahoo.co.uk> | 2020-11-19 09:04:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-19 09:04:49 +0000 |
| commit | ef05485243b1d0043b442a4f5ab4cc9be99f16bb (patch) | |
| tree | 39fbf843ae9ebb0c15902d18c3617aece339df50 /MediaBrowser.Controller/Providers/DirectoryService.cs | |
| parent | 3ffdcfdb802079ee9ad3e76527b4519f3fe4458a (diff) | |
| parent | 49ebbcf87edb805862c032e48796d2b8020e5aa7 (diff) | |
Merge branch 'master' into PluginConfigSave
Diffstat (limited to 'MediaBrowser.Controller/Providers/DirectoryService.cs')
| -rw-r--r-- | MediaBrowser.Controller/Providers/DirectoryService.cs | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index f77455485..16fd1d42b 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -1,6 +1,7 @@ #pragma warning disable CS1591 using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using MediaBrowser.Model.IO; @@ -11,11 +12,11 @@ namespace MediaBrowser.Controller.Providers { private readonly IFileSystem _fileSystem; - private readonly Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new ConcurrentDictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase); - private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new ConcurrentDictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase); - private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new ConcurrentDictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); public DirectoryService(IFileSystem fileSystem) { @@ -24,14 +25,7 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata[] GetFileSystemEntries(string path) { - if (!_cache.TryGetValue(path, out FileSystemMetadata[] entries)) - { - entries = _fileSystem.GetFileSystemEntries(path).ToArray(); - - _cache[path] = entries; - } - - return entries; + return _cache.GetOrAdd(path, p => _fileSystem.GetFileSystemEntries(p).ToArray()); } public List<FileSystemMetadata> GetFiles(string path) @@ -51,21 +45,19 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata GetFile(string path) { - if (!_fileCache.TryGetValue(path, out FileSystemMetadata file)) + var result = _fileCache.GetOrAdd(path, p => { - file = _fileSystem.GetFileInfo(path); + var file = _fileSystem.GetFileInfo(p); + return file != null && file.Exists ? file : null; + }); - if (file != null && file.Exists) - { - _fileCache[path] = file; - } - else - { - return null; - } + if (result == null) + { + // lets not store null results in the cache + _fileCache.TryRemove(path, out _); } - return file; + return result; } public IReadOnlyList<string> GetFilePaths(string path) @@ -73,14 +65,12 @@ namespace MediaBrowser.Controller.Providers public IReadOnlyList<string> GetFilePaths(string path, bool clearCache) { - if (clearCache || !_filePathCache.TryGetValue(path, out List<string> result)) + if (clearCache) { - result = _fileSystem.GetFilePaths(path).ToList(); - - _filePathCache[path] = result; + _filePathCache.TryRemove(path, out _); } - return result; + return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(p).ToList()); } } } |
