diff --git a/Emby.Server.Implementations/Session/SessionManager.cs b/Emby.Server.Implementations/Session/SessionManager.cs
index 3dda5fdee7..681c252b69 100644
--- a/Emby.Server.Implementations/Session/SessionManager.cs
+++ b/Emby.Server.Implementations/Session/SessionManager.cs
@@ -237,7 +237,7 @@ namespace Emby.Server.Implementations.Session
ArgumentException.ThrowIfNullOrEmpty(deviceId);
var activityDate = DateTime.UtcNow;
- var session = await GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
+ var session = GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user);
var lastActivityDate = session.LastActivityDate;
session.LastActivityDate = activityDate;
@@ -435,7 +435,7 @@ namespace Emby.Server.Implementations.Session
/// The remote end point.
/// The user.
/// SessionInfo.
- private async Task GetSessionInfo(
+ private SessionInfo GetSessionInfo(
string appName,
string appVersion,
string deviceId,
@@ -453,7 +453,7 @@ namespace Emby.Server.Implementations.Session
if (!_activeConnections.TryGetValue(key, out var sessionInfo))
{
- sessionInfo = await CreateSession(key, appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
+ sessionInfo = CreateSession(key, appName, appVersion, deviceId, deviceName, remoteEndPoint, user);
_activeConnections[key] = sessionInfo;
}
@@ -478,7 +478,7 @@ namespace Emby.Server.Implementations.Session
return sessionInfo;
}
- private async Task CreateSession(
+ private SessionInfo CreateSession(
string key,
string appName,
string appVersion,
@@ -508,7 +508,7 @@ namespace Emby.Server.Implementations.Session
deviceName = "Network Device";
}
- var deviceOptions = await _deviceManager.GetDeviceOptions(deviceId).ConfigureAwait(false);
+ var deviceOptions = _deviceManager.GetDeviceOptions(deviceId);
if (string.IsNullOrEmpty(deviceOptions.CustomName))
{
sessionInfo.DeviceName = deviceName;
@@ -1297,7 +1297,7 @@ namespace Emby.Server.Implementations.Session
return new[] { item };
}
- private IEnumerable TranslateItemForInstantMix(Guid id, User user)
+ private List TranslateItemForInstantMix(Guid id, User user)
{
var item = _libraryManager.GetItemById(id);
@@ -1307,7 +1307,7 @@ namespace Emby.Server.Implementations.Session
return new List();
}
- return _musicManager.GetInstantMixFromItem(item, user, new DtoOptions(false) { EnableImages = false });
+ return _musicManager.GetInstantMixFromItem(item, user, new DtoOptions(false) { EnableImages = false }).ToList();
}
///
@@ -1520,12 +1520,12 @@ namespace Emby.Server.Implementations.Session
// This should be validated above, but if it isn't don't delete all tokens.
ArgumentException.ThrowIfNullOrEmpty(deviceId);
- var existing = (await _deviceManager.GetDevices(
+ var existing = _deviceManager.GetDevices(
new DeviceQuery
{
DeviceId = deviceId,
UserId = user.Id
- }).ConfigureAwait(false)).Items;
+ }).Items;
foreach (var auth in existing)
{
@@ -1553,12 +1553,12 @@ namespace Emby.Server.Implementations.Session
ArgumentException.ThrowIfNullOrEmpty(accessToken);
- var existing = (await _deviceManager.GetDevices(
+ var existing = _deviceManager.GetDevices(
new DeviceQuery
{
Limit = 1,
AccessToken = accessToken
- }).ConfigureAwait(false)).Items;
+ }).Items;
if (existing.Count > 0)
{
@@ -1597,10 +1597,10 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
- var existing = await _deviceManager.GetDevices(new DeviceQuery
+ var existing = _deviceManager.GetDevices(new DeviceQuery
{
UserId = userId
- }).ConfigureAwait(false);
+ });
foreach (var info in existing.Items)
{
@@ -1787,11 +1787,11 @@ namespace Emby.Server.Implementations.Session
///
public async Task GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint)
{
- var items = (await _deviceManager.GetDevices(new DeviceQuery
+ var items = _deviceManager.GetDevices(new DeviceQuery
{
AccessToken = token,
Limit = 1
- }).ConfigureAwait(false)).Items;
+ }).Items;
if (items.Count == 0)
{
diff --git a/Jellyfin.Api/Controllers/DevicesController.cs b/Jellyfin.Api/Controllers/DevicesController.cs
index 6d9ec343e0..2a2ab4ad16 100644
--- a/Jellyfin.Api/Controllers/DevicesController.cs
+++ b/Jellyfin.Api/Controllers/DevicesController.cs
@@ -47,10 +47,10 @@ public class DevicesController : BaseJellyfinApiController
/// An containing the list of devices.
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
- public async Task>> GetDevices([FromQuery] Guid? userId)
+ public ActionResult> GetDevices([FromQuery] Guid? userId)
{
userId = RequestHelpers.GetUserId(User, userId);
- return await _deviceManager.GetDevicesForUser(userId).ConfigureAwait(false);
+ return _deviceManager.GetDevicesForUser(userId);
}
///
@@ -63,9 +63,9 @@ public class DevicesController : BaseJellyfinApiController
[HttpGet("Info")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task> GetDeviceInfo([FromQuery, Required] string id)
+ public ActionResult GetDeviceInfo([FromQuery, Required] string id)
{
- var deviceInfo = await _deviceManager.GetDevice(id).ConfigureAwait(false);
+ var deviceInfo = _deviceManager.GetDevice(id);
if (deviceInfo is null)
{
return NotFound();
@@ -84,9 +84,9 @@ public class DevicesController : BaseJellyfinApiController
[HttpGet("Options")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task> GetDeviceOptions([FromQuery, Required] string id)
+ public ActionResult GetDeviceOptions([FromQuery, Required] string id)
{
- var deviceInfo = await _deviceManager.GetDeviceOptions(id).ConfigureAwait(false);
+ var deviceInfo = _deviceManager.GetDeviceOptions(id);
if (deviceInfo is null)
{
return NotFound();
@@ -124,13 +124,13 @@ public class DevicesController : BaseJellyfinApiController
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task DeleteDevice([FromQuery, Required] string id)
{
- var existingDevice = await _deviceManager.GetDevice(id).ConfigureAwait(false);
+ var existingDevice = _deviceManager.GetDevice(id);
if (existingDevice is null)
{
return NotFound();
}
- var sessions = await _deviceManager.GetDevices(new DeviceQuery { DeviceId = id }).ConfigureAwait(false);
+ var sessions = _deviceManager.GetDevices(new DeviceQuery { DeviceId = id });
foreach (var session in sessions.Items)
{
diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs
index d8d1b6fa83..7bb0bb1bd5 100644
--- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs
+++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs
@@ -27,6 +27,8 @@ namespace Jellyfin.Server.Implementations.Devices
private readonly IDbContextFactory _dbProvider;
private readonly IUserManager _userManager;
private readonly ConcurrentDictionary _capabilitiesMap = new();
+ private readonly IDictionary _devices;
+ private readonly IDictionary _deviceOptions;
///
/// Initializes a new instance of the class.
@@ -37,6 +39,24 @@ namespace Jellyfin.Server.Implementations.Devices
{
_dbProvider = dbProvider;
_userManager = userManager;
+ _devices = new ConcurrentDictionary();
+ _deviceOptions = new ConcurrentDictionary();
+
+ using var dbContext = _dbProvider.CreateDbContext();
+ foreach (var device in dbContext.Devices
+ .Include(d => d.User)
+ .OrderBy(d => d.Id)
+ .AsEnumerable())
+ {
+ _devices.Add(device.Id, device);
+ }
+
+ foreach (var deviceOption in dbContext.DeviceOptions
+ .OrderBy(d => d.Id)
+ .AsEnumerable())
+ {
+ _deviceOptions.Add(deviceOption.DeviceId, deviceOption);
+ }
}
///
@@ -66,6 +86,8 @@ namespace Jellyfin.Server.Implementations.Devices
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
+ _deviceOptions[deviceId] = deviceOptions;
+
DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs>(new Tuple(deviceId, deviceOptions)));
}
@@ -80,21 +102,15 @@ namespace Jellyfin.Server.Implementations.Devices
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
+ _devices.Add(device.Id, device);
+
return device;
}
///
- public async Task GetDeviceOptions(string deviceId)
+ public DeviceOptions GetDeviceOptions(string deviceId)
{
- var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
- DeviceOptions? deviceOptions;
- await using (dbContext.ConfigureAwait(false))
- {
- deviceOptions = await dbContext.DeviceOptions
- .AsNoTracking()
- .FirstOrDefaultAsync(d => d.DeviceId == deviceId)
- .ConfigureAwait(false);
- }
+ _deviceOptions.TryGetValue(deviceId, out var deviceOptions);
return deviceOptions ?? new DeviceOptions(deviceId);
}
@@ -108,57 +124,45 @@ namespace Jellyfin.Server.Implementations.Devices
}
///
- public async Task GetDevice(string id)
+ public DeviceInfo? GetDevice(string id)
{
- var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
- await using (dbContext.ConfigureAwait(false))
- {
- var device = await dbContext.Devices
- .Where(d => d.DeviceId == id)
- .OrderByDescending(d => d.DateLastActivity)
- .Include(d => d.User)
- .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })
- .FirstOrDefaultAsync()
- .ConfigureAwait(false);
+ var device = _devices.Values.OrderByDescending(d => d.DateLastActivity).FirstOrDefault(d => d.DeviceId == id);
+ _deviceOptions.TryGetValue(id, out var deviceOption);
- var deviceInfo = device is null ? null : ToDeviceInfo(device.Device, device.Options);
-
- return deviceInfo;
- }
+ var deviceInfo = device is null ? null : ToDeviceInfo(device, deviceOption);
+ return deviceInfo;
}
///
- public async Task> GetDevices(DeviceQuery query)
+ public QueryResult GetDevices(DeviceQuery query)
{
- var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
- await using (dbContext.ConfigureAwait(false))
+ var devices = _devices.Values.OrderBy(d => d.Id)
+ .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))
+ .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId)
+ .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken);
+ var canGetCountDirectly = devices.TryGetNonEnumeratedCount(out var count);
+ if (!canGetCountDirectly)
{
- var devices = dbContext.Devices
- .OrderBy(d => d.Id)
- .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))
- .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId)
- .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken);
-
- var count = await devices.CountAsync().ConfigureAwait(false);
-
- if (query.Skip.HasValue)
- {
- devices = devices.Skip(query.Skip.Value);
- }
-
- if (query.Limit.HasValue)
- {
- devices = devices.Take(query.Limit.Value);
- }
-
- return new QueryResult(query.Skip, count, await devices.ToListAsync().ConfigureAwait(false));
+ count = devices.Count();
}
+
+ if (query.Skip.HasValue)
+ {
+ devices = devices.Skip(query.Skip.Value);
+ }
+
+ if (query.Limit.HasValue)
+ {
+ devices = devices.Take(query.Limit.Value);
+ }
+
+ return new QueryResult(query.Skip, count, devices.ToList());
}
///
- public async Task> GetDeviceInfos(DeviceQuery query)
+ public QueryResult GetDeviceInfos(DeviceQuery query)
{
- var devices = await GetDevices(query).ConfigureAwait(false);
+ var devices = GetDevices(query);
return new QueryResult(
devices.StartIndex,
@@ -167,38 +171,38 @@ namespace Jellyfin.Server.Implementations.Devices
}
///
- public async Task> GetDevicesForUser(Guid? userId)
+ public QueryResult GetDevicesForUser(Guid? userId)
{
- var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
- await using (dbContext.ConfigureAwait(false))
+ var devices = _devices.Values
+ .OrderByDescending(d => d.DateLastActivity)
+ .ThenBy(d => d.DeviceId)
+ .AsEnumerable();
+
+ if (!userId.IsNullOrEmpty())
{
- var sessions = dbContext.Devices
- .Include(d => d.User)
- .OrderByDescending(d => d.DateLastActivity)
- .ThenBy(d => d.DeviceId)
- .SelectMany(d => dbContext.DeviceOptions.Where(o => o.DeviceId == d.DeviceId).DefaultIfEmpty(), (d, o) => new { Device = d, Options = o })
- .AsAsyncEnumerable();
-
- if (!userId.IsNullOrEmpty())
+ var user = _userManager.GetUserById(userId.Value);
+ if (user is null)
{
- var user = _userManager.GetUserById(userId.Value);
- if (user is null)
- {
- throw new ResourceNotFoundException();
- }
-
- sessions = sessions.Where(i => CanAccessDevice(user, i.Device.DeviceId));
+ throw new ResourceNotFoundException();
}
- var array = await sessions.Select(device => ToDeviceInfo(device.Device, device.Options)).ToArrayAsync().ConfigureAwait(false);
-
- return new QueryResult(array);
+ devices = devices.Where(i => CanAccessDevice(user, i.DeviceId));
}
+
+ var array = devices.Select(device =>
+ {
+ _deviceOptions.TryGetValue(device.DeviceId, out var option);
+ return ToDeviceInfo(device, option);
+ }).ToArray();
+
+ return new QueryResult(array);
}
///
public async Task DeleteDevice(Device device)
{
+ var id = _devices.FirstOrDefault(x => x.Value.Equals(device)).Key;
+ _devices.Remove(id);
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
@@ -207,6 +211,19 @@ namespace Jellyfin.Server.Implementations.Devices
}
}
+ ///
+ public async Task UpdateDevice(Device device)
+ {
+ var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
+ await using (dbContext.ConfigureAwait(false))
+ {
+ dbContext.Devices.Update(device);
+ await dbContext.SaveChangesAsync().ConfigureAwait(false);
+ }
+
+ _devices[device.Id] = device;
+ }
+
///
public bool CanAccessDevice(User user, string deviceId)
{
diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
index a889898406..ff29d69b43 100644
--- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
+++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs
@@ -1,6 +1,5 @@
using System;
using System.IO;
-using EFCoreSecondLevelCacheInterceptor;
using MediaBrowser.Common.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -16,28 +15,13 @@ public static class ServiceCollectionExtensions
/// Adds the interface to the service collection with second level caching enabled.
///
/// An instance of the interface.
- /// Whether second level cache disabled..
/// The updated service collection.
- public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection, bool disableSecondLevelCache)
+ public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection)
{
- if (!disableSecondLevelCache)
- {
- serviceCollection.AddEFSecondLevelCache(options =>
- options.UseMemoryCacheProvider()
- .CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(10))
- .UseCacheKeyPrefix("EF_")
- // Don't cache null values. Remove this optional setting if it's not necessary.
- .SkipCachingResults(result => result.Value is null or EFTableRows { RowsCount: 0 }));
- }
-
serviceCollection.AddPooledDbContextFactory((serviceProvider, opt) =>
{
var applicationPaths = serviceProvider.GetRequiredService();
- var dbOpt = opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}");
- if (!disableSecondLevelCache)
- {
- dbOpt.AddInterceptors(serviceProvider.GetRequiredService());
- }
+ opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}");
});
return serviceCollection;
diff --git a/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj b/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj
index 7c4155bfc6..20944ee4b2 100644
--- a/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj
+++ b/Jellyfin.Server.Implementations/Jellyfin.Server.Implementations.csproj
@@ -27,7 +27,6 @@
-
diff --git a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
index 6bda12c5b4..2ae722982a 100644
--- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
+++ b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
@@ -4,7 +4,10 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
+using Jellyfin.Data.Queries;
+using Jellyfin.Extensions;
using MediaBrowser.Controller;
+using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using Microsoft.AspNetCore.Http;
@@ -17,15 +20,18 @@ namespace Jellyfin.Server.Implementations.Security
{
private readonly IDbContextFactory _jellyfinDbProvider;
private readonly IUserManager _userManager;
+ private readonly IDeviceManager _deviceManager;
private readonly IServerApplicationHost _serverApplicationHost;
public AuthorizationContext(
IDbContextFactory jellyfinDb,
IUserManager userManager,
+ IDeviceManager deviceManager,
IServerApplicationHost serverApplicationHost)
{
_jellyfinDbProvider = jellyfinDb;
_userManager = userManager;
+ _deviceManager = deviceManager;
_serverApplicationHost = serverApplicationHost;
}
@@ -121,7 +127,11 @@ namespace Jellyfin.Server.Implementations.Security
var dbContext = await _jellyfinDbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
- var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false);
+ var device = _deviceManager.GetDevices(
+ new DeviceQuery
+ {
+ AccessToken = token
+ }).Items.FirstOrDefault();
if (device is not null)
{
@@ -178,8 +188,7 @@ namespace Jellyfin.Server.Implementations.Security
if (updateToken)
{
- dbContext.Devices.Update(device);
- await dbContext.SaveChangesAsync().ConfigureAwait(false);
+ await _deviceManager.UpdateDevice(device).ConfigureAwait(false);
}
}
else
diff --git a/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs b/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs
index e40b541a35..634aea9f09 100644
--- a/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs
+++ b/Jellyfin.Server.Implementations/Users/DeviceAccessHost.cs
@@ -60,10 +60,10 @@ public sealed class DeviceAccessHost : IHostedService
private async Task UpdateDeviceAccess(User user)
{
- var existing = (await _deviceManager.GetDevices(new DeviceQuery
+ var existing = _deviceManager.GetDevices(new DeviceQuery
{
UserId = user.Id
- }).ConfigureAwait(false)).Items;
+ }).Items;
foreach (var device in existing)
{
diff --git a/MediaBrowser.Controller/Devices/IDeviceManager.cs b/MediaBrowser.Controller/Devices/IDeviceManager.cs
index eb181dcc4c..5566421cbe 100644
--- a/MediaBrowser.Controller/Devices/IDeviceManager.cs
+++ b/MediaBrowser.Controller/Devices/IDeviceManager.cs
@@ -44,26 +44,28 @@ namespace MediaBrowser.Controller.Devices
///
/// The identifier.
/// DeviceInfo.
- Task GetDevice(string id);
+ DeviceInfo GetDevice(string id);
///
/// Gets devices based on the provided query.
///
/// The device query.
/// A representing the retrieval of the devices.
- Task> GetDevices(DeviceQuery query);
+ QueryResult GetDevices(DeviceQuery query);
- Task> GetDeviceInfos(DeviceQuery query);
+ QueryResult GetDeviceInfos(DeviceQuery query);
///
/// Gets the devices.
///
/// The user's id, or null.
/// IEnumerable<DeviceInfo>.
- Task> GetDevicesForUser(Guid? userId);
+ QueryResult GetDevicesForUser(Guid? userId);
Task DeleteDevice(Device device);
+ Task UpdateDevice(Device device);
+
///
/// Determines whether this instance [can access device] the specified user identifier.
///
@@ -74,6 +76,6 @@ namespace MediaBrowser.Controller.Devices
Task UpdateDeviceOptions(string deviceId, string deviceName);
- Task GetDeviceOptions(string deviceId);
+ DeviceOptions GetDeviceOptions(string deviceId);
}
}