diff options
| author | gnattu <gnattu@users.noreply.github.com> | 2024-08-05 10:58:22 -0400 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2024-08-05 10:58:22 -0400 |
| commit | 22d8528d904e69a8e22ba0e6d43dcb58a54bdcf5 (patch) | |
| tree | 66ee833b499e5ca4d1f1b55e316a2060081a0773 /Jellyfin.Server.Implementations/Devices | |
| parent | d5fdb9c3a728ff8204c14c171ee4bdb3c992b02f (diff) | |
Backport pull request #11901 from jellyfin/release-10.9.z
Implement Device Cache to replace EFCoreSecondLevelCacheInterceptor
Original-merge: b7bc0e1c96553675a490c0bd92a58ad9c5f0d0e1
Merged-by: joshuaboniface <joshua@boniface.me>
Backported-by: Bond_009 <bond.009@outlook.com>
Diffstat (limited to 'Jellyfin.Server.Implementations/Devices')
| -rw-r--r-- | Jellyfin.Server.Implementations/Devices/DeviceManager.cs | 157 |
1 files changed, 86 insertions, 71 deletions
diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index d8d1b6fa8..d7a46e2d5 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<JellyfinDbContext> _dbProvider; private readonly IUserManager _userManager; private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new(); + private readonly ConcurrentDictionary<int, Device> _devices; + private readonly ConcurrentDictionary<string, DeviceOptions> _deviceOptions; /// <summary> /// Initializes a new instance of the <see cref="DeviceManager"/> class. @@ -37,6 +39,23 @@ namespace Jellyfin.Server.Implementations.Devices { _dbProvider = dbProvider; _userManager = userManager; + _devices = new ConcurrentDictionary<int, Device>(); + _deviceOptions = new ConcurrentDictionary<string, DeviceOptions>(); + + using var dbContext = _dbProvider.CreateDbContext(); + foreach (var device in dbContext.Devices + .OrderBy(d => d.Id) + .AsEnumerable()) + { + _devices.TryAdd(device.Id, device); + } + + foreach (var deviceOption in dbContext.DeviceOptions + .OrderBy(d => d.Id) + .AsEnumerable()) + { + _deviceOptions.TryAdd(deviceOption.DeviceId, deviceOption); + } } /// <inheritdoc /> @@ -66,6 +85,8 @@ namespace Jellyfin.Server.Implementations.Devices await dbContext.SaveChangesAsync().ConfigureAwait(false); } + _deviceOptions[deviceId] = deviceOptions; + DeviceOptionsUpdated?.Invoke(this, new GenericEventArgs<Tuple<string, DeviceOptions>>(new Tuple<string, DeviceOptions>(deviceId, deviceOptions))); } @@ -76,25 +97,17 @@ namespace Jellyfin.Server.Implementations.Devices await using (dbContext.ConfigureAwait(false)) { dbContext.Devices.Add(device); - await dbContext.SaveChangesAsync().ConfigureAwait(false); + _devices.TryAdd(device.Id, device); } return device; } /// <inheritdoc /> - public async Task<DeviceOptions> 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 +121,43 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> - public async Task<DeviceInfo?> 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.Where(d => d.DeviceId == id).OrderByDescending(d => d.DateLastActivity).FirstOrDefault(); + _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; } /// <inheritdoc /> - public async Task<QueryResult<Device>> GetDevices(DeviceQuery query) + public QueryResult<Device> GetDevices(DeviceQuery query) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - await using (dbContext.ConfigureAwait(false)) + IEnumerable<Device> devices = _devices.Values + .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) + .OrderBy(d => d.Id) + .ToList(); + var count = devices.Count(); + + if (query.Skip.HasValue) { - 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); - } + devices = devices.Skip(query.Skip.Value); + } - return new QueryResult<Device>(query.Skip, count, await devices.ToListAsync().ConfigureAwait(false)); + if (query.Limit.HasValue) + { + devices = devices.Take(query.Limit.Value); } + + return new QueryResult<Device>(query.Skip, count, devices.ToList()); } /// <inheritdoc /> - public async Task<QueryResult<DeviceInfo>> GetDeviceInfos(DeviceQuery query) + public QueryResult<DeviceInfo> GetDeviceInfos(DeviceQuery query) { - var devices = await GetDevices(query).ConfigureAwait(false); + var devices = GetDevices(query); return new QueryResult<DeviceInfo>( devices.StartIndex, @@ -167,38 +166,36 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> - public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId) + public QueryResult<DeviceInfo> GetDevicesForUser(Guid? userId) { - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); - await using (dbContext.ConfigureAwait(false)) + IEnumerable<Device> devices = _devices.Values + .OrderByDescending(d => d.DateLastActivity) + .ThenBy(d => d.DeviceId); + + 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<DeviceInfo>(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<DeviceInfo>(array); } /// <inheritdoc /> public async Task DeleteDevice(Device device) { + _devices.TryRemove(device.Id, out _); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { @@ -208,6 +205,19 @@ namespace Jellyfin.Server.Implementations.Devices } /// <inheritdoc /> + 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; + } + + /// <inheritdoc /> public bool CanAccessDevice(User user, string deviceId) { ArgumentNullException.ThrowIfNull(user); @@ -225,6 +235,11 @@ namespace Jellyfin.Server.Implementations.Devices private DeviceInfo ToDeviceInfo(Device authInfo, DeviceOptions? options = null) { var caps = GetCapabilities(authInfo.DeviceId); + var user = _userManager.GetUserById(authInfo.UserId); + if (user is null) + { + throw new ResourceNotFoundException("User with UserId " + authInfo.UserId + " not found"); + } return new DeviceInfo { @@ -232,7 +247,7 @@ namespace Jellyfin.Server.Implementations.Devices AppVersion = authInfo.AppVersion, Id = authInfo.DeviceId, LastUserId = authInfo.UserId, - LastUserName = authInfo.User.Username, + LastUserName = user.Username, Name = authInfo.DeviceName, DateLastActivity = authInfo.DateLastActivity, IconUrl = caps.IconUrl, |
