jellyfin/MediaBrowser.Server.Implementations/Library/UserDataManager.cs

110 lines
3.5 KiB
C#
Raw Normal View History

2013-10-02 18:08:58 +02:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
2013-10-02 18:58:30 +02:00
using MediaBrowser.Model.Logging;
2013-10-02 18:08:58 +02:00
using System;
2013-10-02 18:58:30 +02:00
using System.Collections.Concurrent;
2013-10-02 18:08:58 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Library
{
/// <summary>
/// Class UserDataManager
/// </summary>
public class UserDataManager : IUserDataManager
{
2013-10-02 18:58:30 +02:00
private readonly ConcurrentDictionary<string, UserItemData> _userData = new ConcurrentDictionary<string, UserItemData>();
private readonly ILogger _logger;
public UserDataManager(ILogManager logManager)
{
_logger = logManager.GetLogger(GetType().Name);
}
2013-10-02 18:08:58 +02:00
/// <summary>
/// Gets or sets the repository.
/// </summary>
/// <value>The repository.</value>
public IUserDataRepository Repository { get; set; }
/// <summary>
/// Saves the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="key">The key.</param>
/// <param name="userData">The user data.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2013-10-02 18:58:30 +02:00
public async Task SaveUserData(Guid userId, string key, UserItemData userData, CancellationToken cancellationToken)
2013-10-02 18:08:58 +02:00
{
2013-10-02 18:58:30 +02:00
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;
}
2013-10-02 18:08:58 +02:00
}
/// <summary>
/// Gets the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="key">The key.</param>
/// <returns>Task{UserItemData}.</returns>
public UserItemData GetUserData(Guid userId, string key)
{
2013-10-02 18:58:30 +02:00
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));
}
/// <summary>
/// Gets the internal key.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="key">The key.</param>
/// <returns>System.String.</returns>
private string GetCacheKey(Guid userId, string key)
{
return userId + key;
2013-10-02 18:08:58 +02:00
}
}
}