Skip to content

Commit b4bf986

Browse files
committed
修改优化tablecreator为无锁模型创建,优化添加无缓存路由,并且发布x.4.2.9
1 parent e7474d1 commit b4bf986

File tree

15 files changed

+234
-99
lines changed

15 files changed

+234
-99
lines changed

nuget-publish.bat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
:start
22
::定义版本
3-
set EFCORE2=2.4.2.08
4-
set EFCORE3=3.4.2.08
5-
set EFCORE5=5.4.2.08
6-
set EFCORE6=6.4.2.08
3+
set EFCORE2=2.4.2.09
4+
set EFCORE3=3.4.2.09
5+
set EFCORE5=5.4.2.09
6+
set EFCORE6=6.4.2.09
77

88
::删除所有bin与obj下的文件
99
@echo off

samples/Sample.SqlServer/UnionAllMerge/ShardingCoreSqlServerExtension.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public static void UseSqlServer<TShardingDbContext>(this ShardingConfigOptions<T
1515
{
1616
option.UseShardingQuery((conStr, builder) =>
1717
{
18-
builder.UseSqlServer(conStr);
18+
builder.UseSqlServer(conStr).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
1919
builderConfigure?.Invoke(builder);
2020
});
2121
option.UseShardingTransaction((connection, builder) =>
2222
{
23-
builder.UseSqlServer(connection);
23+
builder.UseSqlServer(connection).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
2424
builderConfigure?.Invoke(builder);
2525
});
2626
}
@@ -33,7 +33,7 @@ public static DbContextOptionsBuilder UseUnionAllMerge<TShardingDbContext>(this
3333
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
3434
return optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory,
3535
UnionAllMergeSqlServerQuerySqlGeneratorFactory<TShardingDbContext>>()
36-
.ReplaceService<IQueryCompiler, UnionAllMergeQueryCompiler>(); ;
36+
.ReplaceService<IQueryCompiler, UnionAllMergeQueryCompiler>();
3737
}
3838

3939
}

