diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 682ec9a8c8..9293d81998 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -25,8 +25,8 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The _active connections /// - private readonly List _activeConnections = - new List(); + private readonly ConcurrentDictionary _activeConnections = + new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); /// /// The _users @@ -69,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.Library /// All connections. public IEnumerable AllConnections { - get { return _activeConnections.Where(c => GetUserById(c.UserId) != null).OrderByDescending(c => c.LastActivityDate); } + get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate); } } /// @@ -313,29 +313,19 @@ namespace MediaBrowser.Server.Implementations.Library /// ClientConnectionInfo. private ClientConnectionInfo GetConnection(Guid userId, string clientType, string deviceId, string deviceName) { - lock (_activeConnections) - { - var conn = _activeConnections.FirstOrDefault(c => string.Equals(c.Client, clientType, StringComparison.OrdinalIgnoreCase) && string.Equals(deviceId, c.DeviceId)); + var key = clientType + deviceId; - if (conn == null) - { - conn = new ClientConnectionInfo - { - UserId = userId, - Client = clientType, - DeviceName = deviceName, - DeviceId = deviceId - }; - - _activeConnections.Add(conn); - } - else - { - conn.UserId = userId; - } + var connection = _activeConnections.GetOrAdd(key, keyName => new ClientConnectionInfo + { + UserId = userId, + Client = clientType, + DeviceName = deviceName, + DeviceId = deviceId + }); - return conn; - } + connection.UserId = userId; + + return connection; } /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index c6f35a978d..f471365ce2 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -33,6 +33,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } + /// + /// Gets a value indicating whether [enable delayed commands]. + /// + /// true if [enable delayed commands]; otherwise, false. + protected override bool EnableDelayedCommands + { + get + { + return false; + } + } + /// /// The _protobuf serializer /// diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs index c5320a1f62..376cf50657 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs @@ -43,6 +43,18 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The logger. protected ILogger Logger { get; private set; } + /// + /// Gets a value indicating whether [enable delayed commands]. + /// + /// true if [enable delayed commands]; otherwise, false. + protected virtual bool EnableDelayedCommands + { + get + { + return true; + } + } + /// /// Initializes a new instance of the class. /// @@ -85,8 +97,11 @@ namespace MediaBrowser.Server.Implementations.Sqlite await connection.OpenAsync().ConfigureAwait(false); - // Run once - FlushTimer = new Timer(Flush, null, TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); + if (EnableDelayedCommands) + { + // Run once + FlushTimer = new Timer(Flush, null, TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); + } } /// @@ -147,16 +162,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite { if (connection != null) { - // If we're not already flushing, do it now - if (!IsFlushing) - { - Flush(null); - } - - // Don't dispose in the middle of a flush - while (IsFlushing) + if (EnableDelayedCommands) { - Thread.Sleep(25); + FlushOnDispose(); } if (connection.IsOpen()) @@ -181,6 +189,24 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } + /// + /// Flushes the on dispose. + /// + private void FlushOnDispose() + { + // If we're not already flushing, do it now + if (!IsFlushing) + { + Flush(null); + } + + // Don't dispose in the middle of a flush + while (IsFlushing) + { + Thread.Sleep(25); + } + } + /// /// Queues the command. ///