mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-01 19:15:10 +08:00
-
This commit is contained in:
parent
bb00bfd19b
commit
449796d578
@ -9,7 +9,7 @@ namespace SqlSugar
|
||||
{
|
||||
protected List<T> GetEntityList<T>(Type type, SqlSugarClient context, IDataReader dataReader, string fields)
|
||||
{
|
||||
var cacheManager = CacheManager<IDataReaderEntityBuilder<T>>.GetInstance();
|
||||
var cacheManager = context.RewritableMethods.GetCacheInstance<IDataReaderEntityBuilder<T>>();
|
||||
string key = "DataReaderToList." + fields + context.CurrentConnectionConfig.DbType + type.FullName;
|
||||
IDataReaderEntityBuilder<T> eblist = null;
|
||||
if (cacheManager.ContainsKey(key))
|
||||
|
@ -69,7 +69,7 @@ namespace SqlSugar
|
||||
#region Private
|
||||
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
||||
{
|
||||
return CacheFactory.Func<List<T>>(cacheKey,
|
||||
return this.Context.RewritableMethods.GetCacheInstance<List<T>>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[cacheKey];
|
||||
|
@ -18,7 +18,7 @@ namespace SqlSugar
|
||||
public EntityInfo GetEntityInfo(Type type)
|
||||
{
|
||||
string cacheKey = "GetEntityInfo" + type.FullName;
|
||||
return CacheFactory.Func<EntityInfo>(cacheKey,
|
||||
return this.Context.RewritableMethods.GetCacheInstance<EntityInfo>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[cacheKey];
|
||||
|
@ -4,10 +4,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class CacheManager<V>
|
||||
public class CacheManager<V> : ICacheManager<V>
|
||||
{
|
||||
readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();
|
||||
private static CacheManager<V> _instance = null;
|
||||
@ -26,7 +25,7 @@ namespace SqlSugar
|
||||
{
|
||||
return this.InstanceCache.ContainsKey(key);
|
||||
}
|
||||
|
||||
|
||||
public V Get(string key)
|
||||
{
|
||||
if (this.ContainsKey(key))
|
||||
@ -34,7 +33,7 @@ namespace SqlSugar
|
||||
else
|
||||
return default(V);
|
||||
}
|
||||
|
||||
|
||||
public static CacheManager<V> GetInstance()
|
||||
{
|
||||
if (_instance == null)
|
||||
@ -43,12 +42,12 @@ namespace SqlSugar
|
||||
_instance = new CacheManager<V>();
|
||||
return _instance;
|
||||
}
|
||||
|
||||
|
||||
public void Add(string key, V value)
|
||||
{
|
||||
this.InstanceCache.GetOrAdd(key, value);
|
||||
}
|
||||
|
||||
|
||||
public void Add(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
Add(key, value);
|
||||
@ -65,21 +64,17 @@ namespace SqlSugar
|
||||
return this.InstanceCache.Keys;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class CacheFactory {
|
||||
public static void Action<T>(string cacheKey, Action<CacheManager<T>, string> successAction, Func<CacheManager<T>, string, T> errorAction)
|
||||
public void Action(string cacheKey, Action<ICacheManager<V>, string> successAction, Func<ICacheManager<V>, string, V> errorAction)
|
||||
{
|
||||
var cm = CacheManager<T>.GetInstance();
|
||||
if (cm.ContainsKey(cacheKey)) successAction(cm, cacheKey);
|
||||
if (this.ContainsKey(cacheKey)) successAction(this, cacheKey);
|
||||
else
|
||||
{
|
||||
cm.Add(cacheKey, errorAction(cm, cacheKey));
|
||||
this.Add(cacheKey, errorAction(this, cacheKey));
|
||||
}
|
||||
}
|
||||
public static T Func<T>(string cacheKey, Func<CacheManager<T>, string, T> successAction, Func<CacheManager<T>, string, T> errorAction)
|
||||
public V Func(string cacheKey, Func<ICacheManager<V>, string, V> successAction, Func<ICacheManager<V>, string, V> errorAction)
|
||||
{
|
||||
var cm = CacheManager<T>.GetInstance();
|
||||
var cm = CacheManager<V>.GetInstance();
|
||||
if (cm.ContainsKey(cacheKey)) return successAction(cm, cacheKey);
|
||||
else
|
||||
{
|
||||
|
@ -188,5 +188,10 @@ namespace SqlSugar
|
||||
return JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(deserializeObject));
|
||||
|
||||
}
|
||||
|
||||
public ICacheManager<T> GetCacheInstance<T>()
|
||||
{
|
||||
return CacheManager<T>.GetInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
SqlSugar/Interface/ICacheManager.cs
Normal file
18
SqlSugar/Interface/ICacheManager.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface ICacheManager<V>
|
||||
{
|
||||
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 Func(string cacheKey, Func<ICacheManager<V>, string, V> successAction, Func<ICacheManager<V>, string, V> errorAction);
|
||||
void Action(string cacheKey, Action<ICacheManager<V>, string> successAction, Func<ICacheManager<V>, string, V> errorAction);
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ namespace SqlSugar
|
||||
T DeserializeObject<T>(string value);
|
||||
T TranslateCopy<T>(T sourceObject);
|
||||
dynamic DataTableToDynamic(DataTable table);
|
||||
ICacheManager<T> GetCacheInstance<T>();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,7 @@
|
||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
|
||||
<Compile Include="Interface\ICacheManager.cs" />
|
||||
<Compile Include="Entities\SugarMessageResult.cs" />
|
||||
<Compile Include="Enum\InitKeyType.cs" />
|
||||
<Compile Include="Realization\MySql\DbType.cs" />
|
||||
|
@ -69,7 +69,7 @@ namespace SqlSugar
|
||||
protected void InitMppingInfo<T>()
|
||||
{
|
||||
string cacheKey = "Context.InitAttributeMappingTables"+typeof(T).FullName;
|
||||
var entityInfo=CacheFactory.Func<EntityInfo>(cacheKey,
|
||||
var entityInfo=this.Context.RewritableMethods.GetCacheInstance<EntityInfo>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
var cacheInfo = cm[key];
|
||||
|
Loading…
Reference in New Issue
Block a user