mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.9 KiB
58 lines
1.9 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System.Collections.Concurrent;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
|
|
#pragma warning disable EF1001 // Internal EF Core API usage.
|
|
|
|
namespace Squidex.Infrastructure;
|
|
|
|
public sealed class PooledDbNamedContextFactory<TContext>(
|
|
IServiceProvider serviceProvider,
|
|
Action<DbContextOptionsBuilder<TContext>, string> configure)
|
|
: IDbContextNamedFactory<TContext>, IAsyncDisposable where TContext : DbContext
|
|
{
|
|
private readonly ConcurrentDictionary<string, DbContextPool<TContext>> pools = new ();
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
foreach (var (_, pool) in pools)
|
|
{
|
|
await pool.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<TContext> CreateDbContextAsync(string name,
|
|
CancellationToken ct = default)
|
|
{
|
|
var pool = pools.GetOrAdd(name, CreatePool);
|
|
|
|
var lease = new DbContextLease(pool, true);
|
|
await lease.Context.SetLeaseAsync(lease, ct);
|
|
|
|
return (TContext)lease.Context;
|
|
}
|
|
|
|
private DbContextPool<TContext> CreatePool(string name)
|
|
{
|
|
var builder = new DbContextOptionsBuilder<TContext>();
|
|
|
|
configure(builder, name);
|
|
|
|
builder.UsePoolSize(128);
|
|
builder.UsePrefix(name);
|
|
|
|
// All prefixes share the internal service provider, therefore the model must be cached per prefix.
|
|
builder.ReplaceService<IModelCacheKeyFactory, PrefixModelCacheKeyFactory>();
|
|
|
|
var pool = new DbContextPool<TContext>(builder.Options, serviceProvider);
|
|
return pool;
|
|
}
|
|
}
|
|
|