From f09306530bac14dfd825ef305ecd8058947da160 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Tue, 18 Oct 2022 10:51:22 +0800 Subject: [PATCH] [#204] --- .../EFCores/EFCore2x/ShardingModelSource.cs | 9 ++++----- .../EFCores/EFCore3x/ShardingModelSource.cs | 12 ++++++------ .../EFCores/EFCore5x/ShardingModelSource.cs | 12 ++++++------ .../EFCores/EFCore6x/ShardingModelSource.cs | 13 ++++++------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs index f9999e26..582b6b06 100644 --- a/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs @@ -57,15 +57,14 @@ namespace ShardingCore.EFCores } int waitSeconds = 3; - if (context is IShardingModelCacheOption shardingModelCacheOption) - { - waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds(); - } var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context); if (!_models.TryGetValue(cacheKey, out var model)) { - + if (context is IShardingModelCacheOption shardingModelCacheOption) + { + waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds(); + } var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds)); if (!acquire) { diff --git a/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs index 9fd57935..41c7af90 100644 --- a/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs @@ -67,16 +67,16 @@ namespace ShardingCore.EFCores priority = CacheItemPriority.Normal; } } - if (context is IShardingModelCacheOption shardingModelCacheOption) - { - priority = shardingModelCacheOption.GetModelCachePriority(); - size = shardingModelCacheOption.GetModelCacheEntrySize(); - waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds(); - } 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 acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds)); diff --git a/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs index e42a7bea..d2243a14 100644 --- a/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs @@ -83,16 +83,16 @@ namespace ShardingCore.EFCores priority = CacheItemPriority.Normal; } } - if (context is IShardingModelCacheOption shardingModelCacheOption) - { - priority = shardingModelCacheOption.GetModelCachePriority(); - size = shardingModelCacheOption.GetModelCacheEntrySize(); - waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds(); - } 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 acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds)); if (!acquire) diff --git a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs index 85173623..4a24d3a2 100644 --- a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs @@ -98,17 +98,16 @@ namespace ShardingCore.EFCores priority = CacheItemPriority.Normal; } } - - if (context is IShardingModelCacheOption shardingModelCacheOption) - { - priority = shardingModelCacheOption.GetModelCachePriority(); - size = shardingModelCacheOption.GetModelCacheEntrySize(); - waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds(); - } 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(); + } // Make sure OnModelCreating really only gets called once, since it may not be thread safe. var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds)); if (!acquire)