diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index cc2a19345..e253c110e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -22,6 +22,7 @@ namespace SqlSugar } public virtual void InitTables(Type entityType) { + this.Context.RewritableMethods.RemoveCacheAll(); if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index 32507ab4c..a1ffc5bac 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -39,6 +39,7 @@ namespace SqlSugar public void Init() { + this.Context.RewritableMethods.RemoveCacheAll(); if (!this.Context.DbMaintenance.IsAnySystemTablePermissions()) { Check.Exception(true, "Dbfirst and Codefirst requires system table permissions"); diff --git a/Src/Asp.Net/SqlSugar/Common/CacheManager.cs b/Src/Asp.Net/SqlSugar/Common/CacheManager.cs index a4fc5c4c1..a75924416 100644 --- a/Src/Asp.Net/SqlSugar/Common/CacheManager.cs +++ b/Src/Asp.Net/SqlSugar/Common/CacheManager.cs @@ -41,7 +41,8 @@ namespace SqlSugar if (_instance == null) { _instance = new CacheManager(); - CacheManager.Add(_instance.InstanceCache); + Action addItem =()=> { CacheManager.GetInstance().RemoveAllCache(); }; + CacheManager.Add(addItem); } return _instance; } @@ -62,6 +63,14 @@ namespace SqlSugar this.InstanceCache.TryRemove(key, out val); } + public void RemoveAllCache() + { + foreach (var key in GetAllKey()) + { + this.Remove(key); + } + } + public IEnumerable GetAllKey() { return this.InstanceCache.Keys; @@ -89,19 +98,19 @@ namespace SqlSugar } public static class CacheManager { - private static List CacheObjects = new List(); - internal static void Add(object CacheObject) + private static List removeActions = new List(); + internal static void Add(Action removeAction) { - CacheObjects.Add(CacheObject); + removeActions.Add(removeAction); } public static void RemoveAllCache() { - lock (CacheObjects) + lock (removeActions) { - for (int i = 0; i < CacheObjects.Count; i++) + foreach (var item in removeActions) { - CacheObjects[i] = Activator.CreateInstance(CacheObjects[i].GetType(), true); + item(); } } } diff --git a/Src/Asp.Net/SqlSugar/Common/RewritableMethods.cs b/Src/Asp.Net/SqlSugar/Common/RewritableMethods.cs index 5d82e1bbb..0a76ad3a0 100644 --- a/Src/Asp.Net/SqlSugar/Common/RewritableMethods.cs +++ b/Src/Asp.Net/SqlSugar/Common/RewritableMethods.cs @@ -188,6 +188,11 @@ namespace SqlSugar CacheManager.RemoveAllCache(); } + public void RemoveCacheAll() + { + CacheManager.GetInstance().RemoveAllCache(); + } + public void RemoveCache(string key) { CacheManager.GetInstance().Remove(key); diff --git a/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs b/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs index baf1bb5c5..730711572 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs @@ -17,6 +17,9 @@ namespace SqlSugar T TranslateCopy(T sourceObject); dynamic DataTableToDynamic(DataTable table); ICacheManager GetCacheInstance(); - + void RemoveCacheAll(); + void RemoveCacheAll(); + void RemoveCache(string key); + } }