diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 3b8e3339a..ddc01aa4e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -26,6 +26,7 @@ namespace SqlSugar public List>> 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(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey); } else { @@ -904,7 +905,7 @@ namespace SqlSugar var result = CacheSchemeMain.GetOrCreate(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(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate(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 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 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(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate(cacheService, this.QueryBuilder, () => { return GetCount(); }, CacheTime, this.Context,CacheKey); } else { @@ -1415,7 +1425,7 @@ namespace SqlSugar var result = CacheSchemeMain.GetOrCreate(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(cacheService, this.QueryBuilder, () => { return this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray()); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate(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>(cacheService, this.QueryBuilder, () => { return GetData(sqlObj); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate>(cacheService, this.QueryBuilder, () => { return GetData(sqlObj); }, CacheTime, this.Context,CacheKey); } else { @@ -1815,7 +1825,7 @@ namespace SqlSugar if (IsCache) { var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService; - result = CacheSchemeMain.GetOrCreate>(cacheService, this.QueryBuilder, () => { return GetData(sqlObj); }, CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate>(cacheService, this.QueryBuilder, () => { return GetData(sqlObj); }, CacheTime, this.Context,CacheKey); } else { diff --git a/Src/Asp.Net/SqlSugar/CacheScheme/CacheSchemeMain.cs b/Src/Asp.Net/SqlSugar/CacheScheme/CacheSchemeMain.cs index c487e330d..a40ac9fa4 100644 --- a/Src/Asp.Net/SqlSugar/CacheScheme/CacheSchemeMain.cs +++ b/Src/Asp.Net/SqlSugar/CacheScheme/CacheSchemeMain.cs @@ -7,9 +7,10 @@ namespace SqlSugar { internal class CacheSchemeMain { - public static T GetOrCreate(ICacheService cacheService, QueryBuilder queryBuilder, Func getData, int cacheDurationInSeconds, SqlSugarProvider context) + public static T GetOrCreate(ICacheService cacheService, QueryBuilder queryBuilder, Func 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; diff --git a/Src/Asp.Net/SqlSugar/Entities/CacheKey.cs b/Src/Asp.Net/SqlSugar/Entities/CacheKey.cs index 40cd87ade..0c7df50ea 100644 --- a/Src/Asp.Net/SqlSugar/Entities/CacheKey.cs +++ b/Src/Asp.Net/SqlSugar/Entities/CacheKey.cs @@ -7,12 +7,15 @@ namespace SqlSugar { public class CacheKey { + public string AppendKey { get; set; } public string Database { get; set; } public List Tables { get; set; } public List 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; } } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 70aed59d2..0d25c6d28 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -165,6 +165,7 @@ namespace SqlSugar List ToPageList(int pageIndex, int pageSize, ref int totalNumber); List ToPageList(int pageIndex, int pageSize, ref int totalNumber,ref int totalPage); Task> ToPageListAsync(int pageIndex, int pageSize, RefAsync totalNumber); + ISugarQueryable WithCache(string cacheKey,int cacheDurationInSeconds = int.MaxValue); ISugarQueryable WithCache(int cacheDurationInSeconds = int.MaxValue); ISugarQueryable WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue); string ToClassString(string className);