UserDataManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Persistence;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Server.Implementations.Library
  10. {
  11. /// <summary>
  12. /// Class UserDataManager
  13. /// </summary>
  14. public class UserDataManager : IUserDataManager
  15. {
  16. private readonly ConcurrentDictionary<string, UserItemData> _userData = new ConcurrentDictionary<string, UserItemData>();
  17. private readonly ILogger _logger;
  18. public UserDataManager(ILogManager logManager)
  19. {
  20. _logger = logManager.GetLogger(GetType().Name);
  21. }
  22. /// <summary>
  23. /// Gets or sets the repository.
  24. /// </summary>
  25. /// <value>The repository.</value>
  26. public IUserDataRepository Repository { get; set; }
  27. /// <summary>
  28. /// Saves the user data.
  29. /// </summary>
  30. /// <param name="userId">The user id.</param>
  31. /// <param name="key">The key.</param>
  32. /// <param name="userData">The user data.</param>
  33. /// <param name="cancellationToken">The cancellation token.</param>
  34. /// <returns>Task.</returns>
  35. public async Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken)
  36. {
  37. if (userData == null)
  38. {
  39. throw new ArgumentNullException("userData");
  40. }
  41. if (cancellationToken == null)
  42. {
  43. throw new ArgumentNullException("cancellationToken");
  44. }
  45. if (userId == Guid.Empty)
  46. {
  47. throw new ArgumentNullException("userId");
  48. }
  49. if (string.IsNullOrEmpty(key))
  50. {
  51. throw new ArgumentNullException("key");
  52. }
  53. cancellationToken.ThrowIfCancellationRequested();
  54. try
  55. {
  56. await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
  57. var newValue = userData;
  58. // Once it succeeds, put it into the dictionary to make it available to everyone else
  59. _userData.AddOrUpdate(GetCacheKey(userId, key), newValue, delegate { return newValue; });
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.ErrorException("Error saving user data", ex);
  64. throw;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets the user data.
  69. /// </summary>
  70. /// <param name="userId">The user id.</param>
  71. /// <param name="key">The key.</param>
  72. /// <returns>Task{UserItemData}.</returns>
  73. public UserItemData GetUserData(Guid userId, string key)
  74. {
  75. if (userId == Guid.Empty)
  76. {
  77. throw new ArgumentNullException("userId");
  78. }
  79. if (string.IsNullOrEmpty(key))
  80. {
  81. throw new ArgumentNullException("key");
  82. }
  83. return _userData.GetOrAdd(GetCacheKey(userId, key), keyName => Repository.GetUserData(userId, key));
  84. }
  85. /// <summary>
  86. /// Gets the internal key.
  87. /// </summary>
  88. /// <param name="userId">The user id.</param>
  89. /// <param name="key">The key.</param>
  90. /// <returns>System.String.</returns>
  91. private string GetCacheKey(Guid userId, string key)
  92. {
  93. return userId + key;
  94. }
  95. }
  96. }