From 1515ffa15c11e106f35e1447bc990b7867c449bb Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 16 四月 2026 16:07:14 +0800
Subject: [PATCH] feat(Robot): 机械手换盘任务特殊处理
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Redis使用案例.md | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 258 insertions(+), 12 deletions(-)
diff --git "a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Redis\344\275\277\347\224\250\346\241\210\344\276\213.md" "b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Redis\344\275\277\347\224\250\346\241\210\344\276\213.md"
index 8f86a1b..9ebca13 100644
--- "a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Redis\344\275\277\347\224\250\346\241\210\344\276\213.md"
+++ "b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Redis\344\275\277\347\224\250\346\241\210\344\276\213.md"
@@ -1,6 +1,6 @@
# Redis 鏈嶅姟浣跨敤妗堜緥
-## 1. 缂撳瓨锛圛CacheService锛�
+## 1. 缂撳瓨锛圛CacheService锛�- 鍩虹鎿嶄綔
閫氳繃鏋勯�犲嚱鏁版敞鍏� `ICacheService`锛孒ybridCacheService 鑷姩瀹炵幇 L1(鍐呭瓨) + L2(Redis) 鍙屽眰缂撳瓨銆�
@@ -38,7 +38,211 @@
}
```
-## 2. 鍒嗗竷寮忛攣锛圛DistributedLockService锛�
+## 2. 缂撳瓨锛圛CacheService锛�- 鎵╁睍鍒犻櫎鏂规硶
+
+```csharp
+public class CacheDeleteDemo
+{
+ private readonly ICacheService _cache;
+
+ // 鍒犻櫎骞惰幏鍙栧��
+ public string? RemoveAndGet(string key)
+ {
+ return _cache.RemoveAndGet(key); // 杩斿洖琚垹闄ょ殑鍊�
+ }
+
+ // 鎸夊墠缂�鍒犻櫎鎵�鏈夊尮閰嶇殑key
+ public int ClearUserCache()
+ {
+ return _cache.RemoveByPrefix("user:"); // 鍒犻櫎鎵�鏈� user: 寮�澶寸殑key
+ }
+
+ // 鎸夋ā寮忓垹闄わ紙鏀寔閫氶厤绗︼級
+ public int ClearSessionCache()
+ {
+ return _cache.RemoveByPattern("session:123:*"); // 鍒犻櫎 session:123: 寮�澶寸殑鎵�鏈塳ey
+ }
+
+ // 鎵归噺鍒犻櫎骞惰繑鍥炴垚鍔熸暟閲�
+ public int RemoveMultiple()
+ {
+ var keys = new[] { "key1", "key2", "key3" };
+ return _cache.RemoveAll(keys); // 杩斿洖瀹為檯鍒犻櫎鐨勬暟閲�
+ }
+
+ // 鏉′欢鍒犻櫎
+ public int RemoveTempCache()
+ {
+ return _cache.RemoveWhere(key => key.Contains("temp")); // 鍒犻櫎鍖呭惈"temp"鐨刱ey
+ }
+}
+```
+
+## 3. 缂撳瓨锛圛CacheService锛�- 娣诲姞鍜屼慨鏀规墿灞曟柟娉�
+
+```csharp
+public class CacheAdvancedDemo
+{
+ private readonly ICacheService _cache;
+
+ // 鎵归噺娣诲姞
+ public void AddMultiple()
+ {
+ var items = new Dictionary<string, string>
+ {
+ { "user:1", "寮犱笁" },
+ { "user:2", "鏉庡洓" },
+ { "user:3", "鐜嬩簲" }
+ };
+ _cache.AddAll(items, 300); // 鎵归噺娣诲姞锛�300绉掕繃鏈�
+ }
+
+ // 鎵归噺娣诲姞瀵硅薄
+ public void AddMultipleObjects()
+ {
+ var items = new Dictionary<string, object>
+ {
+ { "order:1", new { Id = 1, Amount = 100 } },
+ { "order:2", new { Id = 2, Amount = 200 } }
+ };
+ _cache.AddAllObjects(items, 600);
+ }
+
+ // 鏇挎崲锛堜粎瀛樺湪鏃舵浛鎹級
+ public bool ReplaceExisting()
+ {
+ return _cache.Replace("user:1", "鏂扮敤鎴峰悕"); // key涓嶅瓨鍦ㄨ繑鍥瀎alse
+ }
+
+ // 鑾峰彇骞跺埛鏂拌繃鏈熸椂闂�
+ public string? GetAndRefresh(string key)
+ {
+ return _cache.GetAndRefresh(key, 1800); // 鑾峰彇鍊煎苟寤堕暱30鍒嗛挓
+ }
+
+ // 鍒锋柊杩囨湡鏃堕棿
+ public bool RefreshExpire(string key)
+ {
+ return _cache.RefreshExpire(key, 3600); // 鍒锋柊涓�1灏忔椂鍚庤繃鏈�
+ }
+
+ // 璁剧疆N绉掑悗杩囨湡
+ public bool SetExpireIn(string key, int seconds)
+ {
+ return _cache.ExpireIn(key, seconds);
+ }
+
+ // 璁剧疆鍦ㄦ寚瀹氭椂闂寸偣杩囨湡
+ public bool SetExpireAt(string key, DateTime expireTime)
+ {
+ return _cache.ExpireAt(key, expireTime);
+ }
+
+ // 鑾峰彇鍓╀綑杩囨湡鏃堕棿
+ public long? GetTTL(string key)
+ {
+ return _cache.GetExpire(key); // 杩斿洖鍓╀綑绉掓暟锛宯ull琛ㄧず姘镐笉杩囨湡鎴杒ey涓嶅瓨鍦�
+ }
+}
+```
+
+## 4. 缂撳瓨锛圛CacheService锛�- 鍘熷瓙鎿嶄綔鏂规硶
+
+```csharp
+public class AtomicOperationDemo
+{
+ private readonly ICacheService _cache;
+
+ // 鍘熷瓙娣诲姞锛堜粎涓嶅瓨鍦ㄦ椂娣诲姞锛�- 鍒嗗竷寮忛攣鍦烘櫙
+ public bool AcquireLock(string lockKey, string lockValue)
+ {
+ return _cache.AddIfNotExists(lockKey, lockValue, 30); // 30绉掕嚜鍔ㄨ繃鏈�
+ }
+
+ // 鑾峰彇鏃у�煎苟璁剧疆鏂板��
+ public string? GetAndSet(string key, string newValue)
+ {
+ return _cache.GetAndSet(key, newValue); // 杩斿洖鏃у�硷紝璁剧疆鏂板��
+ }
+
+ // 鑷璁℃暟鍣�
+ public long IncrementCounter(string key)
+ {
+ return _cache.Increment(key); // 鑷1锛岃繑鍥炴柊鍊�
+ }
+
+ // 鑷鎸囧畾鍊�
+ public long IncrementBy(string key, long value)
+ {
+ return _cache.Increment(key, value); // 鑷value
+ }
+
+ // 鑷噺璁℃暟鍣�
+ public long DecrementCounter(string key)
+ {
+ return _cache.Decrement(key); // 鑷噺1
+ }
+
+ // 杩藉姞鍐呭
+ public long AppendContent(string key, string content)
+ {
+ return _cache.Append(key, content); // 杩斿洖杩藉姞鍚庣殑瀛楃涓查暱搴�
+ }
+}
+```
+
+## 5. 缂撳瓨锛圛CacheService锛�- ConcurrentDictionary椋庢牸鏂规硶
+
+```csharp
+public class ConcurrentStyleDemo
+{
+ private readonly ICacheService _cache;
+
+ // 灏濊瘯娣诲姞锛堜粎涓嶅瓨鍦ㄦ椂娣诲姞锛�
+ public bool TryAdd(string key, string value)
+ {
+ return _cache.TryAdd(key, value, 60); // key瀛樺湪杩斿洖false
+ }
+
+ // 灏濊瘯鑾峰彇
+ public bool TryGet(string key, out string? value)
+ {
+ return _cache.TryGetValue(key, out value);
+ }
+
+ // 灏濊瘯绉婚櫎骞惰繑鍥炲��
+ public bool TryRemove(string key, out string? value)
+ {
+ return _cache.TryRemove(key, out value);
+ }
+
+ // 灏濊瘯鏇存柊锛堜粎瀛樺湪鏃舵洿鏂帮級
+ public bool TryUpdate(string key, string newValue)
+ {
+ return _cache.TryUpdate(key, newValue, 60);
+ }
+
+ // 鍊兼敼鍙樻椂鏇存柊锛堥伩鍏嶆棤鏁堝啓鍏ワ級
+ public bool TryUpdateIfChanged(string key, string newValue)
+ {
+ return _cache.TryUpdateIfChanged(key, newValue, 60); // 鍊肩浉鍚岃繑鍥瀎alse
+ }
+
+ // 鑾峰彇鎴栨坊鍔�
+ public string GetOrAdd(string key)
+ {
+ return _cache.GetOrAdd(key, "榛樿鍊�", 60);
+ }
+
+ // 鑾峰彇鎴栨坊鍔狅紙宸ュ巶鏂规硶锛�
+ public T GetOrAdd<T>(string key, Func<string, T> factory) where T : class
+ {
+ return _cache.GetOrAdd(key, factory, 60);
+ }
+}
+```
+
+## 6. 鍒嗗竷寮忛攣锛圛DistributedLockService锛�
```csharp
public class OrderService
@@ -80,7 +284,7 @@
}
```
-## 3. 璁℃暟鍣紙ICounterService锛�
+## 7. 璁℃暟鍣紙ICounterService锛�
```csharp
public class StatisticsService
@@ -107,7 +311,7 @@
}
```
-## 4. 鍙戝竷/璁㈤槄锛圛MessageQueueService锛�
+## 8. 鍙戝竷/璁㈤槄锛圛MessageQueueService锛�
```csharp
public class NotificationService
@@ -143,7 +347,7 @@
}
```
-## 5. 闄愭祦锛圛RateLimitingService锛�
+## 9. 闄愭祦锛圛RateLimitingService锛�
```csharp
public class ApiController
@@ -175,7 +379,7 @@
}
```
-## 6. 鍒嗗竷寮廔D鐢熸垚鍣紙IDistributedIdGenerator锛�
+## 10. 鍒嗗竷寮廔D鐢熸垚鍣紙IDistributedIdGenerator锛�
```csharp
public class TaskService
@@ -203,7 +407,7 @@
}
```
-## 7. 鎺掕姒滐紙ILeaderboardService锛�
+## 11. 鎺掕姒滐紙ILeaderboardService锛�
```csharp
public class LeaderboardDemo
@@ -231,7 +435,7 @@
}
```
-## 8. 瀵硅薄瀛樺偍锛圛ObjectStorageService锛�
+## 12. 瀵硅薄瀛樺偍锛圛ObjectStorageService锛�
```csharp
public class DeviceService
@@ -259,7 +463,7 @@
}
```
-## 9. 閰嶇疆涓績锛圛ConfigurationCenterService锛�
+## 13. 閰嶇疆涓績锛圛ConfigurationCenterService锛�
```csharp
public class ConfigDemo
@@ -290,7 +494,7 @@
}
```
-## 10. 鐩戞帶锛圛RedisMonitorService锛�
+## 14. 鐩戞帶锛圛RedisMonitorService锛�
```csharp
public class MonitorDemo
@@ -318,7 +522,7 @@
}
```
-## 11. Session瀛樺偍锛圛SessionStorage锛�
+## 15. Session瀛樺偍锛圛SessionStorage锛�
```csharp
public class SessionDemo
@@ -350,7 +554,7 @@
}
```
-## 12. 甯冮殕杩囨护鍣紙IBloomFilterService锛�
+## 16. 甯冮殕杩囨护鍣紙IBloomFilterService锛�
```csharp
public class BloomFilterDemo
@@ -378,3 +582,45 @@
}
}
```
+
+## 17. 缂撳瓨鑷姩鍚屾锛圕acheSyncBackgroundService锛�
+
+Redis鍒板唴瀛樼紦瀛樼殑鑷姩鍚屾鍚庡彴鏈嶅姟锛岃В鍐矻1+L2娣峰悎缂撳瓨涓閮ㄤ慨鏀筊edis鏁版嵁瀵艰嚧鍐呭瓨缂撳瓨涓嶄竴鑷寸殑闂銆�
+
+### 閰嶇疆璇存槑
+
+鍦� `appsettings.json` 涓厤缃細
+
+```json
+{
+ "RedisConfig": {
+ "EnableL1Cache": true, // 鍚敤L1鍐呭瓨缂撳瓨
+ "EnableAutoSync": true, // 鍚敤鑷姩鍚屾
+ "SyncIntervalSeconds": 30, // 鍚屾闂撮殧锛�30绉�
+ "SyncBatchSize": 1000 // 鍗曟鎵归噺鑾峰彇key鏁伴噺涓婇檺
+ }
+}
+```
+
+### 宸ヤ綔鍘熺悊
+
+1. **鍚姩鏃跺叏閲忓悓姝�**锛氭湇鍔″惎鍔ㄥ悗绔嬪嵆鎵ц涓�娆″叏閲忓悓姝�
+2. **瀹氭湡澧為噺鍚屾**锛氭寜鐓ч厤缃殑闂撮殧锛堥粯璁�30绉掞級瀹氭湡鎵ц鍚屾
+3. **鍙屽悜鍚屾**锛�
+ - 灏哛edis涓殑鏁版嵁鏇存柊鍒板唴瀛樼紦瀛�
+ - 娓呯悊鍐呭瓨缂撳瓨涓笉瀛樺湪浜嶳edis鐨刱ey
+4. **鏅鸿兘TTL鍚屾**锛氬悓姝ユ椂浼氫繚鐣橰edis涓缃殑杩囨湡鏃堕棿
+
+### 浣跨敤鍦烘櫙
+
+- **澶氱郴缁熷叡浜玆edis**锛氬綋澶氫釜WCS瀹炰緥鍏变韩鍚屼竴涓猂edis锛屼竴涓疄渚嬩慨鏀规暟鎹悗锛屽叾浠栧疄渚嬭兘鑷姩鍚屾
+- **澶栭儴淇敼Redis**锛氶�氳繃Redis CLI鎴栧叾浠栧伐鍏蜂慨鏀规暟鎹悗锛屽簲鐢ㄨ兘鑷姩鑾峰彇鏈�鏂板��
+- **缂撳瓨涓�鑷存�т繚闅�**锛氶伩鍏嶅惎鐢↙1缂撳瓨鍚庯紝鍐呭瓨缂撳瓨鍜孯edis鏁版嵁涓嶄竴鑷寸殑闂
+
+### 娉ㄦ剰浜嬮」
+
+- 浠呭湪 `EnableL1Cache = true` 涓� `EnableAutoSync = true` 鏃惰繍琛�
+- IMemoryCache涓嶆敮鎸佹灇涓撅紝鍥犳鍙兘娓呯悊宸茶窡韪殑key
+- 鍚屾闂撮殧寤鸿璁剧疆涓�30-60绉掞紝杩囩煭浼氬奖鍝嶆�ц兘
+- 瀵逛簬瑕佹眰寮轰竴鑷存�х殑鍦烘櫙锛屽缓璁洿鎺ョ鐢↙1缂撳瓨锛坄EnableL1Cache: false`锛�
+```
--
Gitblit v1.9.3