From 781090b7d97784b50c1d64b9f2c60fffe50c6cb2 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 28 Sep 2017 13:12:22 +0800 Subject: [PATCH] Code optimization --- .../SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs | 6 +++--- .../Abstract/EntityMaintenance/EntityMaintenance.cs | 2 +- .../SqlSugar/Infrastructure/SqlSugarAccessory.cs | 2 +- Src/Asp.Net/SqlSugar/Interface/ICacheManager.cs | 3 +-- .../Oracle/DbMaintenance/OracleDbMaintenance.cs | 4 ++-- .../Sqlite/DbMaintenance/SqliteDbMaintenance.cs | 4 ++-- Src/Asp.Net/SqlSugar/Utilities/CacheManager.cs | 10 +--------- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 36817a8e2..d948602a7 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -41,7 +41,7 @@ namespace SqlSugar { string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; @@ -56,7 +56,7 @@ namespace SqlSugar { string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; @@ -216,7 +216,7 @@ namespace SqlSugar #region Private private List GetListOrCache(string cacheKey, string sql) { - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 794c36340..1ee65454c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -18,7 +18,7 @@ namespace SqlSugar public EntityInfo GetEntityInfo(Type type) { string cacheKey = "GetEntityInfo" + type.FullName; - return this.Context.Utilities.GetCacheInstance().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index 51e24ffe1..2f01296b3 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -98,7 +98,7 @@ namespace SqlSugar public void InitMppingInfo(Type type) { string cacheKey = "Context.InitAttributeMappingTables" + type.FullName; - var entityInfo = this.Context.Utilities.GetCacheInstance().Func(cacheKey, + var entityInfo = this.Context.Utilities.GetCacheInstance().GetOrCreate(cacheKey, (cm, key) => { var cacheInfo = cm[key]; diff --git a/Src/Asp.Net/SqlSugar/Interface/ICacheManager.cs b/Src/Asp.Net/SqlSugar/Interface/ICacheManager.cs index 4e6beaa6d..6bfb10531 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ICacheManager.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ICacheManager.cs @@ -12,7 +12,6 @@ namespace SqlSugar V Get(string key); IEnumerable GetAllKey(); void Remove(string key); - V Func(string cacheKey, Func, string, V> successAction, Func, string, V> errorAction); - void Action(string cacheKey, Action, string> successAction, Func, string, V> errorAction); + V GetOrCreate(string cacheKey, Func, string, V> successAction, Func, string, V> errorAction); } } \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs index fcca29b48..c31e016d3 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs @@ -170,7 +170,7 @@ namespace SqlSugar { string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; @@ -211,7 +211,7 @@ namespace SqlSugar { string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs index 3b320e6ae..ed139d6ff 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs @@ -168,7 +168,7 @@ namespace SqlSugar { string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; @@ -260,7 +260,7 @@ namespace SqlSugar } private List GetListOrCache(string cacheKey, string sql) { - return this.Context.Utilities.GetCacheInstance>().Func(cacheKey, + return this.Context.Utilities.GetCacheInstance>().GetOrCreate(cacheKey, (cm, key) => { return cm[cacheKey]; diff --git a/Src/Asp.Net/SqlSugar/Utilities/CacheManager.cs b/Src/Asp.Net/SqlSugar/Utilities/CacheManager.cs index a75924416..34e8a1b81 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/CacheManager.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/CacheManager.cs @@ -76,15 +76,7 @@ namespace SqlSugar return this.InstanceCache.Keys; } - public void Action(string cacheKey, Action, string> successAction, Func, string, V> errorAction) - { - if (this.ContainsKey(cacheKey)) successAction(this, cacheKey); - else - { - this.Add(cacheKey, errorAction(this, cacheKey)); - } - } - public V Func(string cacheKey, Func, string, V> successAction, Func, string, V> errorAction) + public V GetOrCreate(string cacheKey, Func, string, V> successAction, Func, string, V> errorAction) { var cm = CacheManager.GetInstance(); if (cm.ContainsKey(cacheKey)) return successAction(cm, cacheKey);