|
@@ -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);
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|