From 42f3380a98fff04674cf4ef95ea5e99f5c54f1fd Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 十二月 2024 12:07:36 +0800
Subject: [PATCH] #地图文件bug尝试修复

---
 src/main/java/com/zy/common/utils/RedisUtil.java |   63 +++++++++++++++++++++++++++++--
 1 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/RedisUtil.java b/src/main/java/com/zy/common/utils/RedisUtil.java
index b2df114..95ab170 100644
--- a/src/main/java/com/zy/common/utils/RedisUtil.java
+++ b/src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,9 +1,13 @@
 package com.zy.common.utils;
 
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -13,14 +17,39 @@
  * redisTemplate灏佽
  *
  */
-//@Component
+@Component
 public class RedisUtil {
 
-    @Autowired
-    private RedisTemplate<String, Object> redisTemplate;
+//    @Autowired
+//    private RedisTemplate<String, Object> redisTemplate;
 
-    public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Autowired
+    private StringRedisTemplate stringRedisTemplate;
+
+    private static final String LOCK_PREFIX = "lock:";
+    private static final long DEFAULT_EXPIRE_TIME = 10; // 榛樿閿佽繃鏈熸椂闂达紝鍗曚綅涓哄垎閽�
+
+    public RedisUtil(RedisTemplate redisTemplate) {
         this.redisTemplate = redisTemplate;
+    }
+
+    /**
+     * Redis鍔犻攣鐨勬搷浣�
+     */
+    public Boolean tryLock(String key) {
+        String redisKey = LOCK_PREFIX + key;
+        return redisTemplate.opsForValue().setIfAbsent(redisKey, "locked", DEFAULT_EXPIRE_TIME, TimeUnit.MINUTES);
+    }
+
+    /**
+     * Redis瑙i攣鐨勬搷浣�
+     */
+    public void unlock(String lockKey) {
+        String redisKey = LOCK_PREFIX + lockKey;
+        redisTemplate.delete(redisKey);
     }
 
     /**
@@ -96,6 +125,21 @@
     }
 
     /**
+     * 鑾峰彇鍏ㄩ儴鏁版嵁
+     * @return
+     */
+    public HashMap<Object, Object> getRedis() {
+        Set<String> keys = redisTemplate.keys("*");
+        HashMap<Object, Object> map = new HashMap<>();
+        for (String key : keys) {
+            Object value = redisTemplate.opsForValue().get(key);
+            map.put(key, value);
+        }
+
+        return map;//杩斿洖鍏ㄩ儴鏁版嵁闆嗗悎
+    }
+
+    /**
      * 鏅�氱紦瀛樻斁鍏�
      *
      * @param key   閿�
@@ -105,6 +149,17 @@
     public boolean set(String key, Object value) {
         try {
             redisTemplate.opsForValue().set(key, value);
+            long start = System.currentTimeMillis();
+            while (System.currentTimeMillis() - start < 10000) {//鏈夋晥鏈�10s
+                Object o = redisTemplate.opsForValue().get(key);
+                if (o == null) {
+                    continue;
+                }
+
+                if (o.equals(value)) {
+                    break;
+                }
+            }
             return true;
         } catch (Exception e) {
             e.printStackTrace();

--
Gitblit v1.9.1