From b836fe96857de9e39d9b565b1f57a151a82e401d Mon Sep 17 00:00:00 2001 From: cvium Date: Fri, 21 Oct 2022 11:55:32 +0200 Subject: remove JellyfinDbProvider and add second level caching --- .../Extensions/ServiceCollectionExtensions.cs | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs (limited to 'Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs') diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..0bf5ca1d1 --- /dev/null +++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using EFCoreSecondLevelCacheInterceptor; +using MediaBrowser.Common.Configuration; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Server.Implementations.Extensions; + +/// +/// Extensions for the interface. +/// +public static class ServiceCollectionExtensions +{ + /// + /// Adds the interface to the service collection with second level caching enabled. + /// + /// An instance of the interface. + /// The updated service collection. + public static IServiceCollection AddJellyfinDbContext(this IServiceCollection serviceCollection) + { + serviceCollection.AddEFSecondLevelCache(options => + options.UseMemoryCacheProvider() + .CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(10)) + .DisableLogging(true) + .UseCacheKeyPrefix("EF_") + .SkipCachingCommands(commandText => + commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase)) + // Don't cache null values. Remove this optional setting if it's not necessary. + .SkipCachingResults(result => + result.Value == null || (result.Value is EFTableRows rows && rows.RowsCount == 0))); + + serviceCollection.AddPooledDbContextFactory((serviceProvider, opt) => + { + var applicationPaths = serviceProvider.GetRequiredService(); + var loggerFactory = serviceProvider.GetRequiredService(); + opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}") + .AddInterceptors(serviceProvider.GetRequiredService()) + .UseLoggerFactory(loggerFactory); + }); + + return serviceCollection; + } +} -- cgit v1.2.3 From 395efc94a771d2feb83af8d13964ca1ff6ecf476 Mon Sep 17 00:00:00 2001 From: cvium Date: Fri, 21 Oct 2022 15:10:47 +0200 Subject: remove unnecessary skipcommand since SQLite does not have NEWID --- .../Extensions/ServiceCollectionExtensions.cs | 2 -- 1 file changed, 2 deletions(-) (limited to 'Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs') diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs index 0bf5ca1d1..f98a0aede 100644 --- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs +++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs @@ -25,8 +25,6 @@ public static class ServiceCollectionExtensions .CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(10)) .DisableLogging(true) .UseCacheKeyPrefix("EF_") - .SkipCachingCommands(commandText => - commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase)) // Don't cache null values. Remove this optional setting if it's not necessary. .SkipCachingResults(result => result.Value == null || (result.Value is EFTableRows rows && rows.RowsCount == 0))); -- cgit v1.2.3