From 9f8a1b30a1ecec0af3c48bcb30e035938f93218c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 2 Oct 2013 12:58:30 -0400 Subject: moved user data cache to manager class --- .../Library/UserDataManager.cs | 70 +++++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Library/UserDataManager.cs') diff --git a/MediaBrowser.Server.Implementations/Library/UserDataManager.cs b/MediaBrowser.Server.Implementations/Library/UserDataManager.cs index f382df0f0..7d965cd3e 100644 --- a/MediaBrowser.Server.Implementations/Library/UserDataManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserDataManager.cs @@ -1,7 +1,9 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Logging; using System; +using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; @@ -12,6 +14,15 @@ namespace MediaBrowser.Server.Implementations.Library /// public class UserDataManager : IUserDataManager { + private readonly ConcurrentDictionary _userData = new ConcurrentDictionary(); + + private readonly ILogger _logger; + + public UserDataManager(ILogManager logManager) + { + _logger = logManager.GetLogger(GetType().Name); + } + /// /// Gets or sets the repository. /// @@ -26,9 +37,42 @@ namespace MediaBrowser.Server.Implementations.Library /// The user data. /// The cancellation token. /// Task. - public Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken) + public async Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken) { - return Repository.SaveUserData(userId, key, userData, cancellationToken); + if (userData == null) + { + throw new ArgumentNullException("userData"); + } + if (cancellationToken == null) + { + throw new ArgumentNullException("cancellationToken"); + } + if (userId == Guid.Empty) + { + throw new ArgumentNullException("userId"); + } + if (string.IsNullOrEmpty(key)) + { + throw new ArgumentNullException("key"); + } + + cancellationToken.ThrowIfCancellationRequested(); + + try + { + await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false); + + var newValue = userData; + + // Once it succeeds, put it into the dictionary to make it available to everyone else + _userData.AddOrUpdate(GetCacheKey(userId, key), newValue, delegate { return newValue; }); + } + catch (Exception ex) + { + _logger.ErrorException("Error saving user data", ex); + + throw; + } } /// @@ -39,7 +83,27 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{UserItemData}. public UserItemData GetUserData(Guid userId, string key) { - return Repository.GetUserData(userId, key); + if (userId == Guid.Empty) + { + throw new ArgumentNullException("userId"); + } + if (string.IsNullOrEmpty(key)) + { + throw new ArgumentNullException("key"); + } + + return _userData.GetOrAdd(GetCacheKey(userId, key), keyName => Repository.GetUserData(userId, key)); + } + + /// + /// Gets the internal key. + /// + /// The user id. + /// The key. + /// System.String. + private string GetCacheKey(Guid userId, string key) + { + return userId + key; } } } -- cgit v1.2.3