@ -23,7 +23,8 @@ namespace MediaBrowser.Server.Implementations.Library
{
public event EventHandler < UserDataSaveEventArgs > UserDataSaved ;
private readonly Dictionary < string , UserItemData > _userData = new Dictionary < string , UserItemData > ( StringComparer . OrdinalIgnoreCase ) ;
private readonly ConcurrentDictionary < string , UserItemData > _userData =
new ConcurrentDictionary < string , UserItemData > ( StringComparer . OrdinalIgnoreCase ) ;
private readonly ILogger _logger ;
private readonly IServerConfigurationManager _config ;
@ -64,13 +65,6 @@ namespace MediaBrowser.Server.Implementations.Library
try
{
await Repository . SaveUserData ( userId , key , userData , cancellationToken ) . ConfigureAwait ( false ) ;
var newValue = userData ;
lock ( _userData )
{
_userData [ GetCacheKey ( userId , key ) ] = newValue ;
}
}
catch ( Exception ex )
{
@ -80,6 +74,9 @@ namespace MediaBrowser.Server.Implementations.Library
}
}
var cacheKey = GetCacheKey ( userId , item . Id ) ;
_userData . AddOrUpdate ( cacheKey , userData , ( k , v ) = > userData ) ;
EventHelper . FireEventIfNotNull ( UserDataSaved , this , new UserDataSaveEventArgs
{
Keys = keys ,
@ -140,7 +137,7 @@ namespace MediaBrowser.Server.Implementations.Library
return Repository . GetAllUserData ( userId ) ;
}
public UserItemData GetUserData ( Guid userId , List< string > keys )
public UserItemData GetUserData ( Guid userId , Guid itemId , List< string > keys )
{
if ( userId = = Guid . Empty )
{
@ -150,91 +147,44 @@ namespace MediaBrowser.Server.Implementations.Library
{
throw new ArgumentNullException ( "keys" ) ;
}
lock ( _userData )
{
foreach ( var key in keys )
if ( keys . Count = = 0 )
{
var cacheKey = GetCacheKey ( userId , key ) ;
UserItemData value ;
if ( _userData . TryGetValue ( cacheKey , out value ) )
{
return value ;
throw new ArgumentException ( "UserData keys cannot be empty." ) ;
}
value = Repository . GetUserData ( userId , key ) ;
var cacheKey = GetCacheKey ( userId , itemId ) ;
if ( value ! = null )
{
_userData [ cacheKey ] = value ;
return value ;
}
}
return _userData . GetOrAdd ( cacheKey , k = > GetUserDataInternal ( userId , keys ) ) ;
}
if ( keys . Count > 0 )
{
return new UserItemData
private UserItemData GetUserDataInternal ( Guid userId , List < string > keys )
{
UserId = userId ,
Key = keys [ 0 ]
} ;
}
var userData = Repository . GetUserData ( userId , keys ) ;
return null ;
}
/// <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 )
{
if ( userId = = Guid . Empty )
if ( userData ! = null )
{
throw new ArgumentNullException ( "userId" ) ;
}
if ( string . IsNullOrEmpty ( key ) )
{
throw new ArgumentNullException ( "key" ) ;
return userData ;
}
lock ( _userData )
{
var cacheKey = GetCacheKey ( userId , key ) ;
UserItemData value ;
if ( _userData . TryGetValue ( cacheKey , out value ) )
{
return value ;
}
value = Repository . GetUserData ( userId , key ) ;
if ( value = = null )
if ( keys . Count > 0 )
{
value = new UserItemData
return new UserItemData
{
UserId = userId ,
Key = key
Key = keys [ 0 ]
} ;
}
_userData [ cacheKey ] = value ;
return value ;
}
return null ;
}
/// <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 )
private string GetCacheKey ( Guid userId , Guid itemId )
{
return userId + key ;
return userId . ToString ( "N" ) + itemId . ToString ( "N" ) ;
}
public UserItemData GetUserData ( IHasUserData user , IHasUserData item )
@ -249,7 +199,7 @@ namespace MediaBrowser.Server.Implementations.Library
public UserItemData GetUserData ( Guid userId , IHasUserData item )
{
return GetUserData ( userId , item . GetUserDataKeys( ) ) ;
return GetUserData ( userId , item . Id, item . GetUserDataKeys( ) ) ;
}
public UserItemDataDto GetUserDataDto ( IHasUserData item , User user )