diff options
| author | cvium <clausvium@gmail.com> | 2022-10-21 11:55:32 +0200 |
|---|---|---|
| committer | cvium <clausvium@gmail.com> | 2022-10-21 11:55:32 +0200 |
| commit | b836fe96857de9e39d9b565b1f57a151a82e401d (patch) | |
| tree | c49622deedb94fa93ca95aea3199953ce0f4e629 /Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs | |
| parent | 509c6ec24ca35b2e16561808792cd581c5f9d8fc (diff) | |
remove JellyfinDbProvider and add second level caching
Diffstat (limited to 'Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs')
| -rw-r--r-- | Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs | 45 |
1 files changed, 45 insertions, 0 deletions
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; + +/// <summary> +/// Extensions for the <see cref="IServiceCollection"/> interface. +/// </summary> +public static class ServiceCollectionExtensions +{ + /// <summary> + /// Adds the <see cref="IDbContextFactory{TContext}"/> interface to the service collection with second level caching enabled. + /// </summary> + /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param> + /// <returns>The updated service collection.</returns> + 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<JellyfinDb>((serviceProvider, opt) => + { + var applicationPaths = serviceProvider.GetRequiredService<IApplicationPaths>(); + var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(); + opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}") + .AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>()) + .UseLoggerFactory(loggerFactory); + }); + + return serviceCollection; + } +} |
