升级到7.6.0.8移除IShardingModelCacheOption,将默认的缓存设置进行了建议配置

This commit is contained in:
xuejiaming 2022-11-27 00:00:09 +08:00
parent 9de612b37f
commit ff307db879
18 changed files with 119 additions and 203 deletions

View File

@ -1,10 +1,10 @@
:start
::定义版本
set EFCORE7=7.7.0.7
set EFCORE6=7.6.0.7
set EFCORE5=7.5.0.7
set EFCORE3=7.3.0.7
set EFCORE2=7.2.0.7
set EFCORE7=7.7.0.8
set EFCORE6=7.6.0.8
set EFCORE5=7.5.0.8
set EFCORE3=7.3.0.8
set EFCORE2=7.2.0.8
::删除所有bin与obj下的文件
@echo off

View File

@ -34,16 +34,22 @@ namespace Sample.MySql.Shardings
builder.ShardingProperty(o => o.Time);
}
// protected override List<TableRouteUnit> AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List<TableRouteUnit> shardingRouteUnits)
// {
// if (shardingRouteUnits.Count > 10)
// {
// _logger.LogInformation("截断前:"+string.Join(",",shardingRouteUnits.Select(o=>o.Tail)));
// //这边你要自己做顺序处理阶段
// var result= shardingRouteUnits.OrderByDescending(o=>o.Tail).Take(10).ToList();
// _logger.LogInformation("截断后:"+string.Join(",",result.Select(o=>o.Tail)));
// return result;
// }
// return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
// }
protected override List<TableRouteUnit> AfterShardingRouteUnitFilter(DataSourceRouteResult dataSourceRouteResult, List<TableRouteUnit> shardingRouteUnits)
{
if (shardingRouteUnits.Count > 10)
{
_logger.LogInformation("截断前:"+string.Join(",",shardingRouteUnits.Select(o=>o.Tail)));
//这边你要自己做顺序处理阶段
var result= shardingRouteUnits.OrderByDescending(o=>o.Tail).Take(10).ToList();
_logger.LogInformation("截断后:"+string.Join(",",result.Select(o=>o.Tail)));
return result;
}
Console.WriteLine("AfterShardingRouteUnitFilter:"+shardingRouteUnits.Count);
return base.AfterShardingRouteUnitFilter(dataSourceRouteResult, shardingRouteUnits);
}
}

