From 17e5dbd7bd0364e27a33f1a7dab91cf33d5dcabc Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 04 三月 2026 11:52:12 +0800
Subject: [PATCH] 增强Redis缓存服务与设备通信优化

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs |  202 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
index 5f6d4ef..a1c7c18 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Caches/MemoryCacheService.cs
@@ -145,6 +145,183 @@
             keys.ToList().ForEach(item => _cache.Remove(item));
         }
 
+        #region 鍒犻櫎鎵╁睍鏂规硶
+
+        public string? RemoveAndGet(string key)
+        {
+            var value = Get(key);
+            if (value != null) _cache.Remove(key);
+            return value;
+        }
+
+        public T? RemoveAndGet<T>(string key) where T : class
+        {
+            var value = Get<T>(key);
+            if (value != null) _cache.Remove(key);
+            return value;
+        }
+
+        public int RemoveByPrefix(string prefix)
+        {
+            if (string.IsNullOrEmpty(prefix)) return 0;
+            // MemoryCache涓嶆敮鎸佹灇涓撅紝杩斿洖0
+            return 0;
+        }
+
+        public int RemoveByPattern(string pattern)
+        {
+            // MemoryCache涓嶆敮鎸佹ā寮忓尮閰�
+            return 0;
+        }
+
+        public int RemoveAll(IEnumerable<string> keys)
+        {
+            if (keys == null) return 0;
+            int count = 0;
+            foreach (var key in keys)
+            {
+                if (Remove(key)) count++;
+            }
+            return count;
+        }
+
+        public int RemoveWhere(Func<string, bool> predicate)
+        {
+            // MemoryCache涓嶆敮鎸佹灇涓炬墍鏈塊ey
+            return 0;
+        }
+
+        #endregion
+
+        #region 娣诲姞鍜屼慨鏀规墿灞曟柟娉�
+
+        public void AddAll(IDictionary<string, string> items, int expireSeconds = -1)
+        {
+            if (items == null) return;
+            foreach (var item in items)
+            {
+                Add(item.Key, item.Value, expireSeconds);
+            }
+        }
+
+        public void AddAllObjects(IDictionary<string, object> items, int expireSeconds = -1)
+        {
+            if (items == null) return;
+            foreach (var item in items)
+            {
+                AddObject(item.Key, item.Value, expireSeconds);
+            }
+        }
+
+        public bool Replace(string key, string newValue, int expireSeconds = -1)
+        {
+            return TryUpdate(key, newValue, expireSeconds);
+        }
+
+        public bool Replace<T>(string key, T newValue, int expireSeconds = -1) where T : class
+        {
+            if (!Exists(key)) return false;
+            AddObject(key, newValue, expireSeconds);
+            return true;
+        }
+
+        public string? GetAndRefresh(string key, int expireSeconds)
+        {
+            var value = Get(key);
+            if (value != null)
+            {
+                Add(key, value, expireSeconds);
+                return value;
+            }
+            return null;
+        }
+
+        public T? GetAndRefresh<T>(string key, int expireSeconds) where T : class
+        {
+            var value = Get<T>(key);
+            if (value != null)
+            {
+                AddObject(key, value, expireSeconds);
+                return value;
+            }
+            return default;
+        }
+
+        public bool RefreshExpire(string key, int expireSeconds)
+        {
+            var value = Get(key);
+            if (value != null)
+            {
+                Add(key, value, expireSeconds);
+                return true;
+            }
+            return false;
+        }
+
+        public bool ExpireIn(string key, int seconds)
+        {
+            return RefreshExpire(key, seconds);
+        }
+
+        public bool ExpireAt(string key, DateTime expireTime)
+        {
+            var seconds = (int)(expireTime - DateTime.Now).TotalSeconds;
+            if (seconds <= 0) return Remove(key);
+            return RefreshExpire(key, seconds);
+        }
+
+        public long? GetExpire(string key)
+        {
+            return null; // MemoryCache涓嶆敮鎸乀TL鏌ヨ
+        }
+
+        public bool AddIfNotExists(string key, string value, int expireSeconds = -1)
+        {
+            return TryAdd(key, value, expireSeconds);
+        }
+
+        public bool AddIfNotExists<T>(string key, T value, int expireSeconds = -1) where T : class
+        {
+            return TryAdd(key, value, expireSeconds);
+        }
+
+        public string? GetAndSet(string key, string newValue, int expireSeconds = -1)
+        {
+            var oldValue = Get(key);
+            Add(key, newValue, expireSeconds);
+            return oldValue;
+        }
+
+        public T? GetAndSet<T>(string key, T newValue, int expireSeconds = -1) where T : class
+        {
+            var oldValue = Get<T>(key);
+            AddObject(key, newValue, expireSeconds);
+            return oldValue;
+        }
+
+        public long Increment(string key, long value = 1)
+        {
+            var current = long.TryParse(Get(key), out var v) ? v : 0;
+            var newValue = current + value;
+            Add(key, newValue.ToString());
+            return newValue;
+        }
+
+        public long Decrement(string key, long value = 1)
+        {
+            return Increment(key, -value);
+        }
+
+        public long Append(string key, string value)
+        {
+            var current = Get(key) ?? "";
+            var newValue = current + value;
+            Add(key, newValue);
+            return newValue.Length;
+        }
+
+        #endregion
+
         public bool TryAdd(string key, string value, int expireSeconds = -1)
         {
             if (Exists(key)) return false;
@@ -190,6 +367,31 @@
             return true;
         }
 
+        public bool TryUpdateIfChanged(string key, string newValue, int expireSeconds = -1)
+        {
+            var existing = Get(key);
+            if (existing == null) return false;
+            if (existing == newValue) return false; // 鍊肩浉鍚岋紝涓嶆洿鏂�
+            Remove(key);
+            Add(key, newValue, expireSeconds);
+            return true;
+        }
+
+        public bool TryUpdateIfChanged<T>(string key, T newValue, int expireSeconds = -1) where T : class
+        {
+            var existing = Get<T>(key);
+            if (existing == null) return false;
+
+            // 浣跨敤JSON搴忓垪鍖栨瘮杈冨唴瀹癸紝鑰屼笉鏄紩鐢ㄦ瘮杈�
+            var existingJson = JsonConvert.SerializeObject(existing);
+            var newJson = JsonConvert.SerializeObject(newValue);
+            if (existingJson == newJson) return false; // JSON瀛楃涓茬浉鍚岋紝涓嶆洿鏂�
+
+            Remove(key);
+            AddObject(key, newValue, expireSeconds);
+            return true;
+        }
+
         public string GetOrAdd(string key, string value, int expireSeconds = -1)
         {
             var existing = _cache.Get(key)?.ToString();

--
Gitblit v1.9.3