瀏覽代碼

add TryRemove

MIke 2 年之前
父節點
當前提交
637e378f87
共有 2 個文件被更改,包括 14 次插入15 次删除
  1. 3 2
      Interfaces/Cache/CacheMaintenance.cs
  2. 11 13
      Interfaces/Cache/CacheManager.cs

+ 3 - 2
Interfaces/Cache/CacheMaintenance.cs

@@ -21,8 +21,9 @@ namespace WingServerCommon.Interfaces.Cache
 
         private void OnCollectionDocumentChanged(Object sender, CollectionDocumentChangedArgs args)
         {
-            var typeName = $"I{args.CollectionName}Manager";
-            var type = Type.GetType(typeName);
+           var typeName = $"I{args.CollectionName}Manager";
+            //var type = Type.GetType(typeName);
+            var type  = _instances.FirstOrDefault(i=>i.Key.Name==typeName).Key;
             ICacheManager manager;
             if (_instances.TryGetValue(type, out manager))
             {

+ 11 - 13
Interfaces/Cache/CacheManager.cs

@@ -1,13 +1,11 @@
-using System;
-using System.Runtime.Serialization;
 using System.Collections.Concurrent;
 using WingServerCommon.Interfaces.OpLog;
 namespace WingServerCommon.Interfaces.Cache
 {
-    public class CacheManager<T> : IBaseCacheManager<T>  where T: ICacheObject
+    public class CacheManager<T> : IBaseCacheManager<T> where T : ICacheObject
     {
         protected ConcurrentDictionary<string, T> CacheList = new ConcurrentDictionary<string, T>();
-              
+
         /// <summary>
         /// Load cache from db objects
         /// </summary>
@@ -15,7 +13,7 @@ namespace WingServerCommon.Interfaces.Cache
         public void LoadFromDbOject(IList<T> dbOjects)
         {
             CacheList.Clear();
-            foreach(var dbOject in dbOjects)
+            foreach (var dbOject in dbOjects)
             {
                 Add(dbOject);
             }
@@ -27,10 +25,10 @@ namespace WingServerCommon.Interfaces.Cache
         /// <param name="documentId">The db collection document id</param>
         public void Remove(string documentId)
         {
-            var result = CacheList.TryRemove(documentId, out var cache);
-            if (!result)
+            CacheList.TryGetValue(documentId, out var cache);
+            if (cache != null)
             {
-                throw new InvalidOperationException($"Remove failed with id:{documentId}");
+                CacheList.TryRemove(documentId, out var removeCache);
             }
         }
 
@@ -38,9 +36,9 @@ namespace WingServerCommon.Interfaces.Cache
         /// Add a cache object to cache manager
         /// </summary>
         // <param name="cacheObj">A new cache object</param>    
-        public void Add(T cacheObj)           
+        public void Add(T cacheObj)
         {
-            var documentId =cacheObj.Code;
+            var documentId = cacheObj.Code;
             var result = CacheList.TryAdd(documentId, cacheObj);
             if (!result)
             {
@@ -55,10 +53,10 @@ namespace WingServerCommon.Interfaces.Cache
         public virtual void RemoveMany(CollectionDocumentChangedArgs args)
         {
             var ids = args.DocumentIds;
-            foreach(var id in ids)
+            foreach (var id in ids)
             {
                 Remove(id);
             }
-        }       
-    }    
+        }
+    }
 }