Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加全局缓存工厂 #1824

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Examples/base_entity/MessagePackMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MessagePackMap01
[Key(0)]
public string name { get; set; }
[Key(1)]
public string address { get;set; }
public string address { get; set; }
}

namespace FreeSql.DataAnnotations
Expand All @@ -31,10 +31,10 @@ public class MessagePackMapAttribute : Attribute { }
public static class FreeSqlMessagePackMapCoreExtensions
{
internal static int _isAoped = 0;
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
static ConcurrentDictionary<Type, bool> _dicTypes =FreeSql.Internal. Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, bool>>();
static MethodInfo MethodMessagePackSerializerDeserialize = typeof(MessagePackSerializer).GetMethod("Deserialize", new[] { typeof(Type), typeof(ReadOnlyMemory<byte>), typeof(MessagePackSerializerOptions), typeof(CancellationToken) });
static MethodInfo MethodMessagePackSerializerSerialize = typeof(MessagePackSerializer).GetMethod("Serialize", new[] { typeof(Type), typeof(object), typeof(MessagePackSerializerOptions), typeof(CancellationToken) });
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicMessagePackMapFluentApi = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicMessagePackMapFluentApi =FreeSql.Internal. Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>>();
static object _concurrentObj = new object();

public static ColumnFluent MessagePackMap(this ColumnFluent col)
Expand All @@ -55,7 +55,7 @@ public static void UseMessagePackMap(this IFreeSql that, MessagePackSerializerOp
{
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) =>
{
if (_dicTypes.ContainsKey(type))
if (_dicTypes.ContainsKey(type))
return Expression.IfThenElse(
Expression.TypeIs(valueExp, type),
Expression.Return(returnTarget, valueExp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace FreeSql
{
public class AggregateRootUtils
{
static ConcurrentDictionary<PropertyInfo, ConcurrentDictionary<string, AggregateRootBoundaryAttribute>> _dicGetPropertyBoundaryAttribute = new ConcurrentDictionary<PropertyInfo, ConcurrentDictionary<string, AggregateRootBoundaryAttribute>>();
static ConcurrentDictionary<PropertyInfo, ConcurrentDictionary<string, AggregateRootBoundaryAttribute>> _dicGetPropertyBoundaryAttribute
= Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<PropertyInfo, ConcurrentDictionary<string, AggregateRootBoundaryAttribute>>>();
public static AggregateRootBoundaryAttribute GetPropertyBoundaryAttribute(PropertyInfo prop, string boundaryName)
{
if (boundaryName == null) return null;
Expand Down Expand Up @@ -161,7 +162,7 @@ void LocalCompareEntityValueCollection(Type elementType, IEnumerable collectionB
{
if (dictAfter.ContainsKey(key) == false)
dictAfter.Add(key, item);
else if (key == "0" && table.Primarys.Length == 1 &&
else if (key == "0" && table.Primarys.Length == 1 &&
new[] { typeof(long), typeof(int) }.Contains(table.Primarys[0].CsType))
tracking.InsertLog.Add(NativeTuple.Create(elementType, item));
}
Expand Down Expand Up @@ -215,8 +216,9 @@ public static bool CompareEntityPropertyValue(Type type, object propvalBefore, o
if (propvalBefore == null && propvalAfter != null) return false;
if (propvalBefore != null && propvalAfter == null) return false;

if (FreeSql.Internal.Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(type)) {
if (type.FullName.StartsWith("Newtonsoft."))
if (FreeSql.Internal.Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(type))
{
if (type.FullName.StartsWith("Newtonsoft."))
return object.Equals(propvalBefore.ToString(), propvalAfter.ToString());

if (typeof(IDictionary).IsAssignableFrom(type))
Expand Down Expand Up @@ -261,7 +263,7 @@ public static bool CompareEntityPropertyValue(Type type, object propvalBefore, o

if (type.FullName.StartsWith("System.") ||
type.FullName.StartsWith("Npgsql.") ||
type.FullName.StartsWith("NetTopologySuite."))
type.FullName.StartsWith("NetTopologySuite."))
return object.Equals(propvalBefore, propvalAfter);

if (type.IsClass)
Expand Down Expand Up @@ -434,7 +436,8 @@ void LocalMapEntityValueCollection(Type entityType, object entityFrom, object en
}
}

static ConcurrentDictionary<string, ConcurrentDictionary<Type, ConcurrentDictionary<Type, Action<ISelect0>>>> _dicGetAutoIncludeQuery = new ConcurrentDictionary<string, ConcurrentDictionary<Type, ConcurrentDictionary<Type, Action<ISelect0>>>>();
static ConcurrentDictionary<string, ConcurrentDictionary<Type, ConcurrentDictionary<Type, Action<ISelect0>>>> _dicGetAutoIncludeQuery
= Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<string, ConcurrentDictionary<Type, ConcurrentDictionary<Type, Action<ISelect0>>>>>();
public static ISelect<TEntity> GetAutoIncludeQuery<TEntity>(string boundaryName, ISelect<TEntity> select)
{
var select0p = select as Select0Provider;
Expand Down Expand Up @@ -644,8 +647,8 @@ public static void SetNavigateRelationshipValue(IFreeSql orm, TableRef tbref, Ty
case TableRefType.ManyToOne:
for (var idx = 0; idx < tbref.RefColumns.Count; idx++)
{
var colval = rightItem == null ?
tbref.Columns[idx].CsType.CreateInstanceGetDefaultValue() :
var colval = rightItem == null ?
tbref.Columns[idx].CsType.CreateInstanceGetDefaultValue() :
Utils.GetDataReaderValue(tbref.Columns[idx].CsType, EntityUtilExtensions.GetEntityValueWithPropertyName(orm, tbref.RefEntityType, rightItem, tbref.RefColumns[idx].CsName));
EntityUtilExtensions.SetEntityValueWithPropertyName(orm, leftType, leftItem, tbref.Columns[idx].CsName, colval);
}
Expand Down
4 changes: 2 additions & 2 deletions Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
public static class FreeSqlJsonMapCoreExtensions
{
static int _isAoped = 0;
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
static ConcurrentDictionary<Type, bool> _dicTypes =FreeSql.Internal. Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, bool>>();
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object), typeof(JsonSerializerSettings) });
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicJsonMapFluentApi = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicJsonMapFluentApi =FreeSql.Internal. Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>>();
static object _concurrentObj = new object();

public static ColumnFluent JsonMap(this ColumnFluent col)
Expand Down
5 changes: 3 additions & 2 deletions FreeSql.DbContext/DbContext/DbContextAsync.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using FreeSql.Internal;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,7 +20,7 @@ public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationTo
return SaveChangesSuccess();
}

static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>> _dicFlushCommandDbSetBatchAsync = Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>>>();
internal async Task FlushCommandAsync(CancellationToken cancellationToken)
{
if (isFlushCommanding) return;
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/DbContext/DbContextSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public virtual int SaveChanges()
return SaveChangesSuccess();
}

static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>> _dicFlushCommandDbSetBatch = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>> _dicFlushCommandDbSetBatch = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], int>>>>();
bool isFlushCommanding = false;
/// <summary>
/// 刷新队列中的命令
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/DbSet/DbSetSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void AddOrUpdateNavigate(TEntity item, bool isAdd, string propertyName)
else if (_table.Properties.TryGetValue(propertyName, out var prop))
action(prop);
}
static ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> _dicLazyIsSetField = new ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> _dicLazyIsSetField = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>>();
object GetItemValue(TEntity item, PropertyInfo prop)
{
object propVal = null;
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/Extensions/FreeSqlDbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ public static IFreeSql SetDbContextOptions(this IFreeSql that, Action<DbContextO
_dicSetDbContextOptions.AddOrUpdate(that.Ado.Identifier, cfg, (t, o) => cfg);
return that;
}
internal static ConcurrentDictionary<Guid, DbContextOptions> _dicSetDbContextOptions = new ConcurrentDictionary<Guid, DbContextOptions>();
internal static ConcurrentDictionary<Guid, DbContextOptions> _dicSetDbContextOptions = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Guid, DbContextOptions>>();
}
18 changes: 0 additions & 18 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public RepositoryDbContext(IFreeSql orm, IBaseRepository repo) : base()
_repo = repo;
}

static ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> _dicGetRepositoryDbField = new ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> _dicGetRepositoryDbField = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>>();
static FieldInfo GetRepositoryDbField(Type type, string fieldName) => _dicGetRepositoryDbField.GetOrAdd(type, tp => new ConcurrentDictionary<string, FieldInfo>()).GetOrAdd(fieldName, fn =>
typeof(BaseRepository<,>).MakeGenericType(type, typeof(int)).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic));
public override IDbSet Set(Type entityType)
Expand All @@ -33,10 +33,10 @@ public override IDbSet Set(Type entityType)
{
repo = Activator.CreateInstance(typeof(DefaultRepository<,>).MakeGenericType(entityType, typeof(int)), _repo.Orm);
(repo as IBaseRepository).UnitOfWork = _repo.UnitOfWork;
GetRepositoryDbField(entityType, "_dbPriv").SetValue(repo, this);
GetRepositoryDbField(entityType, "_dbPriv").SetValue(repo, this);
GetRepositoryDbField(entityType, "_asTablePriv").SetValue(repo,
_repo.GetType().GetField("_asTablePriv", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_repo));
//GetRepositoryDbField(_repo.EntityType, "_asTablePriv").GetValue(_repo));
//GetRepositoryDbField(_repo.EntityType, "_asTablePriv").GetValue(_repo));

if (typeof(IBaseRepository<>).MakeGenericType(_repo.EntityType).IsAssignableFrom(_repo.GetType()))
typeof(RepositoryDbContext).GetMethod("SetRepositoryDataFilter").MakeGenericMethod(_repo.EntityType)
Expand Down Expand Up @@ -68,7 +68,8 @@ int SaveChangesSuccess()
{
UnitOfWork.EntityChangeReport.Report.AddRange(_entityChangeReport);
if (UnitOfWork.EntityChangeReport.OnChange == null) UnitOfWork.EntityChangeReport.OnChange = Options.OnEntityChange;
} else
}
else
EmitOnEntityChange(_entityChangeReport);
}
finally
Expand Down
4 changes: 2 additions & 2 deletions FreeSql.DbContext/Repository/DataFilter/DataFilterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class DataFilterUtil

internal static Action<FluentDataFilter> _globalDataFilter;

static ConcurrentDictionary<Type, Delegate> _dicSetRepositoryDataFilterApplyDataFilterFunc = new ConcurrentDictionary<Type, Delegate>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicSetRepositoryDataFilterConvertFilterNotExists = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
static ConcurrentDictionary<Type, Delegate> _dicSetRepositoryDataFilterApplyDataFilterFunc = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, Delegate>>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicSetRepositoryDataFilterConvertFilterNotExists = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>>();
internal static void SetRepositoryDataFilter(object repos, Action<FluentDataFilter> scopedDataFilter)
{
if (scopedDataFilter != null)
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UnitOfWork : IUnitOfWork
/// <summary>
/// 正在使用中的工作单元(调试)
/// </summary>
public static ConcurrentDictionary<string, UnitOfWork> DebugBeingUsed { get; } = new ConcurrentDictionary<string, UnitOfWork>();
public static ConcurrentDictionary<string, UnitOfWork> DebugBeingUsed { get; } = FreeSql.Internal.Utils.GlobalCacheFactory.CreateCacheItem<ConcurrentDictionary<string, UnitOfWork>>();

protected IFreeSql _fsql;
protected Object<DbConnection> _conn;
Expand Down
Loading