View File

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
namespace ShardingCore.Core
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/4/15 11:40:58
/// Email: 326308290@qq.com
public interface IShardingModelCacheOption
{
#if !EFCORE2
CacheItemPriority GetModelCachePriority();
int GetModelCacheEntrySize();
#endif
int GetModelCacheLockObjectSeconds();
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
using ShardingCore.Core.ShardingConfigurations;
using ShardingCore.Exceptions;
@ -26,6 +27,23 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
_locks.Add(new object());
}
}
public int GetModelCacheLockObjectSeconds()
{
return _shardingConfigOptions.ModelCacheLockObjectSeconds;
}
#if !EFCORE2
public CacheItemPriority GetCacheItemPriority()
{
return _shardingConfigOptions.CacheItemPriority;
}
public int GetCacheEntrySize()
{
return _shardingConfigOptions.CacheEntrySize;
}
#endif
public object GetCacheLockObject(object modelCacheKey)
{
if (modelCacheKey == null)

View File

@ -1,7 +1,16 @@
using Microsoft.Extensions.Caching.Memory;
namespace ShardingCore.Core.ModelCacheLockerProviders
{
public interface IModelCacheLockerProvider
{
int GetModelCacheLockObjectSeconds();
#if !EFCORE2
CacheItemPriority GetCacheItemPriority();
int GetCacheEntrySize();
#endif
object GetCacheLockObject(object modelCacheKey);
}
}

View File

@ -3,6 +3,7 @@ using ShardingCore.Sharding.ReadWriteConfigurations;
using System;
using System.Collections.Generic;
using System.Data.Common;
using Microsoft.Extensions.Caching.Memory;
using ShardingCore.Core.ServiceProviders;
namespace ShardingCore.Core.ShardingConfigurations
@ -12,6 +13,20 @@ namespace ShardingCore.Core.ShardingConfigurations
/// </summary>
public class ShardingConfigOptions
{
/// <summary>
/// 模型缓存锁等待时间
/// </summary>
public int ModelCacheLockObjectSeconds { get; set; } = 3;
#if !EFCORE2
/// <summary>
/// 模型缓存的优先级
/// </summary>
public CacheItemPriority CacheItemPriority { get; set; } = CacheItemPriority.High;
/// <summary>
/// efcore缓存最多限制10240个单个缓存size设置为10那么就意味可以最多统一时间缓存1024个(缓存过期了那么还是会可以缓存进去的)
/// </summary>
public int CacheEntrySize { get; set; } = 10;
#endif
/// <summary>
/// 模型缓存锁等级
/// </summary>

View File

@ -58,16 +58,13 @@ namespace ShardingCore.EFCores
}
}
int waitSeconds = 3;
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
if (!_models.TryGetValue(cacheKey, out var model))
{
if (context is IShardingModelCacheOption shardingModelCacheOption)
{
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
}
var cacheLockObject = _shardingRuntimeContext.GetModelCacheLockerProvider().GetCacheLockObject(cacheKey);
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
if (!acquire)
{

View File

@ -49,9 +49,7 @@ namespace ShardingCore.EFCores
DbContext context,
IConventionSetBuilder conventionSetBuilder)
{
var priority = CacheItemPriority.High;
var size = 200;
var waitSeconds = 3;
CacheItemPriority? setPriority = null;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
@ -65,22 +63,19 @@ namespace ShardingCore.EFCores
}
else if (shardingTableDbContext.RouteTail is ISingleQueryRouteTail singleQueryRouteTail && singleQueryRouteTail.IsShardingTableQuery())
{
priority = CacheItemPriority.Normal;
setPriority = CacheItemPriority.Normal;
}
}
var cache = Dependencies.MemoryCache;
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
if (!cache.TryGetValue(cacheKey, out IModel model))
{
if (context is IShardingModelCacheOption shardingModelCacheOption)
{
priority = shardingModelCacheOption.GetModelCachePriority();
size = shardingModelCacheOption.GetModelCacheEntrySize();
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
}
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
var cacheLockObject = _shardingRuntimeContext.GetModelCacheLockerProvider().GetCacheLockObject(cacheKey);
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
var size = modelCacheLockerProvider.GetCacheEntrySize();
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
if (!acquire)
{

View File

@ -66,9 +66,7 @@ namespace ShardingCore.EFCores
ModelDependencies modelDependencies)
{
var priority = CacheItemPriority.High;
var size = 200;
var waitSeconds = 3;
CacheItemPriority? setPriority = null;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
@ -82,20 +80,19 @@ namespace ShardingCore.EFCores
}
else if (shardingTableDbContext.RouteTail is ISingleQueryRouteTail singleQueryRouteTail&& singleQueryRouteTail.IsShardingTableQuery())
{
priority = CacheItemPriority.Normal;
setPriority = CacheItemPriority.Normal;
}
}
var cache = Dependencies.MemoryCache;
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
if (!cache.TryGetValue(cacheKey, out IModel model))
{
if (context is IShardingModelCacheOption shardingModelCacheOption)
{
priority = shardingModelCacheOption.GetModelCachePriority();
size = shardingModelCacheOption.GetModelCacheEntrySize();
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
}
var cacheLockObject = _shardingRuntimeContext.GetModelCacheLockerProvider().GetCacheLockObject(cacheKey);
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
var size = modelCacheLockerProvider.GetCacheEntrySize();
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
if (!acquire)

View File

@ -10,6 +10,7 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -78,9 +79,7 @@ namespace ShardingCore.EFCores
ModelCreationDependencies modelCreationDependencies,
bool designTime)
{
var priority = CacheItemPriority.High;
var size = 200;
var waitSeconds = 3;
CacheItemPriority? setPriority = null;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
@ -96,21 +95,19 @@ namespace ShardingCore.EFCores
else
if (shardingTableDbContext.RouteTail is ISingleQueryRouteTail singleQueryRouteTail && singleQueryRouteTail.IsShardingTableQuery())
{
priority = CacheItemPriority.Normal;
setPriority = CacheItemPriority.Normal;
}
}
var cache = Dependencies.MemoryCache;
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context, designTime);
if (!cache.TryGetValue(cacheKey, out IModel model))
{
if (context is IShardingModelCacheOption shardingModelCacheOption)
{
priority = shardingModelCacheOption.GetModelCachePriority();
size = shardingModelCacheOption.GetModelCacheEntrySize();
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
}
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
var cacheLockObject = _shardingRuntimeContext.GetModelCacheLockerProvider().GetCacheLockObject(cacheKey);
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
var size = modelCacheLockerProvider.GetCacheEntrySize();
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
if (!acquire)
@ -138,35 +135,6 @@ namespace ShardingCore.EFCores
return model;
}
// public IModelCacheKeyFactory GetModelCacheKeyFactory()
// {
// return Dependencies.ModelCacheKeyFactory;
// }
//
// public object GetSyncObject()
// {
// return _syncObject;
// }
// public void Remove(object key)
// {
// var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(3));
// if (!acquire)
// {
// throw new ShardingCoreInvalidOperationException("cache model timeout");
// }
// try
// {
//
// var cache = Dependencies.MemoryCache;
// cache.Remove(key);
// }
// finally
// {
// Monitor.Exit(_syncObject);
// }
// }
}
}
#endif

