UserDataManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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="item">The item.</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">userData
  40. /// or
  41. /// cancellationToken
  42. /// or
  43. /// userId
  44. /// or
  45. /// key</exception>
  46. public async Task SaveUserData(Guid userId, IHasUserData item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
  47. {
  48. if (userData == null)
  49. {
  50. throw new ArgumentNullException("userData");
  51. }
  52. if (item == null)
  53. {
  54. throw new ArgumentNullException("item");
  55. }
  56. if (userId == Guid.Empty)
  57. {
  58. throw new ArgumentNullException("userId");
  59. }
  60. cancellationToken.ThrowIfCancellationRequested();
  61. var key = item.GetUserDataKey();
  62. try
  63. {
  64. await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
  65. var newValue = userData;
  66. // Once it succeeds, put it into the dictionary to make it available to everyone else
  67. _userData.AddOrUpdate(GetCacheKey(userId, key), newValue, delegate { return newValue; });
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.ErrorException("Error saving user data", ex);
  72. throw;
  73. }
  74. EventHelper.FireEventIfNotNull(UserDataSaved, this, new UserDataSaveEventArgs
  75. {
  76. Key = key,
  77. UserData = userData,
  78. SaveReason = reason,
  79. UserId = userId,
  80. Item = item
  81. }, _logger);
  82. }
  83. /// <summary>
  84. /// Gets the user data.
  85. /// </summary>
  86. /// <param name="userId">The user id.</param>
  87. /// <param name="key">The key.</param>
  88. /// <returns>Task{UserItemData}.</returns>
  89. public UserItemData GetUserData(Guid userId, string key)
  90. {
  91. if (userId == Guid.Empty)
  92. {
  93. throw new ArgumentNullException("userId");
  94. }
  95. if (string.IsNullOrEmpty(key))
  96. {
  97. throw new ArgumentNullException("key");
  98. }
  99. return _userData.GetOrAdd(GetCacheKey(userId, key), keyName => Repository.GetUserData(userId, key));
  100. }
  101. /// <summary>
  102. /// Gets the internal key.
  103. /// </summary>
  104. /// <param name="userId">The user id.</param>
  105. /// <param name="key">The key.</param>
  106. /// <returns>System.String.</returns>
  107. private string GetCacheKey(Guid userId, string key)
  108. {
  109. return userId + key;
  110. }
  111. }
  112. }