From aefdecd0aa3226b7d00d1dc764241b82658b3be8 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 06 三月 2026 10:41:02 +0800
Subject: [PATCH] 添加机器人客户端;更新 WCS 缓存及任务
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/HybridCacheService.cs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 98 insertions(+), 0 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/HybridCacheService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/HybridCacheService.cs
index c991362..36f0375 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/HybridCacheService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/HybridCacheService.cs
@@ -573,6 +573,104 @@
}
}
+ /// <summary>
+ /// 瀹夊叏鏇存柊锛氫粎褰撳唴瀛樼紦瀛樹腑鐨勫�间笌expectedVersion鍖归厤鏃舵墠鏇存柊
+ /// 闃叉骞跺彂鍐欏叆鏃舵棫鍊艰鐩栨柊鍊�
+ /// </summary>
+ /// <typeparam name="T">鍊肩被鍨�</typeparam>
+ /// <param name="key">缂撳瓨閿�</param>
+ /// <param name="newValue">鏂板��</param>
+ /// <param name="expectedVersion">鏈熸湜鐨勭増鏈紙閫氬父鏄棫瀵硅薄鐨勫搱甯屽�兼垨鏃堕棿鎴筹級</param>
+ /// <param name="versionExtractor">浠庡璞℃彁鍙栫増鏈彿鐨勫嚱鏁�</param>
+ /// <param name="expireSeconds">杩囨湡鏃堕棿</param>
+ /// <returns>鏄惁鏇存柊鎴愬姛</returns>
+ public bool TrySafeUpdate<T>(
+ string key,
+ T newValue,
+ object? expectedVersion,
+ Func<T, object?> versionExtractor,
+ int expireSeconds = -1) where T : class
+ {
+ var fullKey = BuildKey(key);
+
+ // 浠嶳edis鑾峰彇褰撳墠鍊�
+ string? existingJson = null;
+ T? existingValue = default;
+ if (RedisAvailable)
+ {
+ try
+ {
+ var value = _connectionManager.GetDatabase().StringGet(fullKey);
+ if (!value.IsNullOrEmpty)
+ {
+ existingJson = value.ToString();
+ existingValue = _serializer.Deserialize<T>(existingJson);
+ }
+ }
+ catch { }
+ }
+
+ // 濡傛灉Redis涓嶅彲鐢紝浠庡唴瀛樼紦瀛樿幏鍙�
+ if (existingValue == null && _options.EnableL1Cache)
+ {
+ if (_memoryCache.TryGetValue(fullKey, out string? cached) && cached != null)
+ {
+ existingValue = _serializer.Deserialize<T>(cached);
+ existingJson = cached;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ // 妫�鏌ョ増鏈槸鍚﹀尮閰�
+ if (existingValue != null)
+ {
+ var currentVersion = versionExtractor(existingValue);
+ if (!Equals(currentVersion, expectedVersion))
+ {
+ _logger.LogWarning("TrySafeUpdate鐗堟湰涓嶅尮閰�, key={Key}, expected={Expected}, current={Current}",
+ key, expectedVersion, currentVersion);
+ return false; // 鐗堟湰涓嶅尮閰嶏紝鎷掔粷鏇存柊
+ }
+ }
+
+ // 鐗堟湰鍖归厤锛屾墽琛屾洿鏂�
+ var newJson = _serializer.Serialize(newValue);
+
+ // 鍏堝啓鍏edis
+ if (RedisAvailable)
+ {
+ try
+ {
+ var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
+ if (!_connectionManager.GetDatabase().StringSet(fullKey, newJson, expiry))
+ {
+ _logger.LogWarning("Redis TrySafeUpdate鍐欏叆澶辫触, key={Key}", key);
+ return _options.FallbackToMemory && _options.EnableL1Cache;
+ }
+ else
+ {
+ _logger.LogInformation("Redis TrySafeUpdate鍐欏叆鎴愬姛, key={Key}", key);
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.LogWarning(ex, "Redis TrySafeUpdate鍐欏叆澶辫触, key={Key}", key);
+ return _options.FallbackToMemory && _options.EnableL1Cache;
+ }
+ }
+
+ // 鏇存柊鍐呭瓨缂撳瓨
+ if (_options.EnableL1Cache)
+ {
+ SetMemoryCache(fullKey, newJson, expireSeconds, false);
+ }
+
+ return true;
+ }
+
public object? Get(Type type, string key)
{
var fullKey = BuildKey(key);
--
Gitblit v1.9.3