mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-12 10:40:24 +01:00
update save methods
This commit is contained in:
@@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data
|
||||
public abstract class BaseSqliteRepository : IDisposable
|
||||
{
|
||||
protected string DbFilePath { get; set; }
|
||||
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
|
||||
protected ReaderWriterLockSlim WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
|
||||
protected ILogger Logger { get; private set; }
|
||||
|
||||
protected BaseSqliteRepository(ILogger logger)
|
||||
@@ -130,9 +130,10 @@ namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
lock (_disposeLock)
|
||||
{
|
||||
WriteLock.Wait();
|
||||
|
||||
CloseConnection();
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
CloseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -178,4 +179,51 @@ namespace Emby.Server.Implementations.Data
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReaderWriterLockSlimExtensions
|
||||
{
|
||||
private sealed class ReadLockToken : IDisposable
|
||||
{
|
||||
private ReaderWriterLockSlim _sync;
|
||||
public ReadLockToken(ReaderWriterLockSlim sync)
|
||||
{
|
||||
_sync = sync;
|
||||
sync.EnterReadLock();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_sync != null)
|
||||
{
|
||||
_sync.ExitReadLock();
|
||||
_sync = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
private sealed class WriteLockToken : IDisposable
|
||||
{
|
||||
private ReaderWriterLockSlim _sync;
|
||||
public WriteLockToken(ReaderWriterLockSlim sync)
|
||||
{
|
||||
_sync = sync;
|
||||
sync.EnterWriteLock();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_sync != null)
|
||||
{
|
||||
_sync.ExitWriteLock();
|
||||
_sync = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable Read(this ReaderWriterLockSlim obj)
|
||||
{
|
||||
return new ReadLockToken(obj);
|
||||
}
|
||||
public static IDisposable Write(this ReaderWriterLockSlim obj)
|
||||
{
|
||||
return new WriteLockToken(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ namespace Emby.Server.Implementations.Data
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
lock (WriteLock)
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
@@ -127,7 +127,7 @@ namespace Emby.Server.Implementations.Data
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
lock (WriteLock)
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
@@ -159,24 +159,27 @@ namespace Emby.Server.Implementations.Data
|
||||
|
||||
var guidId = displayPreferencesId.GetMD5();
|
||||
|
||||
using (var connection = CreateConnection(true))
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
|
||||
|
||||
var paramList = new List<object>();
|
||||
paramList.Add(guidId.ToGuidParamValue());
|
||||
paramList.Add(userId.ToGuidParamValue());
|
||||
paramList.Add(client);
|
||||
|
||||
foreach (var row in connection.Query(commandText, paramList.ToArray()))
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
return Get(row);
|
||||
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
|
||||
|
||||
var paramList = new List<object>();
|
||||
paramList.Add(guidId.ToGuidParamValue());
|
||||
paramList.Add(userId.ToGuidParamValue());
|
||||
paramList.Add(client);
|
||||
|
||||
foreach (var row in connection.Query(commandText, paramList.ToArray()))
|
||||
{
|
||||
return Get(row);
|
||||
}
|
||||
|
||||
return new DisplayPreferences
|
||||
{
|
||||
Id = guidId.ToString("N")
|
||||
};
|
||||
}
|
||||
|
||||
return new DisplayPreferences
|
||||
{
|
||||
Id = guidId.ToString("N")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,16 +193,19 @@ namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
var list = new List<DisplayPreferences>();
|
||||
|
||||
using (var connection = CreateConnection(true))
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
var commandText = "select data from userdisplaypreferences where userId=?";
|
||||
|
||||
var paramList = new List<object>();
|
||||
paramList.Add(userId.ToGuidParamValue());
|
||||
|
||||
foreach (var row in connection.Query(commandText, paramList.ToArray()))
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
list.Add(Get(row));
|
||||
var commandText = "select data from userdisplaypreferences where userId=?";
|
||||
|
||||
var paramList = new List<object>();
|
||||
paramList.Add(userId.ToGuidParamValue());
|
||||
|
||||
foreach (var row in connection.Query(commandText, paramList.ToArray()))
|
||||
{
|
||||
list.Add(Get(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace Emby.Server.Implementations.Data
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
lock (WriteLock)
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
@@ -107,18 +107,21 @@ namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
var list = new List<User>();
|
||||
|
||||
using (var connection = CreateConnection(true))
|
||||
using (WriteLock.Read())
|
||||
{
|
||||
foreach (var row in connection.Query("select guid,data from users"))
|
||||
using (var connection = CreateConnection(true))
|
||||
{
|
||||
var id = row[0].ReadGuid();
|
||||
|
||||
using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob()))
|
||||
foreach (var row in connection.Query("select guid,data from users"))
|
||||
{
|
||||
stream.Position = 0;
|
||||
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
||||
user.Id = id;
|
||||
list.Add(user);
|
||||
var id = row[0].ReadGuid();
|
||||
|
||||
using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob()))
|
||||
{
|
||||
stream.Position = 0;
|
||||
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
||||
user.Id = id;
|
||||
list.Add(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,7 +145,7 @@ namespace Emby.Server.Implementations.Data
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
lock (WriteLock)
|
||||
using (WriteLock.Write())
|
||||
{
|
||||
using (var connection = CreateConnection())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user