From 85d7eb917f1cd7530e28fb0565564c79ddb910ed Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 16 Sep 2013 22:44:06 -0400 Subject: made dtoservice synchronous --- MediaBrowser.Controller/Drawing/ImageManager.cs | 84 +++++++++++++------------ 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'MediaBrowser.Controller/Drawing/ImageManager.cs') diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index 51ae6b7ca..05f45a457 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -68,11 +68,6 @@ namespace MediaBrowser.Controller.Drawing private readonly IItemRepository _itemRepo; - /// - /// The _locks - /// - private readonly ConcurrentDictionary _locks = new ConcurrentDictionary(); - /// /// Initializes a new instance of the class. /// @@ -146,7 +141,7 @@ namespace MediaBrowser.Controller.Drawing } } - var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false); + var originalImageSize = GetImageSize(originalImagePath, dateModified); // Determine the output size based on incoming parameters var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight); @@ -304,7 +299,7 @@ namespace MediaBrowser.Controller.Drawing /// The date modified. /// Task{ImageSize}. /// imagePath - public async Task GetImageSize(string imagePath, DateTime dateModified) + public ImageSize GetImageSize(string imagePath, DateTime dateModified) { if (string.IsNullOrEmpty(imagePath)) { @@ -317,7 +312,7 @@ namespace MediaBrowser.Controller.Drawing if (!_cachedImagedSizes.TryGetValue(name, out size)) { - size = await GetImageSize(name, imagePath).ConfigureAwait(false); + size = GetImageSize(name, imagePath); _cachedImagedSizes.AddOrUpdate(name, size, (keyName, oldValue) => size); } @@ -333,7 +328,7 @@ namespace MediaBrowser.Controller.Drawing /// Name of the key. /// The image path. /// ImageSize. - private async Task GetImageSize(string keyName, string imagePath) + private ImageSize GetImageSize(string keyName, string imagePath) { // Now check the file system cache var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt"); @@ -349,34 +344,29 @@ namespace MediaBrowser.Controller.Drawing // Cache file doesn't exist or is currently being written to } - var semaphore = GetLock(fullCachePath); - - await semaphore.WaitAsync().ConfigureAwait(false); - - try - { - var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); + var syncLock = GetObjectLock(fullCachePath); - return new ImageSize { Width = result[0], Height = result[1] }; - } - catch (FileNotFoundException) + lock (syncLock) { - // Cache file doesn't exist no biggie - } - catch (DirectoryNotFoundException) - { - // Cache file doesn't exist no biggie - } - catch - { - semaphore.Release(); + try + { + var result = File.ReadAllText(fullCachePath) + .Split('|') + .Select(i => double.Parse(i, UsCulture)) + .ToArray(); - throw; - } + return new ImageSize { Width = result[0], Height = result[1] }; + } + catch (FileNotFoundException) + { + // Cache file doesn't exist no biggie + } + catch (DirectoryNotFoundException) + { + // Cache file doesn't exist no biggie + } - try - { - var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false); + var size = ImageHeader.GetDimensions(imagePath, _logger); var parentPath = Path.GetDirectoryName(fullCachePath); @@ -390,10 +380,6 @@ namespace MediaBrowser.Controller.Drawing return new ImageSize { Width = size.Width, Height = size.Height }; } - finally - { - semaphore.Release(); - } } /// @@ -600,7 +586,7 @@ namespace MediaBrowser.Controller.Drawing return GetEnhancedImage(originalImagePath, dateModified, item, imageType, imageIndex, supportedImageEnhancers); } - + /// /// Runs an image through the image enhancers, caches the result, and returns the cached path /// @@ -786,6 +772,11 @@ namespace MediaBrowser.Controller.Drawing return result; } + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _semaphoreLocks = new ConcurrentDictionary(); + /// /// Gets the lock. /// @@ -793,7 +784,22 @@ namespace MediaBrowser.Controller.Drawing /// System.Object. private SemaphoreSlim GetLock(string filename) { - return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _locks = new ConcurrentDictionary(); + + /// + /// Gets the lock. + /// + /// The filename. + /// System.Object. + private object GetObjectLock(string filename) + { + return _locks.GetOrAdd(filename, key => new object()); } } } -- cgit v1.2.3