Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 21dca02

Browse files
committed
Added caching for mapping schema. It significantly reduces memory usage.
1 parent 43dfe8b commit 21dca02

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Source/LinqToDB.EntityFrameworkCore/ILinqToDBForEFTools.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public interface ILinqToDBForEFTools
4848
/// <param name="model">EF.Core data model.</param>
4949
/// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
5050
/// <returns>Mapping schema for provided EF.Core model.</returns>
51+
MappingSchema CreateMappingSchema(IModel model, IMetadataReader metadataReader);
52+
53+
/// <summary>
54+
/// Returns mapping schema using provided EF.Core data model and metadata provider.
55+
/// </summary>
56+
/// <param name="model">EF.Core data model.</param>
57+
/// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
58+
/// <returns>Mapping schema for provided EF.Core model.</returns>
5159
MappingSchema GetMappingSchema(IModel model, IMetadataReader metadataReader);
5260

5361
/// <summary>

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace LinqToDB.EntityFrameworkCore
2626
using Mapping;
2727
using Metadata;
2828
using Extensions;
29+
using Common.Internal.Cache;
2930

3031
using DataProvider;
3132
using DataProvider.DB2;
@@ -83,9 +84,17 @@ public override int GetHashCode()
8384

8485
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new ConcurrentDictionary<ProviderKey, IDataProvider>();
8586

87+
private readonly MemoryCache _schemaCache = new MemoryCache(
88+
new MemoryCacheOptions
89+
{
90+
ExpirationScanFrequency = TimeSpan.FromHours(1.0)
91+
});
92+
93+
8694
public virtual void ClearCaches()
8795
{
8896
_knownProviders.Clear();
97+
_schemaCache.Compact(1.0);
8998
}
9099

91100
/// <summary>
@@ -348,14 +357,31 @@ public virtual IMetadataReader CreateMetadataReader(IModel model,
348357
/// <param name="model">EF.Core data model.</param>
349358
/// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
350359
/// <returns>Mapping schema for provided EF.Core model.</returns>
351-
public virtual MappingSchema GetMappingSchema(IModel model, IMetadataReader metadataReader)
360+
public virtual MappingSchema CreateMappingSchema(IModel model, IMetadataReader metadataReader)
352361
{
353362
var schema = new MappingSchema();
354363
if (metadataReader != null)
355364
schema.AddMetadataReader(metadataReader);
356365
return schema;
357366
}
358367

368+
/// <summary>
369+
/// Returns mapping schema using provided EF.Core data model and metadata provider.
370+
/// </summary>
371+
/// <param name="model">EF.Core data model.</param>
372+
/// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
373+
/// <returns>Mapping schema for provided EF.Core model.</returns>
374+
public virtual MappingSchema GetMappingSchema(IModel model, IMetadataReader metadataReader)
375+
{
376+
var result = _schemaCache.GetOrCreate(Tuple.Create(model, metadataReader), e =>
377+
{
378+
e.SlidingExpiration = TimeSpan.FromHours(1);
379+
return CreateMappingSchema(model, metadataReader);
380+
});
381+
382+
return result;
383+
}
384+
359385
/// <summary>
360386
/// Returns EF.Core <see cref="IDbContextOptions"/> for specific <see cref="DbContext"/> instance.
361387
/// </summary>
@@ -430,7 +456,7 @@ public static bool IsQueryable(MethodCallExpression method, bool enumerable = tr
430456
var type = method.Method.DeclaringType;
431457

432458
return type == typeof(Queryable) || (enumerable && type == typeof(Enumerable)) || type == typeof(LinqExtensions) ||
433-
type == typeof(EntityFrameworkQueryableExtensions);
459+
type == typeof(EntityFrameworkQueryableExtensions);
434460
}
435461

436462
public static object EvaluateExpression(Expression expr)

0 commit comments

Comments
 (0)