mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-09 02:44:58 +08:00
Code optimization
This commit is contained in:
@@ -10,18 +10,12 @@ namespace SqlSugar
|
|||||||
protected List<T> GetEntityList<T>(SqlSugarClient context, IDataReader dataReader, string fields)
|
protected List<T> GetEntityList<T>(SqlSugarClient context, IDataReader dataReader, string fields)
|
||||||
{
|
{
|
||||||
Type type = typeof(T);
|
Type type = typeof(T);
|
||||||
var cacheManager = context.Utilities.GetCacheInstance<IDataReaderEntityBuilder<T>>();
|
|
||||||
string key = "DataReaderToList." + fields + context.CurrentConnectionConfig.DbType + type.FullName;
|
string key = "DataReaderToList." + fields + context.CurrentConnectionConfig.DbType + type.FullName;
|
||||||
IDataReaderEntityBuilder<T> entytyList = null;
|
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetCacheInstance().GetOrCreate(key, () =>
|
||||||
if (cacheManager.ContainsKey(key))
|
|
||||||
{
|
{
|
||||||
entytyList = cacheManager[key];
|
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader).CreateBuilder(type);
|
||||||
}
|
return cacheResult;
|
||||||
else
|
});
|
||||||
{
|
|
||||||
entytyList = new IDataReaderEntityBuilder<T>(context, dataReader).CreateBuilder(type);
|
|
||||||
cacheManager.Add(key, entytyList);
|
|
||||||
}
|
|
||||||
List<T> result = new List<T>();
|
List<T> result = new List<T>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,12 +41,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
return this.Context.Utilities.GetCacheInstance<List<string>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,() =>
|
||||||
(cm, key) =>
|
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
|
||||||
return result.Select(it => it.DbColumnName).ToList();
|
return result.Select(it => it.DbColumnName).ToList();
|
||||||
@@ -56,12 +51,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
return this.Context.Utilities.GetCacheInstance<List<string>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,() =>
|
||||||
(cm, key) =>
|
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
|
||||||
return result.Select(it => it.DbColumnName).ToList();
|
return result.Select(it => it.DbColumnName).ToList();
|
||||||
@@ -216,12 +206,8 @@ namespace SqlSugar
|
|||||||
#region Private
|
#region Private
|
||||||
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
||||||
{
|
{
|
||||||
return this.Context.Utilities.GetCacheInstance<List<T>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
||||||
this.Context.Ado.IsEnableLogEvent = false;
|
this.Context.Ado.IsEnableLogEvent = false;
|
||||||
|
|||||||
@@ -18,12 +18,8 @@ namespace SqlSugar
|
|||||||
public EntityInfo GetEntityInfo(Type type)
|
public EntityInfo GetEntityInfo(Type type)
|
||||||
{
|
{
|
||||||
string cacheKey = "GetEntityInfo" + type.FullName;
|
string cacheKey = "GetEntityInfo" + type.FullName;
|
||||||
return this.Context.Utilities.GetCacheInstance<EntityInfo>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
EntityInfo result = new EntityInfo();
|
EntityInfo result = new EntityInfo();
|
||||||
var sugarAttributeInfo = type.GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault();
|
var sugarAttributeInfo = type.GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault();
|
||||||
|
|||||||
@@ -263,9 +263,9 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Cache
|
#region Cache
|
||||||
public ICacheService<T> GetCacheInstance<T>()
|
public ICacheService GetCacheInstance()
|
||||||
{
|
{
|
||||||
return ReflectionInoCache<T>.GetInstance();
|
return ReflectionInoCache.GetInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveCacheAll()
|
public void RemoveCacheAll()
|
||||||
|
|||||||
@@ -356,9 +356,9 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Services
|
#region Services
|
||||||
public static ICacheService<V> GetCacheInstance<V>()
|
public static ICacheService GetCacheInstance()
|
||||||
{
|
{
|
||||||
return ReflectionInoCache<V>.GetInstance();
|
return ReflectionInoCache.GetInstance();
|
||||||
}
|
}
|
||||||
public static ISerializeService GetSerializeInstance()
|
public static ISerializeService GetSerializeInstance()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -98,13 +98,8 @@ namespace SqlSugar
|
|||||||
public void InitMppingInfo(Type type)
|
public void InitMppingInfo(Type type)
|
||||||
{
|
{
|
||||||
string cacheKey = "Context.InitAttributeMappingTables" + type.FullName;
|
string cacheKey = "Context.InitAttributeMappingTables" + type.FullName;
|
||||||
var entityInfo = this.Context.Utilities.GetCacheInstance<EntityInfo>().GetOrCreate(cacheKey,
|
var entityInfo = this.Context.Utilities.GetCacheInstance().GetOrCreate<EntityInfo>(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
var cacheInfo = cm[key];
|
|
||||||
return cacheInfo;
|
|
||||||
},
|
|
||||||
(cm, key) =>
|
|
||||||
{
|
{
|
||||||
var reval = this.Context.EntityMaintenance.GetEntityInfo(type);
|
var reval = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
return reval;
|
return reval;
|
||||||
|
|||||||
@@ -3,15 +3,14 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public interface ICacheService<V>
|
public interface ICacheService
|
||||||
{
|
{
|
||||||
V this[string key] { get; }
|
void Add<V>(string key, V value);
|
||||||
void Add(string key, V value);
|
void Add<V>(string key, V value, int cacheDurationInSeconds);
|
||||||
void Add(string key, V value, int cacheDurationInSeconds);
|
bool ContainsKey<V>(string key);
|
||||||
bool ContainsKey(string key);
|
V Get<V>(string key);
|
||||||
V Get(string key);
|
IEnumerable<string> GetAllKey<V>();
|
||||||
IEnumerable<string> GetAllKey();
|
void Remove<V>(string key);
|
||||||
void Remove(string key);
|
V GetOrCreate<V>(string cacheKey, Func<V> create);
|
||||||
V GetOrCreate(string cacheKey, Func<ICacheService<V>, string, V> successAction, Func<ICacheService<V>, string, V> errorAction);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,55 @@ using System.Collections;
|
|||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class ReflectionInoCache<V> : ICacheService<V>
|
public class ReflectionInoCache : ICacheService
|
||||||
|
{
|
||||||
|
private static ReflectionInoCache _instance = null;
|
||||||
|
private static readonly object _instanceLock = new object();
|
||||||
|
public static ReflectionInoCache GetInstance()
|
||||||
|
{
|
||||||
|
if (_instance == null)
|
||||||
|
lock (_instanceLock)
|
||||||
|
if (_instance == null)
|
||||||
|
{
|
||||||
|
_instance = new ReflectionInoCache();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
public void Add<V>(string key, V value)
|
||||||
|
{
|
||||||
|
ReflectionInoCache<V>.GetInstance().Add(key,value);
|
||||||
|
}
|
||||||
|
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||||
|
{
|
||||||
|
ReflectionInoCache<V>.GetInstance().Add(key, value,cacheDurationInSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey<V>(string key)
|
||||||
|
{
|
||||||
|
return ReflectionInoCache<V>.GetInstance().ContainsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V Get<V>(string key)
|
||||||
|
{
|
||||||
|
return ReflectionInoCache<V>.GetInstance().Get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetAllKey<V>()
|
||||||
|
{
|
||||||
|
return ReflectionInoCache<V>.GetInstance().GetAllKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
public V GetOrCreate<V>(string cacheKey, Func<V> create)
|
||||||
|
{
|
||||||
|
return ReflectionInoCache<V>.GetInstance().GetOrCreate(cacheKey, create);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove<V>(string key)
|
||||||
|
{
|
||||||
|
ReflectionInoCache<V>.GetInstance().Remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class ReflectionInoCache<V>
|
||||||
{
|
{
|
||||||
readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();
|
readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();
|
||||||
private static ReflectionInoCache<V> _instance = null;
|
private static ReflectionInoCache<V> _instance = null;
|
||||||
@@ -76,14 +124,13 @@ namespace SqlSugar
|
|||||||
return this.InstanceCache.Keys;
|
return this.InstanceCache.Keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
public V GetOrCreate(string cacheKey, Func<ICacheService<V>, string, V> successAction, Func<ICacheService<V>, string, V> errorAction)
|
public V GetOrCreate(string cacheKey, Func<V> create)
|
||||||
{
|
{
|
||||||
var cm = ReflectionInoCache<V>.GetInstance();
|
if (this.ContainsKey(cacheKey)) return Get(cacheKey);
|
||||||
if (cm.ContainsKey(cacheKey)) return successAction(cm, cacheKey);
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var reval = errorAction(cm, cacheKey);
|
var reval = create();
|
||||||
cm.Add(cacheKey, reval);
|
this.Add(cacheKey, reval);
|
||||||
return reval;
|
return reval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace SqlSugar
|
|||||||
T TranslateCopy<T>(T sourceObject);
|
T TranslateCopy<T>(T sourceObject);
|
||||||
SqlSugarClient CopyContext(SqlSugarClient context, bool isCopyEvents = false);
|
SqlSugarClient CopyContext(SqlSugarClient context, bool isCopyEvents = false);
|
||||||
dynamic DataTableToDynamic(DataTable table);
|
dynamic DataTableToDynamic(DataTable table);
|
||||||
ICacheService<T> GetCacheInstance<T>();
|
ICacheService GetCacheInstance();
|
||||||
void RemoveCacheAll();
|
void RemoveCacheAll();
|
||||||
void RemoveCacheAll<T>();
|
void RemoveCacheAll<T>();
|
||||||
void RemoveCache<T>(string key);
|
void RemoveCache<T>(string key);
|
||||||
|
|||||||
@@ -170,12 +170,8 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
return this.Context.Utilities.GetCacheInstance<List<DbColumnInfo>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
string sql = "select * from " + tableName + " WHERE 1=2 ";
|
string sql = "select * from " + tableName + " WHERE 1=2 ";
|
||||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||||
@@ -211,12 +207,8 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
return this.Context.Utilities.GetCacheInstance<List<string>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||||
string sql = @" select cu.COLUMN_name KEYNAME from user_cons_columns cu, user_constraints au
|
string sql = @" select cu.COLUMN_name KEYNAME from user_cons_columns cu, user_constraints au
|
||||||
|
|||||||
@@ -168,12 +168,8 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
return this.Context.Utilities.GetCacheInstance<List<DbColumnInfo>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
string sql = "select * from " + tableName + " limit 0,1";
|
string sql = "select * from " + tableName + " limit 0,1";
|
||||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||||
@@ -260,12 +256,8 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
||||||
{
|
{
|
||||||
return this.Context.Utilities.GetCacheInstance<List<T>>().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey,
|
||||||
(cm, key) =>
|
() =>
|
||||||
{
|
|
||||||
return cm[cacheKey];
|
|
||||||
|
|
||||||
}, (cm, key) =>
|
|
||||||
{
|
{
|
||||||
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
||||||
this.Context.Ado.IsEnableLogEvent = false;
|
this.Context.Ado.IsEnableLogEvent = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user