From 89041982c212049469c2e292c6562a15daa7b9b5 Mon Sep 17 00:00:00 2001 From: Gary Wilber Date: Sun, 27 Sep 2020 17:02:10 -0700 Subject: [PATCH 1/4] Use ConcurrentDictionary's in DirectoryService --- .../Providers/DirectoryService.cs | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index f77455485..c4b58918c 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 _cache = new Dictionary(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - private readonly Dictionary _fileCache = new Dictionary(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _fileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - private readonly Dictionary> _filePathCache = new Dictionary>(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary> _filePathCache = new ConcurrentDictionary>(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 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(path); + 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 FileSystemMetadata removed); } - return file; + return result; } public IReadOnlyList GetFilePaths(string path) @@ -73,14 +65,12 @@ namespace MediaBrowser.Controller.Providers public IReadOnlyList GetFilePaths(string path, bool clearCache) { - if (clearCache || !_filePathCache.TryGetValue(path, out List result)) + if (clearCache) { - result = _fileSystem.GetFilePaths(path).ToList(); - - _filePathCache[path] = result; + _filePathCache.TryRemove(path, out List removed); } - return result; + return _filePathCache.GetOrAdd(path, (p) => _fileSystem.GetFilePaths(path).ToList()); } } } From 449f7e1b1e425e646ee1bcef2b1464df8b01d90e Mon Sep 17 00:00:00 2001 From: Gary Wilber Date: Sun, 27 Sep 2020 17:24:12 -0700 Subject: [PATCH 2/4] update based on suggestions --- MediaBrowser.Controller/Providers/DirectoryService.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index c4b58918c..53c6f9eb0 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -25,7 +25,7 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata[] GetFileSystemEntries(string path) { - return _cache.GetOrAdd(path, (p) => _fileSystem.GetFileSystemEntries(p).ToArray()); + return _cache.GetOrAdd(path, p => _fileSystem.GetFileSystemEntries(p).ToArray()); } public List GetFiles(string path) @@ -45,7 +45,7 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata GetFile(string path) { - var result = _fileCache.GetOrAdd(path, (p) => + var result = _fileCache.GetOrAdd(path, p => { var file = _fileSystem.GetFileInfo(path); return (file != null && file.Exists) ? file : null; @@ -54,7 +54,7 @@ namespace MediaBrowser.Controller.Providers if (result == null) { // lets not store null results in the cache - _fileCache.TryRemove(path, out FileSystemMetadata removed); + _fileCache.TryRemove(path, out _); } return result; @@ -67,10 +67,10 @@ namespace MediaBrowser.Controller.Providers { if (clearCache) { - _filePathCache.TryRemove(path, out List removed); + _filePathCache.TryRemove(path, out _); } - return _filePathCache.GetOrAdd(path, (p) => _fileSystem.GetFilePaths(path).ToList()); + return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(path).ToList()); } } } From 50c9083bc03c78ae2f87ca7bd960b5c44834c944 Mon Sep 17 00:00:00 2001 From: Gary Wilber Date: Sun, 27 Sep 2020 17:25:08 -0700 Subject: [PATCH 3/4] remove unnecessary parentheses --- MediaBrowser.Controller/Providers/DirectoryService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index 53c6f9eb0..73731783d 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -48,7 +48,7 @@ namespace MediaBrowser.Controller.Providers var result = _fileCache.GetOrAdd(path, p => { var file = _fileSystem.GetFileInfo(path); - return (file != null && file.Exists) ? file : null; + return file != null && file.Exists ? file : null; }); if (result == null) From eb04773c796bb5858ff37be6552f2ce088ee8f9a Mon Sep 17 00:00:00 2001 From: Gary Wilber Date: Sun, 27 Sep 2020 17:34:36 -0700 Subject: [PATCH 4/4] Use the get or add argument --- MediaBrowser.Controller/Providers/DirectoryService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index 73731783d..16fd1d42b 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -47,7 +47,7 @@ namespace MediaBrowser.Controller.Providers { var result = _fileCache.GetOrAdd(path, p => { - var file = _fileSystem.GetFileInfo(path); + var file = _fileSystem.GetFileInfo(p); return file != null && file.Exists ? file : null; }); @@ -70,7 +70,7 @@ namespace MediaBrowser.Controller.Providers _filePathCache.TryRemove(path, out _); } - return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(path).ToList()); + return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(p).ToList()); } } }