Browse Source

callcache

dss 2 years ago
parent
commit
7cee910b8d

+ 1 - 3
src/CallCenter.Application/Handlers/BaseHandler.cs

@@ -81,9 +81,7 @@ namespace CallCenter.Application.Handlers
                                 }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
 
                                 //处理队列记录
-                                var list = _callCacheManager.GetCallQueueList().ToList();
-                                list.Add(model);
-                                _callCacheManager.AddOrUpdateCallCache(list);
+                                _callCacheManager.AddCallCache(model);
 
                                 break;
                             case ECallType.ExtToOuter:

+ 1 - 3
src/CallCenter.Application/Handlers/FlowControl/IncomingNotificationHandler.cs

@@ -124,9 +124,7 @@ namespace CallCenter.Application.Handlers
                             model.InGroupTime = DateTime.Now;
                             await _callRepository.UpdateAsync(model, cancellationToken);
                             //处理队列记录
-                            var list = _callCacheManager.GetCallQueueList().ToList();
-                            list.Add(model);
-                            _callCacheManager.AddOrUpdateCallCache(list);
+                            _callCacheManager.AddCallCache(model);
                             break;
                         default:
                             break;

+ 15 - 5
src/CallCenter/Caches/CallCacheManager.cs

@@ -6,22 +6,32 @@ namespace CallCenter.Caches
 {
     public class CallCacheManager : ICallCacheManager, IScopeDependency
     {
-        private readonly ITypedCache<IReadOnlyList<Call>> _cacheCall;
+        private readonly ITypedCache<List<Call>> _cacheCall;
         private const string CallKey = "CallQueue";
 
-        public CallCacheManager(ITypedCache<IReadOnlyList<Call>> cacheCall)
+        public CallCacheManager(ITypedCache<List<Call>> cacheCall)
         {
             _cacheCall = cacheCall;
         }
 
-        public void AddOrUpdateCallCache(List<Call> list)
+        public void AddCallCache(Call call)
         {
+            var list = GetCallQueueList();
+            if (list==null)
+            {
+                list = new List<Call>();
+            }
+            list.Add(call);
             _cacheCall.AddOrUpdate(CallKey, list);
         }
 
-        public IReadOnlyList<Call> GetCallQueueList()
+        public List<Call>? GetCallQueueList()
         {
-            return _cacheCall.Get(CallKey)??new List<Call>();
+            var list = _cacheCall.GetOrAdd(CallKey, k =>
+            {
+                return new List<Call>();
+            });
+            return list;
         }
 
         public void RemoveCallCache(string id)

+ 2 - 2
src/CallCenter/Caches/ICallCacheManager.cs

@@ -4,9 +4,9 @@ namespace CallCenter.Caches
 {
     public interface ICallCacheManager
     {
-        IReadOnlyList<Call> GetCallQueueList();
+        List<Call>? GetCallQueueList();
 
-        void AddOrUpdateCallCache(List<Call> list);
+        void AddCallCache(Call call);
 
         void RemoveCallCache(string id);
     }