From 9a56c5710eb281afc06e2f3a211b8b595f24bbe1 Mon Sep 17 00:00:00 2001
From: liujie <liujie>
Date: 星期三, 16 八月 2023 09:39:34 +0800
Subject: [PATCH] update
---
management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java | 265 ++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 204 insertions(+), 61 deletions(-)
diff --git a/management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java b/management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java
index ce51f7f..589657b 100644
--- a/management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java
+++ b/management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/RedisUtil.java
@@ -1,21 +1,15 @@
package com.stylefeng.guns.modular.system.util;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.RestTemplate;
+import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
+import redis.clients.jedis.Pipeline;
-import java.util.HashMap;
-import java.util.Map;
+import java.io.IOException;
+import java.util.*;
/**
@@ -24,6 +18,10 @@
@Component
public class RedisUtil {
+ @Autowired
+ private JedisPool jedisPool;
+
+ private Timer timer;
/**
@@ -32,18 +30,11 @@
* @param value
*/
public void setStrValue(String key, String value){
- if(ToolUtil.isNotEmpty(key)){
- //将请求头部和参数合成一个请求
- Map<String,String> map = new HashMap<>();
- map.put("key", key);
- map.put("value", value);
- String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue_", map);
- JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
- if(jsonObject.getIntValue("code") != 200){
- System.err.println("调用redis出错了");
- }
+ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
+ Jedis resource = jedisPool.getResource();
+ String set = resource.set(key, value);
+ closeJedis(resource);
}
-
}
@@ -54,16 +45,10 @@
* @param time 秒
*/
public void setStrValue(String key, String value, int time){
- if(ToolUtil.isNotEmpty(key)){
- Map<String,String> map = new HashMap<>();
- map.put("key", key);
- map.put("value", value);
- map.put("time", String.valueOf(time));
- String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue", map);
- JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
- if(jsonObject.getIntValue("code") != 200){
- System.err.println("调用redis出错了");
- }
+ if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){
+ Jedis resource = jedisPool.getResource();
+ String setex = resource.setex(key, time, value);
+ closeJedis(resource);
}
}
@@ -75,35 +60,43 @@
*/
public String getValue(String key){
if(ToolUtil.isNotEmpty(key)){
- Map<String,String> map = new HashMap<>();
- map.put("key", key);
- String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map);
- JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
- if(jsonObject.getIntValue("code") != 200){
- System.err.println("调用redis出错了");
- }
- return jsonObject.getString("data");
+ Jedis resource = jedisPool.getResource();
+ String data = resource.get(key);
+ closeJedis(resource);
+ return data;
}
return null;
}
-
/**
- * 从redis中获取值
- * @param key
+ * 批量获取
+ * @param kes
* @return
*/
- public String getValues(String key){
- if(ToolUtil.isNotEmpty(key)){
- Map<String,String> map = new HashMap<>();
- map.put("keys", key);
- String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValues", map);
- JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
- if(jsonObject.getIntValue("code") != 200){
- System.err.println("调用redis出错了");
+ 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);
}
- return jsonObject.getString("data");
+ 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;
}
@@ -113,17 +106,167 @@
* 删除key
* @param key
*/
- public String remove(String key){
+ public void remove(String key){
if(ToolUtil.isNotEmpty(key)){
- Map<String,String> map = new HashMap<>();
- map.put("key", key);
- String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map);
- JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
- if(jsonObject.getIntValue("code") != 200){
- System.err.println("调用redis出错了");
- }
- return jsonObject.getString("data");
+ Jedis resource = jedisPool.getResource();
+ Long del = resource.del(key);
+ closeJedis(resource);
}
- return null;
+ }
+
+
+ /**
+ * 向集合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;
+ }
+
+
+ /**
+ * 删除Set集合中的值
+ * @param key
+ * @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();
+ }
+ }
+
+
+ /**
+ * 删除资源
+ * @param jedis
+ */
+ public void closeJedis(Jedis jedis){
+ if(null != jedis){
+ jedis.close();
+ }
+ }
+
+
+ /**
+ * redis加锁
+ * @param key
+ * @param value
+ * @param time
+ * @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;
+ }
+
+ /**
+ * 获取redis锁
+ * @return
+ */
+ public boolean lock(){
+ try {
+ boolean b = lock(5);
+ if(!b){
+ int num1 = 1;
+ while (num1 <= 10){
+ try {
+ Thread.sleep(3000);//等待3秒
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ b = lock(5);
+ if(b){
+ return true;
+ }else{
+ num1++;
+ }
+ }
+ return false;
+ }
+ return b;
+ }catch (Exception e){
+ e.printStackTrace();
+ unlock();
+ }
+ return false;
+ }
+
+
+ /**
+ * 获取redis锁
+ * @param time
+ * @return
+ */
+ public boolean lock(int time){
+ String uuid = UUID.randomUUID().toString();
+ return lock("redis", uuid, time);
+ }
+
+
+ /**
+ * redis释放锁
+ * @param key
+ * @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
+ */
+ public boolean unlock(){
+ return unlock("redis");
}
}
--
Gitblit v1.7.1