ModelCacheLockObjectSeconds重命名为CacheModelLockObjectSeconds

This commit is contained in:
xuejiaming 2022-11-27 10:12:51 +08:00
parent 66c95d4927
commit ce937b118c
9 changed files with 21 additions and 15 deletions

View File

@ -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>();

View File

@ -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];
}
}
}
}
}

View File

@ -4,7 +4,7 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
{
public interface IModelCacheLockerProvider
{
int GetModelCacheLockObjectSeconds();
int GetCacheModelLockObjectSeconds();
#if !EFCORE2
CacheItemPriority GetCacheItemPriority();
int GetCacheEntrySize();

View File

@ -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>

View File

@ -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)

View File

@ -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)

View File

@ -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));

View File

@ -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));

View File

@ -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));