|
@@ -0,0 +1,34 @@
|
|
|
+//package com.huimv.receive.config;
|
|
|
+//
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//import java.util.concurrent.TimeUnit;
|
|
|
+//
|
|
|
+//@Component
|
|
|
+//public class DistributedLockManager {
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private RedisTemplate<String, String> redisTemplate;
|
|
|
+//
|
|
|
+// public boolean acquireLock(String lockKey, String lockValue, long expireTimeInSeconds) {
|
|
|
+// // 使用 SETNX 命令尝试获取锁
|
|
|
+// Boolean isLockAcquired = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue);
|
|
|
+//
|
|
|
+// if (isLockAcquired != null && isLockAcquired) {
|
|
|
+// // 设置锁的过期时间,以防止锁被长时间占用
|
|
|
+// redisTemplate.expire(lockKey, expireTimeInSeconds, TimeUnit.SECONDS);
|
|
|
+// return true; // 获取锁成功
|
|
|
+// } else {
|
|
|
+// return false; // 获取锁失败
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public void releaseLock(String lockKey, String lockValue) {
|
|
|
+// String storedLockValue = redisTemplate.opsForValue().get(lockKey);
|
|
|
+// if (lockValue.equals(storedLockValue)) {
|
|
|
+// // 如果锁的值匹配,释放锁
|
|
|
+// redisTemplate.delete(lockKey);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//}
|