优化模型处理默认的list改成array
This commit is contained in:
parent
bf676d6815
commit
1f810f9442
|
@ -0,0 +1,44 @@
|
|||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using ShardingCore.Core.ModelCacheLockerProviders;
|
||||
using ShardingCore.Helpers;
|
||||
|
||||
namespace Sample.MySql;
|
||||
|
||||
public class DicModelCacheLockerProvider:IModelCacheLockerProvider
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, object> _locks = new ConcurrentDictionary<string, object>();
|
||||
public int GetCacheModelLockObjectSeconds()
|
||||
{
|
||||
return 20;
|
||||
}
|
||||
|
||||
public CacheItemPriority GetCacheItemPriority()
|
||||
{
|
||||
return CacheItemPriority.High;
|
||||
}
|
||||
|
||||
public int GetCacheEntrySize()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public object GetCacheLockObject(object modelCacheKey)
|
||||
{
|
||||
var key = $"{modelCacheKey}";
|
||||
Console.WriteLine("-----------------------------------------------DicModelCacheLockerProvider:"+key);
|
||||
if (!_locks.TryGetValue(key, out var obj))
|
||||
{
|
||||
obj = new object();
|
||||
if (_locks.TryAdd(key, obj))
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
if (!_locks.TryGetValue(key, out obj))
|
||||
{
|
||||
throw new Exception("错了");
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using System.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Sample.MySql.DbContexts;
|
||||
using Sample.MySql.Domain.Entities;
|
||||
using Sample.MySql.multi;
|
||||
|
@ -8,6 +10,7 @@ using Sample.MySql.Shardings;
|
|||
using ShardingCore;
|
||||
using ShardingCore.Bootstrappers;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.ModelCacheLockerProviders;
|
||||
using ShardingCore.Core.RuntimeContexts;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Extensions;
|
||||
|
@ -55,46 +58,7 @@ namespace Sample.MySql
|
|||
{
|
||||
// services.AddHostedService<AutoStart>();
|
||||
services.AddControllers();
|
||||
// services.AddShardingDbContext<ShardingDefaultDbContext, DefaultDbContext>(o => o.UseMySql(hostBuilderContext.Configuration.GetSection("MySql")["ConnectionString"],new MySqlServerVersion("5.7.15"))
|
||||
// ,op =>
|
||||
// {
|
||||
// op.EnsureCreatedWithOutShardingTable = true;
|
||||
// op.CreateShardingTableOnStart = true;
|
||||
// op.UseShardingOptionsBuilder((connection, builder) => builder.UseMySql(connection,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger),
|
||||
// (conStr,builder)=> builder.UseMySql(conStr,new MySqlServerVersion("5.7.15")).UseLoggerFactory(efLogger));
|
||||
// op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
|
||||
// op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
|
||||
// });
|
||||
// services.AddMultiShardingDbContext<OtherDbContext>()
|
||||
// .UseRouteConfig(op =>
|
||||
// {
|
||||
// op.AddShardingTableRoute<MyUserRoute>();
|
||||
// })
|
||||
// .UseConfig((sp,o) =>
|
||||
// {
|
||||
// o.ThrowIfQueryRouteNotMatch = false;
|
||||
// o.UseShardingQuery((conStr, builder) =>
|
||||
// {
|
||||
// builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
|
||||
// .UseLoggerFactory(efLogger)
|
||||
// .EnableSensitiveDataLogging()
|
||||
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
// });
|
||||
// o.UseShardingTransaction((connection, builder) =>
|
||||
// {
|
||||
// builder
|
||||
// .UseMySql(connection, new MySqlServerVersion(new Version()))
|
||||
// .UseLoggerFactory(efLogger)
|
||||
// .EnableSensitiveDataLogging()
|
||||
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
// });
|
||||
// o.UseShardingMigrationConfigure(b =>
|
||||
// {
|
||||
// b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
||||
// });
|
||||
// o.AddDefaultDataSource("ds0",
|
||||
// "server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;");
|
||||
// }).ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>().AddShardingCore();
|
||||
services.AddSingleton<IMemoryCache>(sp => new MemoryCache(new MemoryCacheOptions { SizeLimit = 102400 }));
|
||||
services.AddShardingDbContext<DefaultShardingDbContext>()
|
||||
.UseRouteConfig(o =>
|
||||
{
|
||||
|
@ -104,6 +68,11 @@ namespace Sample.MySql
|
|||
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
|
||||
}).UseConfig((sp,o) =>
|
||||
{
|
||||
var memoryCache = sp.ApplicationServiceProvider.GetRequiredService<IMemoryCache>();
|
||||
o.UseExecutorDbContextConfigure(b =>
|
||||
{
|
||||
b.UseMemoryCache(memoryCache);
|
||||
});
|
||||
o.CacheModelLockConcurrencyLevel = 1024;
|
||||
o.CacheEntrySize = 1;
|
||||
o.CacheModelLockObjectSeconds = 10;
|
||||
|
@ -139,7 +108,7 @@ namespace Sample.MySql
|
|||
{
|
||||
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
|
||||
});
|
||||
})
|
||||
}).ReplaceService<IModelCacheLockerProvider,DicModelCacheLockerProvider>()
|
||||
.AddShardingCore();
|
||||
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
|
||||
// .UseMutliDefaultSharding<DefaultShardingDbContext>);
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
public class DefaultModelCacheLockerProvider : IModelCacheLockerProvider
|
||||
{
|
||||
private readonly ShardingConfigOptions _shardingConfigOptions;
|
||||
private readonly List<object> _locks;
|
||||
private readonly object[] _locks;
|
||||
|
||||
public DefaultModelCacheLockerProvider(ShardingConfigOptions shardingConfigOptions)
|
||||
{
|
||||
|
@ -21,10 +21,10 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
$"{shardingConfigOptions.CacheModelLockConcurrencyLevel} should > 0");
|
||||
}
|
||||
|
||||
_locks = new List<object>(shardingConfigOptions.CacheModelLockConcurrencyLevel);
|
||||
_locks = new object [shardingConfigOptions.CacheModelLockConcurrencyLevel];
|
||||
for (int i = 0; i < shardingConfigOptions.CacheModelLockConcurrencyLevel; i++)
|
||||
{
|
||||
_locks.Add(new object());
|
||||
_locks[i] = new object();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,14 +54,13 @@ namespace ShardingCore.Core.ModelCacheLockerProviders
|
|||
$"modelCacheKey is null cant {nameof(GetCacheLockObject)}");
|
||||
}
|
||||
|
||||
if (_locks.Count == 1)
|
||||
if (_locks.Length == 1)
|
||||
{
|
||||
return _locks[0];
|
||||
}
|
||||
|
||||
var hashCode = (modelCacheKey.ToString() ?? "").GetHashCode();
|
||||
var lockIndex = hashCode % _locks.Count;
|
||||
var index = lockIndex >= 0 ? lockIndex : Math.Abs(lockIndex);
|
||||
var index = Math.Abs(hashCode % _locks.Length);
|
||||
return _locks[index];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,44 @@
|
|||
using System.Diagnostics;
|
||||
using ShardingCore.Core.Collections;
|
||||
using ShardingCore.Helpers;
|
||||
using Xunit;
|
||||
|
||||
namespace ShardingCore.CommonTest
|
||||
{
|
||||
public class CommonTest
|
||||
{
|
||||
[Fact]
|
||||
public void TestHashCode()
|
||||
{
|
||||
var list = new List<string>();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
list.Add(Guid.NewGuid().ToString()+Guid.NewGuid().ToString()+Guid.NewGuid().ToString()+Guid.NewGuid().ToString());
|
||||
}
|
||||
Stopwatch sp =Stopwatch.StartNew();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
|
||||
ShardingCoreHelper.GetStringHashCode(list[i]);
|
||||
}
|
||||
sp.Stop();
|
||||
var x = sp.ElapsedMilliseconds;
|
||||
sp.Restart();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
list[i].GetHashCode();
|
||||
}
|
||||
sp.Stop();
|
||||
var y = sp.ElapsedMilliseconds;
|
||||
sp.Restart();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
ShardingCoreHelper.GetStringHashCode(list[i]);
|
||||
}
|
||||
sp.Stop();
|
||||
var z = sp.ElapsedMilliseconds;
|
||||
|
||||
}
|
||||
[Fact]
|
||||
public void TestList()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue