wanshenmean
2026-03-06 aefdecd0aa3226b7d00d1dc764241b82658b3be8
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_RedisService/Cache/RedisCacheService.cs
@@ -365,6 +365,34 @@
            return Db.StringSet(fullKey, newJson, expiry, When.Exists);
        }
        public bool TrySafeUpdate<T>(
            string key,
            T newValue,
            object? expectedVersion,
            Func<T, object?> versionExtractor,
            int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (existing.IsNullOrEmpty) return false;
            // 反序列化现有值
            var existingValue = _serializer.Deserialize<T>(existing.ToString()!);
            if (existingValue == null) return false;
            // 检查版本是否匹配
            var currentVersion = versionExtractor(existingValue);
            if (!Equals(currentVersion, expectedVersion))
            {
                return false; // 版本不匹配,拒绝更新
            }
            // 版本匹配,执行更新
            var newJson = _serializer.Serialize(newValue);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newJson, expiry, When.Exists);
        }
        public string GetOrAdd(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);