WithCache Support Cache key

This commit is contained in:
sunkaixuna
2021-08-22 07:49:32 +08:00
parent 4cd5febece
commit 294b3a17c0
4 changed files with 25 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ namespace SqlSugar
public List<Action<List<T>>> Mappers { get; set; }
public bool IsCache { get; set; }
public int CacheTime { get; set; }
public string CacheKey { get; set; }
public bool IsAs { get; set; }
public QueryBuilder QueryBuilder
{
@@ -817,7 +818,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey);
}
else
{
@@ -904,7 +905,7 @@ namespace SqlSugar
var result = CacheSchemeMain.GetOrCreate<string>(cacheService, this.QueryBuilder, () =>
{
return this.Context.Utilities.SerializeObject(this.ToList(), typeof(T));
}, CacheTime, this.Context);
}, CacheTime, this.Context,CacheKey);
return result;
}
else
@@ -1027,7 +1028,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context,CacheKey);
}
else
{
@@ -1187,6 +1188,15 @@ namespace SqlSugar
return _ToSql();
}
}
public ISugarQueryable<T> WithCache(string cacheKey, int cacheDurationInSeconds = int.MaxValue)
{
cacheDurationInSeconds = SetCacheTime(cacheDurationInSeconds);
Check.ArgumentNullException(this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService, "Use Cache ConnectionConfig.ConfigureExternalServices.DataInfoCacheService is required ");
this.IsCache = true;
this.CacheTime = cacheDurationInSeconds;
this.CacheKey = cacheKey;
return this;
}
public ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue)
{
cacheDurationInSeconds = SetCacheTime(cacheDurationInSeconds);
@@ -1322,7 +1332,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<int>(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey);
}
else
{
@@ -1415,7 +1425,7 @@ namespace SqlSugar
var result = CacheSchemeMain.GetOrCreate<string>(cacheService, this.QueryBuilder, () =>
{
return this.Context.Utilities.SerializeObject(this.ToList(), typeof(T));
}, CacheTime, this.Context);
}, CacheTime, this.Context,CacheKey);
return result;
}
else
@@ -1444,7 +1454,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<DataTable>(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context,CacheKey);
}
else
{
@@ -1798,7 +1808,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context,CacheKey);
}
else
{
@@ -1815,7 +1825,7 @@ namespace SqlSugar
if (IsCache)
{
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context);
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context,CacheKey);
}
else
{

View File

@@ -7,9 +7,10 @@ namespace SqlSugar
{
internal class CacheSchemeMain
{
public static T GetOrCreate<T>(ICacheService cacheService, QueryBuilder queryBuilder, Func<T> getData, int cacheDurationInSeconds, SqlSugarProvider context)
public static T GetOrCreate<T>(ICacheService cacheService, QueryBuilder queryBuilder, Func<T> getData, int cacheDurationInSeconds, SqlSugarProvider context,string cacheKey)
{
CacheKey key = CacheKeyBuider.GetKey(context, queryBuilder);
key.AppendKey = cacheKey;
string keyString = key.ToString();
var result = cacheService.GetOrCreate(keyString, getData, cacheDurationInSeconds);
return result;

View File

@@ -7,12 +7,15 @@ namespace SqlSugar
{
public class CacheKey
{
public string AppendKey { get; set; }
public string Database { get; set; }
public List<string> Tables { get; set; }
public List<string> IdentificationList { get; set; }
public new string ToString()
{
return "SqlSugarDataCache" + UtilConstants.Dot + string.Join(UtilConstants.Dot, this.Tables) +UtilConstants.Dot+ string.Join(UtilConstants.Dot, this.IdentificationList.Where(it=>it.HasValue()));
var result= "SqlSugarDataCache" + UtilConstants.Dot + string.Join(UtilConstants.Dot, this.Tables) +UtilConstants.Dot+ string.Join(UtilConstants.Dot, this.IdentificationList.Where(it=>it.HasValue()));
result = result + AppendKey;
return result;
}
}
}

View File

@@ -165,6 +165,7 @@ namespace SqlSugar
List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber);
List<T> ToPageList(int pageIndex, int pageSize, ref int totalNumber,ref int totalPage);
Task<List<T>> ToPageListAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
ISugarQueryable<T> WithCache(string cacheKey,int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue);
ISugarQueryable<T> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue);
string ToClassString(string className);