samples/Sample.SqlServer3x/Controllers/WeatherForecastController.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,34 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger,Defau
3838
[HttpGet]
3939
public IEnumerable<WeatherForecast> Get()
4040
{
41-
//Console.WriteLine("---------------开始-----------------");
42-
//var s = DateTime.Now.ToString("HHmmss");
43-
//Task.Run(() =>
44-
//{
45-
// try
46-
// {
47-
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
48-
// _virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
49-
// _shardingTableCreator.CreateTable<SysUserMod>("A", s);
50-
// }
51-
// catch (Exception e)
52-
// {
53-
// Console.WriteLine(e);
54-
// }
55-
//});
56-
//Task.Run(() =>
57-
//{
58-
// try
59-
// {
60-
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
61-
// _virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
62-
// _shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
63-
// }
64-
// catch (Exception e)
65-
// {
66-
// Console.WriteLine(e);
67-
// }
68-
//});
41+
Console.WriteLine("---------------开始-----------------");
42+
var s = DateTime.Now.ToString("HHmmss");
43+
Task.Run(() =>
44+
{
45+
try
46+
{
47+
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
48+
_virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
49+
_shardingTableCreator.CreateTable<SysUserMod>("A", s);
50+
}
51+
catch (Exception e)
52+
{
53+
Console.WriteLine(e);
54+
}
55+
});
56+
Task.Run(() =>
57+
{
58+
try
59+
{
60+
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
61+
_virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
62+
_shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
63+
}
64+
catch (Exception e)
65+
{
66+
Console.WriteLine(e);
67+
}
68+
});
6969
//try
7070
//{
7171
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Caching.Memory;
7+
8+
namespace ShardingCore.Core
9+
{
10+
/// <summary>
11+
///
12+
/// </summary>
13+
/// Author: xjm
14+
/// Created: 2022/4/15 11:40:58
15+
/// Email: [email protected]
16+
public interface IShardingModelCacheOption
17+
{
18+
#if !EFCORE2
19+
CacheItemPriority GetModelCachePriority();
20+
int GetModelCacheEntrySize();
21+
#endif
22+
int GetModelCacheLockObjectSeconds();
23+
}
24+
}

src/ShardingCore/Core/VirtualRoutes/RouteTailFactory.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ public class RouteTailFactory:IRouteTailFactory
1616
{
1717
public IRouteTail Create(string tail)
1818
{
19-
return new SingleQueryRouteTail(tail);
19+
return Create(tail, true);
20+
}
21+
22+
public IRouteTail Create(string tail, bool cache)
23+
{
24+
if (cache)
25+
{
26+
return new SingleQueryRouteTail(tail);
27+
}
28+
else
29+
{
30+
return new NoCacheSingleQueryRouteTail(tail);
31+
}
2032
}
2133

2234
public IRouteTail Create(TableRouteResult tableRouteResult)

src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IMultiQueryRouteTail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
99
* @Date: Sunday, 22 August 2021 09:44:54
1010
1111
*/
12-
public interface IMultiQueryRouteTail:IRouteTail
12+
public interface IMultiQueryRouteTail: INoCacheRouteTail
1313
{
1414
/// <summary>
1515
/// 获取对象类型的应该后缀
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
8+
{
9+
/// <summary>
10+
///
11+
/// </summary>
12+
/// Author: xjm
13+
/// Created: 2022/4/15 13:22:07
14+
/// Email: [email protected]
15+
public interface INoCacheRouteTail:IRouteTail
16+
{
17+
}
18+
}

src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTailFactory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions
1010
*/
1111
public interface IRouteTailFactory
1212
{
13+
/// <summary>
14+
/// dbcontext模型会被缓存
15+
/// </summary>
16+
/// <param name="tail"></param>
17+
/// <returns></returns>
1318
IRouteTail Create(string tail);
19+
/// <summary>
20+
/// cache false创建的dbcontext模型不会被缓存
21+
/// </summary>
22+
/// <param name="tail"></param>
23+
/// <param name="cache"></param>
24+
/// <returns></returns>
25+
IRouteTail Create(string tail, bool cache);
26+
/// <summary>
27+
/// dbcontext模型不会被缓存
28+
/// </summary>
29+
/// <param name="tableRouteResult"></param>
30+
/// <returns></returns>
1431
IRouteTail Create(TableRouteResult tableRouteResult);
1532
}
1633
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
7+
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
8+
using ShardingCore.Extensions;
9+
10+
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
/// Author: xjm
16+
/// Created: 2022/4/15 13:23:49
17+
/// Email: [email protected]
18+
public class NoCacheSingleQueryRouteTail:ISingleQueryRouteTail,INoCacheRouteTail
19+
{
20+
private readonly TableRouteResult _tableRouteResult;
21+
private readonly string _tail;
22+
private readonly string _modelCacheKey;
23+
private readonly bool _isShardingTableQuery;
24+
25+
public NoCacheSingleQueryRouteTail(TableRouteResult tableRouteResult)
26+
{
27+
if (tableRouteResult.ReplaceTables.IsEmpty() || tableRouteResult.ReplaceTables.Count > 1) throw new ArgumentException("route result replace tables must 1");
28+
_tableRouteResult = tableRouteResult;
29+
_tail = _tableRouteResult.ReplaceTables.First().Tail;
30+
_modelCacheKey = _tail.FormatRouteTail2ModelCacheKey();
31+
_isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail);
32+
}
33+
34+
public NoCacheSingleQueryRouteTail(string tail)
35+
{
36+
_tail = tail;
37+
_modelCacheKey = _tail.FormatRouteTail2ModelCacheKey();
38+
_isShardingTableQuery = !string.IsNullOrWhiteSpace(_tail);
39+
}
40+
public virtual string GetRouteTailIdentity()
41+
{
42+
return _modelCacheKey;
43+
}
44+
45+
public virtual bool IsMultiEntityQuery()
46+
{
47+
return false;
48+
}
49+
50+
public bool IsShardingTableQuery()
51+
{
52+
return _isShardingTableQuery;
53+
}
54+
55+
public virtual string GetTail()
56+
{
57+
return _tail;
58+
}
59+
}
60+
}

src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,24 @@ public override IModel GetModel(DbContext context, IConventionSetBuilder convent
4545
{
4646
if (context is IShardingTableDbContext shardingTableDbContext)
4747
{
48-
if (shardingTableDbContext.RouteTail is IMultiQueryRouteTail)
48+
if (shardingTableDbContext.RouteTail is INoCacheRouteTail)
4949
{
5050
var multiModel = CreateModel(context, conventionSetBuilder, validator);
5151
return multiModel;
5252
}
5353
}
5454

55+
int waitSeconds = 3;
56+
if (context is IShardingModelCacheOption shardingModelCacheOption)
57+
{
58+
waitSeconds = shardingModelCacheOption.GetModelCacheLockObjectSeconds();
59+
}
60+
5561
var cacheKey = Dependencies.ModelCacheKeyFactory.Create(context);
5662
if (!_models.TryGetValue(cacheKey, out var model))
5763
{
5864

59-
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(3));
65+
var acquire = Monitor.TryEnter(_syncObject, TimeSpan.FromSeconds(waitSeconds));
6066
if (!acquire)
6167
{
6268
throw new ShardingCoreInvalidOperationException("cache model timeout");

0 commit comments

Comments
 (0)