From ba6b508a44cb1f0730c6a27a5d73b8d2ae8f1d4b Mon Sep 17 00:00:00 2001 From: puzhibing <393733352@qq.com> Date: 星期二, 19 八月 2025 18:43:47 +0800 Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/QYTDriving --- user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/RedisUtil.java | 196 ++++++++++++++---------------------------------- 1 files changed, 57 insertions(+), 139 deletions(-) diff --git a/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/RedisUtil.java b/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/RedisUtil.java index 2e7a476..03f7892 100644 --- a/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/RedisUtil.java +++ b/user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/RedisUtil.java @@ -1,15 +1,11 @@ package com.supersavedriving.user.modular.system.util; -import com.supersavedriving.user.core.util.ToolUtil; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.Pipeline; -import java.io.IOException; -import java.util.*; +import javax.annotation.Resource; +import java.util.UUID; +import java.util.concurrent.TimeUnit; /** @@ -18,10 +14,9 @@ @Component public class RedisUtil { - @Autowired - private JedisPool jedisPool; + @Resource + private RedisTemplate<String, Object> redisTemplate; - private Timer timer; /** @@ -30,11 +25,7 @@ * @param value */ public void setStrValue(String key, String value){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value); - closeJedis(resource); - } + redisTemplate.opsForValue().set(key, value); } @@ -45,11 +36,7 @@ * @param time 秒 */ public void setStrValue(String key, String value, int time){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ - Jedis resource = jedisPool.getResource(); - String setex = resource.setex(key, time, value); - closeJedis(resource); - } + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } @@ -59,47 +46,9 @@ * @return */ public String getValue(String key){ - if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - closeJedis(resource); - return data; - } - return null; + return (String) redisTemplate.opsForValue().get(key); } - - /** - * 批量获取 - * @param kes - * @return - */ - public List<Object> getValues(List<String> kes){ - if(null != kes){ - Jedis resource = jedisPool.getResource(); - Pipeline pipelined = resource.pipelined(); - for(String key : kes){ - pipelined.get(key); - } - List<Object> list = pipelined.syncAndReturnAll(); - - closeJedis(resource); - pipelined.clear(); - try { - pipelined.close(); - } catch (IOException e) { - e.printStackTrace(); - } - List<Object> data = new ArrayList<>(); - for(Object o : list){ - if(null != o){ - data.add(o); - } - } - return data; - } - return null; - } /** @@ -107,42 +56,10 @@ * @param key */ public void remove(String key){ - if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long del = resource.del(key); - closeJedis(resource); - } + redisTemplate.delete(key); } - /** - * 向集合key添加数据 - * @param key - * @param members - */ - public void addSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.sadd(key, members); - resource.close(); - } - } - - - /** - * 返回Set集合数据 - * @param key - * @return - */ - public Set<String> getSetAllValue(String key){ - Set<String> smembers = new HashSet<>(); - if(ToolUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - smembers = resource.smembers(key); - resource.close(); - } - return smembers; - } /** @@ -151,23 +68,9 @@ * @param members */ public void delSetValue(String key, String...members){ - if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(members)){ - Jedis resource = jedisPool.getResource(); - Long sadd = resource.srem(key, members); - resource.close(); - } + redisTemplate.opsForSet().remove(key, members); } - - /** - * 删除资源 - * @param jedis - */ - public void closeJedis(Jedis jedis){ - if(null != jedis){ - jedis.close(); - } - } /** @@ -178,27 +81,8 @@ * @return */ public boolean lock(String key, String value, int time){ - if(!StringUtils.isEmpty(key)){ - key += "_lock"; - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value, "nx", "ex", time); - if("OK".equals(set)){ - String finalKey = key; - timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - System.err.println("定时任务启动"); - Jedis resource = jedisPool.getResource(); - resource.setex(finalKey, time, value); - resource.close(); - } - }, 1000, 500); - } - resource.close(); - return "OK".equals(set) ? true : false; - } - return false; + key += "_lock"; + return redisTemplate.opsForValue().setIfAbsent(key, value); } /** @@ -212,7 +96,37 @@ int num1 = 1; while (num1 <= 10){ try { - Thread.sleep(3000);//等待3秒 + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + b = lock(5); + if(b){ + return true; + }else{ + num1++; + } + } + return false; + } + return b; + }catch (Exception e){ + e.printStackTrace(); + }finally { + unlock(); + } + return false; + } + + + public boolean lock(String key){ + try { + boolean b = lock(key,5); + if(!b){ + int num1 = 1; + while (num1 <= 10){ + try { + Thread.sleep(1000);//等待1秒 } catch (InterruptedException e) { e.printStackTrace(); } @@ -239,6 +153,18 @@ * @param time * @return */ + public boolean lock(String key, int time){ + String uuid = UUID.randomUUID().toString(); + return lock(key, uuid, time); + } + + + + /** + * 获取redis锁 + * @param time + * @return + */ public boolean lock(int time){ String uuid = UUID.randomUUID().toString(); return lock("redis", uuid, time); @@ -251,15 +177,7 @@ * @return */ public boolean unlock(String key){ - if(!StringUtils.isEmpty(key)){ - key += "_lock"; - Jedis resource = jedisPool.getResource(); - timer.cancel();//取消定时任务 - Long del = resource.del(key); - resource.close(); - return del != 0 ? true : false; - } - return false; + return redisTemplate.delete(key); } /** -- Gitblit v1.7.1