mirror of https://github.com/Squidex/squidex.git
committed by
GitHub
28 changed files with 241 additions and 166 deletions
@ -1,23 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.EntityFrameworkCore; |
|||
using Squidex.Infrastructure.Json; |
|||
|
|||
namespace Squidex.Infrastructure; |
|||
|
|||
public sealed class DelegatingDbNamedContextFactory<TContext>( |
|||
IJsonSerializer jsonSerializer, |
|||
Func<IJsonSerializer, string, TContext> factory) |
|||
: IDbContextNamedFactory<TContext> where TContext : DbContext |
|||
{ |
|||
public Task<TContext> CreateDbContextAsync(string name, |
|||
CancellationToken ct = default) |
|||
{ |
|||
return Task.FromResult(factory(jsonSerializer, name)); |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
// ==========================================================================
|
|||
// 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); |
|||
|
|||
var pool = new DbContextPool<TContext>(builder.Options, serviceProvider); |
|||
return pool; |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
#pragma warning disable EF1001 // Internal EF Core API usage.
|
|||
|
|||
namespace Squidex.Infrastructure; |
|||
|
|||
public sealed class PrefixExtension(string prefix) : IDbContextOptionsExtension |
|||
{ |
|||
public DbContextOptionsExtensionInfo Info |
|||
=> new PrefixExtensionInfo(this); |
|||
|
|||
public string Prefix { get; } = prefix; |
|||
|
|||
public void ApplyServices(IServiceCollection services) |
|||
{ |
|||
} |
|||
|
|||
public void Validate(IDbContextOptions options) |
|||
{ |
|||
} |
|||
|
|||
private class PrefixExtensionInfo(IDbContextOptionsExtension extension) : DbContextOptionsExtensionInfo(extension) |
|||
{ |
|||
public override int GetServiceProviderHashCode() => 0; |
|||
|
|||
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) => true; |
|||
|
|||
public override bool IsDatabaseProvider => false; |
|||
|
|||
public override string LogFragment => "PrefixExtension"; |
|||
|
|||
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue