From 191f08b7a74c461d1914652aed737888967a69f1 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 19 十月 2023 16:51:38 +0800
Subject: [PATCH] #地图增加分布式锁

---
 src/main/java/com/zy/common/utils/RedisUtil.java       |   23 +++++++++++
 src/main/java/com/zy/common/utils/NavigateMapData.java |   69 ++++++++++++++++++++--------------
 2 files changed, 63 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateMapData.java b/src/main/java/com/zy/common/utils/NavigateMapData.java
index 9d550f2..786fc6b 100644
--- a/src/main/java/com/zy/common/utils/NavigateMapData.java
+++ b/src/main/java/com/zy/common/utils/NavigateMapData.java
@@ -241,40 +241,51 @@
      */
     public boolean writeNavigateNodeToRedisMap(List<NavigateNode> nodes, boolean lock) {
         RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
-        Object o = redisUtil.get("realtimeBasMap_" + lev);
-        if (o == null) {
-            return false;
-        }
-
-        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
-        ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class);
-        List<List<MapNode>> lists = filterMap(NavigationMapType.NONE.id, arrayList, lev, null, null);//鑾峰彇鍏ㄩ儴鍦板浘鏁版嵁
-
-        NavigateMapData mapData = new NavigateMapData(nodes.get(0).getZ());
-        List<List<MapNode>> realMap = mapData.getJsonData(-1, null, null);//鑾峰彇瀹屾暣鍦板浘(鍖呮嫭鍏ュ簱鍑哄簱)
-        for (NavigateNode node : nodes) {
-            if (node.getZ() != lev) {
-                continue;
+        try {
+            if (!redisUtil.tryLock("realtimeBasMap_" + lev)) {
+                return false;//鍔犻攣澶辫触
             }
 
-            List<MapNode> listX = lists.get(node.getX());
-            MapNode mapNode = listX.get(node.getY());
-            if (lock) {
-                mapNode.setValue(-999);//绂佺敤搴撲綅
-            }else {
-                //鑾峰彇鍘熷鑺傜偣鏁版嵁
-                List<MapNode> rows = realMap.get(node.getX());
-                MapNode col = rows.get(node.getY());
-                mapNode.setValue(col.getValue());//鎭㈠搴撲綅
+            Object o = redisUtil.get("realtimeBasMap_" + lev);
+            if (o == null) {
+                return false;
             }
 
-            listX.set(node.getY(), mapNode);
-            lists.set(node.getX(), listX);
+            BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
+            ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class);
+            List<List<MapNode>> lists = filterMap(NavigationMapType.NONE.id, arrayList, lev, null, null);//鑾峰彇鍏ㄩ儴鍦板浘鏁版嵁
+
+            NavigateMapData mapData = new NavigateMapData(nodes.get(0).getZ());
+            List<List<MapNode>> realMap = mapData.getJsonData(-1, null, null);//鑾峰彇瀹屾暣鍦板浘(鍖呮嫭鍏ュ簱鍑哄簱)
+            for (NavigateNode node : nodes) {
+                if (node.getZ() != lev) {
+                    continue;
+                }
+
+                List<MapNode> listX = lists.get(node.getX());
+                MapNode mapNode = listX.get(node.getY());
+                if (lock) {
+                    mapNode.setValue(-999);//绂佺敤搴撲綅
+                } else {
+                    //鑾峰彇鍘熷鑺傜偣鏁版嵁
+                    List<MapNode> rows = realMap.get(node.getX());
+                    MapNode col = rows.get(node.getY());
+                    mapNode.setValue(col.getValue());//鎭㈠搴撲綅
+                }
+
+                listX.set(node.getY(), mapNode);
+                lists.set(node.getX(), listX);
+            }
+            basMap.setData(JSON.toJSONString(lists));
+            basMap.setUpdateTime(new Date());
+            //灏嗘暟鎹簱鍦板浘鏁版嵁瀛樺叆redis
+            redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }finally {
+            //瑙i攣
+            redisUtil.unlock("realtimeBasMap_" + lev);
         }
-        basMap.setData(JSON.toJSONString(lists));
-        basMap.setUpdateTime(new Date());
-        //灏嗘暟鎹簱鍦板浘鏁版嵁瀛樺叆redis
-        redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap));
         return true;
     }
 
diff --git a/src/main/java/com/zy/common/utils/RedisUtil.java b/src/main/java/com/zy/common/utils/RedisUtil.java
index 95da6dc..33a9694 100644
--- a/src/main/java/com/zy/common/utils/RedisUtil.java
+++ b/src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,5 +1,6 @@
 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;
@@ -25,11 +26,33 @@
     @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);
+    }
+
+    /**
      * 鎸囧畾缂撳瓨澶辨晥鏃堕棿
      *
      * @param key  閿�

--
Gitblit v1.9.1