Skip to content

Commit

Permalink
优化模型处理默认的list改成array
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Nov 28, 2022
1 parent bf676d6 commit 1f810f9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 48 deletions.
44 changes: 44 additions & 0 deletions samples/Sample.MySql/DicModelCacheLockerProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
53 changes: 11 additions & 42 deletions samples/Sample.MySql/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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;
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;
Expand Down Expand Up @@ -54,47 +57,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
// 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.AddControllers();
services.AddSingleton<IMemoryCache>(sp => new MemoryCache(new MemoryCacheOptions { SizeLimit = 102400 }));
services.AddShardingDbContext<DefaultShardingDbContext>()
.UseRouteConfig(o =>
{
Expand All @@ -104,6 +68,11 @@ public void ConfigureServices(IServiceCollection services)
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;
Expand Down Expand Up @@ -139,7 +108,7 @@ public void ConfigureServices(IServiceCollection services)
{
b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
});
})
}).ReplaceService<IModelCacheLockerProvider,DicModelCacheLockerProvider>()
.AddShardingCore();
// services.AddDbContext<DefaultShardingDbContext>(ShardingCoreExtension
// .UseMutliDefaultSharding<DefaultShardingDbContext>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -21,10 +21,10 @@ public DefaultModelCacheLockerProvider(ShardingConfigOptions shardingConfigOptio
$"{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();
}
}

Expand Down Expand Up @@ -54,14 +54,13 @@ public object GetCacheLockObject(object modelCacheKey)
$"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];
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/ShardingCore.CommonTest/CommonTest.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down

0 comments on commit 1f810f9

Please sign in to comment.