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