From c3b7673c16d026e57f8759b4cee99a42bf3c57f2 Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期三, 24 九月 2025 09:15:44 +0800 Subject: [PATCH] 修改bug --- UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java | 142 +++++++++++++++++++++++++++++++++-------------- 1 files changed, 100 insertions(+), 42 deletions(-) diff --git a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java index 2e15fec..3f1b4ef 100644 --- a/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java +++ b/UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java @@ -1,11 +1,22 @@ package com.stylefeng.guns.modular.system.util; import com.stylefeng.guns.core.util.ToolUtil; +import org.apache.tomcat.util.threads.ThreadPoolExecutor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.geo.Circle; +import org.springframework.data.geo.GeoResult; +import org.springframework.data.geo.GeoResults; +import org.springframework.data.geo.Point; +import org.springframework.data.redis.connection.RedisGeoCommands; +import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; + +import javax.annotation.PostConstruct; import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -17,9 +28,9 @@ @Autowired private RedisTemplate redisTemplate; - - - + + private final Map<String, ScheduledExecutorService> LOCK_TASK_MAP = new HashMap<>(); + /** * 向redis中存储字符串没有过期时间 * @param key @@ -72,63 +83,110 @@ redisTemplate.delete(key); } } - - - - - - - + + /** - * redis加锁 + * 加锁 * @param key - * @param value * @param time * @return */ - public boolean lock(String key, String value, int time){ - if(!StringUtils.isEmpty(key)){ - key += "_lock"; - return redisTemplate.opsForValue().setIfAbsent(key, value); + public boolean lock(String key, long keepTime, long time){ + ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); + if(null == scheduledExecutorService || scheduledExecutorService.isTerminated()){ + unlock(key); + } + keepTime *= 1000; + Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); + if(ifAbsent){ + return addLockMap(key, time); + } + long timeMillis = System.currentTimeMillis(); + while (timeMillis + keepTime > System.currentTimeMillis()){ + ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); + if(ifAbsent){ + return addLockMap(key, time); + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } } return false; } - - /** - * 获取redis锁 - * @param time - * @return - */ - public boolean lock(int time){ - String uuid = UUID.randomUUID().toString(); - return lock("redis", uuid, time); + + + private boolean addLockMap(String key, long time){ + ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); + if(null == scheduledExecutorService){ + scheduledExecutorService = Executors.newScheduledThreadPool(1); + } + LOCK_TASK_MAP.put(key, scheduledExecutorService); + scheduledExecutorService.schedule(()->{ + redisTemplate.delete(key); + LOCK_TASK_MAP.remove(key); + }, time, TimeUnit.SECONDS); + return true; } - - - public boolean lock(String key, int time){ - String uuid = UUID.randomUUID().toString(); - return lock(key, uuid, time); - } - + /** * redis释放锁 - * @param key * @return */ public boolean unlock(String key){ - if(!StringUtils.isEmpty(key)){ - key += "_lock"; - return redisTemplate.delete(key); + redisTemplate.delete(key); + ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); + if(null != scheduledExecutorService){ + scheduledExecutorService.shutdownNow(); } - return false; + LOCK_TASK_MAP.remove(key); + return true; } - + + /** - * 删除锁 + * 检索坐标为中心直径范围内的数据,单位米 + * @param k + * @param lon + * @param lat + * @param radius * @return */ - public boolean unlock(){ - return unlock("redis"); + public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> getNearGeo(String k, Double lon, Double lat, Double radius){ + Circle within = new Circle(lon, lat, radius); + GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius(k, within); + return geoResults.getContent(); + } + + + /** + * 检索坐标为中心直径范围内的数据,单位米,由近到远排序 + * @param k + * @param lon + * @param lat + * @param radius + * @return + */ + public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> getNearGeoSortAscending(String k, Double lon, Double lat, Double radius){ + Circle within = new Circle(lon, lat, radius); + RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs(); + geoRadiusCommandArgs.sortAscending(); + GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius(k, within, geoRadiusCommandArgs); + return geoResults.getContent(); + } + + + /** + * 添加地理空间索引坐标 + * @param k + * @param lon + * @param lat + * @param object + */ + public void addGeo(String k, Double lon, Double lat, String object){ + Point point = new Point(lon, lat); + redisTemplate.opsForGeo().add(k, point, object); } } -- Gitblit v1.7.1