View File

@ -1,5 +1,4 @@

#if EFCORE7
#if EFCORE7
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
@ -14,12 +13,12 @@ using ShardingCore.Core.RuntimeContexts;
namespace ShardingCore.EFCores
{
public class ShardingModelSource : ModelSource
{
private readonly IShardingRuntimeContext _shardingRuntimeContext;
public ShardingModelSource(ModelSourceDependencies dependencies,IShardingRuntimeContext shardingRuntimeContext) : base(dependencies)
public ShardingModelSource(ModelSourceDependencies dependencies, IShardingRuntimeContext shardingRuntimeContext)
: base(dependencies)
{
_shardingRuntimeContext = shardingRuntimeContext;
Check.NotNull(dependencies, nameof(dependencies));
@ -44,55 +43,60 @@ namespace ShardingCore.EFCores
ModelCreationDependencies modelCreationDependencies,
bool designTime)
{
var priority = CacheItemPriority.High;
var size = 200;
var waitSeconds = 3;
CacheItemPriority? setPriority = null;
if (context is IShardingTableDbContext shardingTableDbContext)
{
if (shardingTableDbContext.RouteTail is null)
{
throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value");
throw new ShardingCoreInvalidOperationException(
"db context model is inited before RouteTail set value");
}
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
{
var noCacheModel = this.CreateModel(context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies);
noCacheModel = modelCreationDependencies.ModelRuntimeInitializer.Initialize(noCacheModel, designTime, modelCreationDependencies.ValidationLogger);
var noCacheModel = this.CreateModel(context, modelCreationDependencies.ConventionSetBuilder,
modelCreationDependencies.ModelDependencies);
noCacheModel = modelCreationDependencies.ModelRuntimeInitializer.Initialize(noCacheModel,
designTime, modelCreationDependencies.ValidationLogger);
return noCacheModel;
}
else
if (shardingTableDbContext.RouteTail is ISingleQueryRouteTail singleQueryRouteTail && singleQueryRouteTail.IsShardingTableQuery())
else if (shardingTableDbContext.RouteTail is ISingleQueryRouteTail singleQueryRouteTail &&
singleQueryRouteTail.IsShardingTableQuery())
{
priority = CacheItemPriority.Normal;
setPriority = CacheItemPriority.Normal;
}
}
var cache = Dependencies.MemoryCache;
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context, designTime);
if (!cache.TryGetValue(cacheKey, out IModel model))
{
if (context is IShardingModelCacheOption shardingModelCacheOption)
{
priority = shardingModelCacheOption.GetModelCachePriority();
size = shardingModelCacheOption.GetModelCacheEntrySize();
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
}
var cacheLockObject = _shardingRuntimeContext.GetModelCacheLockerProvider().GetCacheLockObject(cacheKey);
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
var size = modelCacheLockerProvider.GetCacheEntrySize();
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
if (!acquire)
{
throw new ShardingCoreInvalidOperationException("cache model timeout");
}
try
{
if (!cache.TryGetValue(cacheKey, out model))
{
model = CreateModel(
context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies);
context, modelCreationDependencies.ConventionSetBuilder,
modelCreationDependencies.ModelDependencies);
model = modelCreationDependencies.ModelRuntimeInitializer.Initialize(
model, designTime, modelCreationDependencies.ValidationLogger);
model = cache.Set(cacheKey, model, new MemoryCacheEntryOptions { Size = size, Priority = priority });
model = cache.Set(cacheKey, model,
new MemoryCacheEntryOptions { Size = size, Priority = priority });
}
}
finally

View File

@ -1,17 +0,0 @@
// using System;
// using System.Collections.Generic;
// using System.Linq;
// using System.Text;
// using System.Threading.Tasks;
// using Microsoft.EntityFrameworkCore.Infrastructure;
//
// namespace ShardingCore.EFCores
// {
// public interface IShardingModelSource
// {
// IModelCacheKeyFactory GetModelCacheKeyFactory();
// object GetSyncObject();
// void Remove(object key);
//
// }
// }

View File

@ -311,58 +311,6 @@ namespace ShardingCore.Extensions
RemoveDbContextRelationModelSaveOnlyThatIsNamedType(dbContext, typeof(T));
}
/// <summary>
/// 移除模型缓存
/// </summary>
/// <param name="dbContext"></param>
// public static void RemoveModelCache(this DbContext dbContext)
// {
// #if !EFCORE2 && !EFCORE3 && !EFCORE5 && !EFCORE6 && !EFCORE7
// throw new NotImplementedException();
// #endif
// #if EFCORE6 || EFCORE7
// var shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
// var modelCacheKeyFactory = shardingModelSource.GetModelCacheKeyFactory();
// object key1 = modelCacheKeyFactory.Create(dbContext,true);
// shardingModelSource.Remove(key1);
// object key2 = modelCacheKeyFactory.Create(dbContext,false);
// shardingModelSource.Remove(key2);
// #endif
// #if EFCORE5
// var shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
// var modelCacheKeyFactory = shardingModelSource.GetModelCacheKeyFactory();
// object key1 = modelCacheKeyFactory.Create(dbContext);
// shardingModelSource.Remove(key1);
// #endif
// #if EFCORE3
//
// var shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
// var modelCacheKeyFactory = shardingModelSource.GetModelCacheKeyFactory();
// object key1 = modelCacheKeyFactory.Create(dbContext);
// shardingModelSource.Remove(key1);
// #endif
//
// #if EFCORE2
//
// var shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
// var modelCacheKeyFactory = shardingModelSource.GetModelCacheKeyFactory();
// object key1 = modelCacheKeyFactory.Create(dbContext);
// shardingModelSource.Remove(key1);
// #endif
// }
// /// <summary>
// /// 获取模型创建的锁
// /// </summary>
// /// <param name="dbContext"></param>
// /// <returns></returns>
// public static object GetModelCacheSyncObject(this DbContext dbContext)
// {
// IShardingModelSource shardingModelSource = dbContext.GetService<IModelSource>() as IShardingModelSource;
// return shardingModelSource.GetSyncObject();
// }
public static IEnumerable<object> GetPrimaryKeyValues<TEntity>(TEntity entity,IKey primaryKey) where TEntity : class
{
return primaryKey.Properties.Select(o =>entity.GetPropertyValue(o.Name));

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>7.7.0.7</Version>
<Version>7.7.0.8</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<DefineConstants>TRACE;DEBUG;EFCORE7;</DefineConstants>
<LangVersion>latest</LangVersion>

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>7.2.0.7</Version>
<Version>7.2.0.8</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<DefineConstants>TRACE;DEBUG;EFCORE2;</DefineConstants>
<LangVersion>9.0</LangVersion>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>7.3.0.7</Version>
<Version>7.3.0.8</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<DefineConstants>TRACE;DEBUG;EFCORE3;</DefineConstants>
<LangVersion>9.0</LangVersion>

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>7.5.0.7</Version>
<Version>7.5.0.8</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<DefineConstants>TRACE;DEBUG;EFCORE5;</DefineConstants>
<LangVersion>9.0</LangVersion>

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>7.6.0.7</Version>
<Version>7.6.0.8</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<DefineConstants>TRACE;DEBUG;EFCORE6;</DefineConstants>
<LangVersion>9.0</LangVersion>