Skip to content

Commit f093065

Browse files
committed
1 parent 61f76b7 commit f093065

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ public override IModel GetModel(DbContext context, IConventionSetBuilder convent
5757
}
5858

5959
int waitSeconds = 3;
60-
if (context is IShardingModelCacheOption shardingModelCacheOption)
61-
{
62-
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
63-
}
6460

6561
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
6662
if (!_models.TryGetValue(cacheKey, out var model))
6763
{
68-
64+
if (context is IShardingModelCacheOption shardingModelCacheOption)
65+
{
66+
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
67+
}
6968
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds));
7069
if (!acquire)
7170
{

src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public override IModel GetModel(
6767
priority = CacheItemPriority.Normal;
6868
}
6969
}
70-
if (context is IShardingModelCacheOption shardingModelCacheOption)
71-
{
72-
priority = shardingModelCacheOption.GetModelCachePriority();
73-
size = shardingModelCacheOption.GetModelCacheEntrySize();
74-
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
75-
}
7670
var cache = Dependencies.MemoryCache;
7771
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
7872
if (!cache.TryGetValue(cacheKey, out IModel model))
7973
{
74+
if (context is IShardingModelCacheOption shardingModelCacheOption)
75+
{
76+
priority = shardingModelCacheOption.GetModelCachePriority();
77+
size = shardingModelCacheOption.GetModelCacheEntrySize();
78+
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
79+
}
8080
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
8181

8282
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds));

src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ public override IModel GetModel(
8383
priority = CacheItemPriority.Normal;
8484
}
8585
}
86-
if (context is IShardingModelCacheOption shardingModelCacheOption)
87-
{
88-
priority = shardingModelCacheOption.GetModelCachePriority();
89-
size = shardingModelCacheOption.GetModelCacheEntrySize();
90-
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
91-
}
9286
var cache = Dependencies.MemoryCache;
9387
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
9488
if (!cache.TryGetValue(cacheKey, out IModel model))
9589
{
90+
if (context is IShardingModelCacheOption shardingModelCacheOption)
91+
{
92+
priority = shardingModelCacheOption.GetModelCachePriority();
93+
size = shardingModelCacheOption.GetModelCacheEntrySize();
94+
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
95+
}
9696
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
9797
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds));
9898
if (!acquire)

src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,16 @@ public override IModel GetModel(
9898
priority = CacheItemPriority.Normal;
9999
}
100100
}
101-
102-
if (context is IShardingModelCacheOption shardingModelCacheOption)
103-
{
104-
priority = shardingModelCacheOption.GetModelCachePriority();
105-
size = shardingModelCacheOption.GetModelCacheEntrySize();
106-
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
107-
}
108101
var cache = Dependencies.MemoryCache;
109102
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context, designTime);
110103
if (!cache.TryGetValue(cacheKey, out IModel model))
111104
{
105+
if (context is IShardingModelCacheOption shardingModelCacheOption)
106+
{
107+
priority = shardingModelCacheOption.GetModelCachePriority();
108+
size = shardingModelCacheOption.GetModelCacheEntrySize();
109+
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
110+
}
112111
// Make sure OnModelCreating really only gets called once, since it may not be thread safe.
113112
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds));
114113
if (!acquire)

0 commit comments

Comments
 (0)