ModelCacheLockObjectSeconds重命名为CacheModelLockObjectSeconds
This commit is contained in:
parent
66c95d4927
commit
ce937b118c
|
@ -106,7 +106,7 @@ namespace Sample.MySql
|
|||
{
|
||||
o.CacheModelLockConcurrencyLevel = 1024;
|
||||
o.CacheEntrySize = 1;
|
||||
o.ModelCacheLockObjectSeconds = 10;
|
||||
o.CacheModelLockObjectSeconds = 10;
|
||||
o.CheckShardingKeyValueGenerated = false;
|
||||
var loggerFactory1= sp.GetService<ILoggerFactory>();
|
||||
var loggerFactory2 = sp.ApplicationServiceProvider.GetService<ILoggerFactory>();
|
||||
|
|
|
@ -7,7 +7,7 @@ using ShardingCore.Exceptions;
|
|||
|
||||
namespace ShardingCore.Core.ModelCacheLockerProviders
|
||||
{
|
||||
public class DefaultModelCacheLockerProvider:IModelCacheLockerProvider
|
||||
public class DefaultModelCacheLockerProvider : IModelCacheLockerProvider
|
||||
{
|
||||
private readonly ShardingConfigOptions _shardingConfigOptions;
|
||||
private readonly List<object> _locks;
|
||||
|
@ -24,12 +24,14 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
_locks = new List<object>(shardingConfigOptions.CacheModelLockConcurrencyLevel);
|
||||
for (int i = 0; i < shardingConfigOptions.CacheModelLockConcurrencyLevel; i++)
|
||||
{
|
||||
_locks.Add(new object());
|
||||
_locks.Add(new object());
|
||||
}
|
||||
}
|
||||
|
||||
public int GetModelCacheLockObjectSeconds()
|
||||
public int GetCacheModelLockObjectSeconds()
|
||||
{
|
||||
if (_shardingConfigOptions.ModelCacheLockObjectSeconds == 3)
|
||||
return _shardingConfigOptions.CacheModelLockObjectSeconds;
|
||||
return _shardingConfigOptions.ModelCacheLockObjectSeconds;
|
||||
}
|
||||
|
||||
|
@ -48,16 +50,18 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
{
|
||||
if (modelCacheKey == null)
|
||||
{
|
||||
throw new ShardingCoreInvalidOperationException($"modelCacheKey is null cant {nameof(GetCacheLockObject)}");
|
||||
throw new ShardingCoreInvalidOperationException(
|
||||
$"modelCacheKey is null cant {nameof(GetCacheLockObject)}");
|
||||
}
|
||||
|
||||
if (_locks.Count == 1)
|
||||
{
|
||||
return _locks[0];
|
||||
}
|
||||
|
||||
var hashCode = (modelCacheKey.ToString()??"").GetHashCode();
|
||||
var index = Math.Abs(hashCode%_locks.Count);
|
||||
var hashCode = (modelCacheKey.ToString() ?? "").GetHashCode();
|
||||
var index = Math.Abs(hashCode % _locks.Count);
|
||||
return _locks[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
{
|
||||
public interface IModelCacheLockerProvider
|
||||
{
|
||||
int GetModelCacheLockObjectSeconds();
|
||||
int GetCacheModelLockObjectSeconds();
|
||||
#if !EFCORE2
|
||||
CacheItemPriority GetCacheItemPriority();
|
||||
int GetCacheEntrySize();
|
||||
|
|
|
@ -16,6 +16,8 @@ namespace ShardingCore.Core.ShardingConfigurations
|
|||
/// <summary>
|
||||
/// 模型缓存锁等待时间
|
||||
/// </summary>
|
||||
public int CacheModelLockObjectSeconds { get; set; } = 3;
|
||||
[Obsolete("plz use CacheModelLockObjectSeconds")]
|
||||
public int ModelCacheLockObjectSeconds { get; set; } = 3;
|
||||
#if !EFCORE2
|
||||
/// <summary>
|
||||
|
|
|
@ -63,7 +63,7 @@ namespace ShardingCore.EFCores
|
|||
if (!_models.TryGetValue(cacheKey, out var model))
|
||||
{
|
||||
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
|
||||
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
|
||||
var waitSeconds = modelCacheLockerProvider.GetCacheModelLockObjectSeconds();
|
||||
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
|
||||
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
|
||||
if (!acquire)
|
||||
|
|
|
@ -74,7 +74,7 @@ namespace ShardingCore.EFCores
|
|||
var modelCacheLockerProvider = _shardingRuntimeContext.GetModelCacheLockerProvider();
|
||||
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
|
||||
var size = modelCacheLockerProvider.GetCacheEntrySize();
|
||||
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
|
||||
var waitSeconds = modelCacheLockerProvider.GetCacheModelLockObjectSeconds();
|
||||
var cacheLockObject = modelCacheLockerProvider.GetCacheLockObject(cacheKey);
|
||||
var acquire = Monitor.TryEnter(cacheLockObject, TimeSpan.FromSeconds(waitSeconds));
|
||||
if (!acquire)
|
||||
|
|
|
@ -91,7 +91,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
|
||||
var size = modelCacheLockerProvider.GetCacheEntrySize();
|
||||
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
|
||||
var waitSeconds = modelCacheLockerProvider.GetCacheModelLockObjectSeconds();
|
||||
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));
|
||||
|
|
|
@ -106,7 +106,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
|
||||
var size = modelCacheLockerProvider.GetCacheEntrySize();
|
||||
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
|
||||
var waitSeconds = modelCacheLockerProvider.GetCacheModelLockObjectSeconds();
|
||||
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));
|
||||
|
|
|
@ -75,7 +75,7 @@ namespace ShardingCore.EFCores
|
|||
|
||||
var priority = setPriority ?? modelCacheLockerProvider.GetCacheItemPriority();
|
||||
var size = modelCacheLockerProvider.GetCacheEntrySize();
|
||||
var waitSeconds = modelCacheLockerProvider.GetModelCacheLockObjectSeconds();
|
||||
var waitSeconds = modelCacheLockerProvider.GetCacheModelLockObjectSeconds();
|
||||
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));
|
||||
|
|
Loading…
Reference in New Issue