UserDataManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 (userId == Guid.Empty)
  55. {
  56. throw new ArgumentNullException("userId");
  57. }
  58. if (string.IsNullOrEmpty(key))
  59. {
  60. throw new ArgumentNullException("key");
  61. }
  62. cancellationToken.ThrowIfCancellationRequested();
  63. try
  64. {
  65. await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
  66. var newValue = userData;
  67. // Once it succeeds, put it into the dictionary to make it available to everyone else
  68. _userData.AddOrUpdate(GetCacheKey(userId, key), newValue, delegate { return newValue; });
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error saving user data", ex);
  73. throw;
  74. }
  75. EventHelper.FireEventIfNotNull(UserDataSaved, this, new UserDataSaveEventArgs
  76. {
  77. Key = key,
  78. UserData = userData,
  79. SaveReason = reason,
  80. UserId = userId
  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. }