From 53e7558400dcacecdce70e39ebfe1727740f9296 Mon Sep 17 00:00:00 2001 From: puzhibing <393733352@qq.com> Date: 星期六, 25 十一月 2023 17:20:06 +0800 Subject: [PATCH] 重写课包支付和排课逻辑 --- cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java | 64 +++++++++++++++++-------------- 1 files changed, 35 insertions(+), 29 deletions(-) diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java index 88be661..d5f9ab2 100644 --- a/cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java +++ b/cloud-server-other/src/main/java/com/dsh/other/util/RedisUtil.java @@ -27,11 +27,12 @@ /** * 向redis中存储字符串没有过期时间 * Storing strings in Redis without an expiration time. + * * @param key * @param value */ - public void setStrValue(String key, String value){ - if(StringUtil.isNotEmpty(key)){ + public void setStrValue(String key, String value) { + if (StringUtil.isNotEmpty(key)) { Jedis resource = jedisPool.getResource(); String set = resource.set(key, value); closeJedis(resource); @@ -42,12 +43,13 @@ /** * 以分钟为单位设置存储值(设置过期时间) * Set storage value in minutes (set expiration time) as units. + * * @param key * @param value - * @param time 秒 + * @param time 秒 */ - public void setStrValue(String key, String value, int time){ - if(StringUtil.isNotEmpty(key)){ + public void setStrValue(String key, String value, int time) { + if (StringUtil.isNotEmpty(key)) { Jedis resource = jedisPool.getResource(); String setex = resource.setex(key, time, value); closeJedis(resource); @@ -57,11 +59,12 @@ /** * 从redis中获取值 + * * @param key * @return */ - public String getValue(String key){ - if(StringUtil.isNotEmpty(key)){ + public String getValue(String key) { + if (StringUtil.isNotEmpty(key)) { Jedis resource = jedisPool.getResource(); String data = resource.get(key); closeJedis(resource); @@ -73,14 +76,15 @@ /** * 批量获取 + * * @param kes * @return */ - public List<Object> getValues(List<String> kes){ - if(null != kes){ + public List<Object> getValues(List<String> kes) { + if (null != kes) { Jedis resource = jedisPool.getResource(); Pipeline pipelined = resource.pipelined(); - for(String key : kes){ + for (String key : kes) { pipelined.get(key); } List<Object> list = pipelined.syncAndReturnAll(); @@ -93,8 +97,8 @@ e.printStackTrace(); } List<Object> data = new ArrayList<>(); - for(Object o : list){ - if(null != o){ + for (Object o : list) { + if (null != o) { data.add(o); } } @@ -106,10 +110,11 @@ /** * 删除key + * * @param key */ - public void remove(String key){ - if(StringUtil.isNotEmpty(key)){ + public void remove(String key) { + if (StringUtil.isNotEmpty(key)) { Jedis resource = jedisPool.getResource(); Long del = resource.del(key); closeJedis(resource); @@ -119,16 +124,17 @@ /** * 删除资源 + * * @param jedis */ - public void closeJedis(Jedis jedis){ - if(null != jedis){ + public void closeJedis(Jedis jedis) { + if (null != jedis) { jedis.close(); } } - public boolean acquireLock(String key,String value) { + public boolean acquireLock(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); @@ -136,19 +142,19 @@ String[] split = key.split(";"); for (String s : split) { - while (true) { - // 尝试获取锁 - String result = jedis.set(s, value, "NX", "PX", TIMEOUT); - if ("OK".equals(result)) { - return true; + while (true) { + // 尝试获取锁 + String result = jedis.set(s, value, "NX", "PX", TIMEOUT); + if ("OK".equals(result)) { + return true; + } + // 超时则返回失败 + if (System.currentTimeMillis() - start > TIMEOUT) { + return false; + } + // 休眠一段时间后重试 + Thread.sleep(SLEEP_TIME); } - // 超时则返回失败 - if (System.currentTimeMillis() - start > TIMEOUT) { - return false; - } - // 休眠一段时间后重试 - Thread.sleep(SLEEP_TIME); - } } } catch (Exception e) { -- Gitblit v1.7.1