UserDataManager.cs 4.2 KB

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