|
@@ -1,5 +1,7 @@
|
|
|
package com.fdkankan.redis.util;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fdkankan.scene.httpclient.CustomHttpClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -7,9 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class RedisClient {
|
|
@@ -18,6 +18,16 @@ public class RedisClient {
|
|
|
|
|
|
public final static String url_str_get = "/redis/string/get/";
|
|
|
|
|
|
+ public final static String url_set_add = "/redis/set/add";
|
|
|
+
|
|
|
+ public final static String url_hash_hscan = "/redis/hash/hscan";
|
|
|
+
|
|
|
+ public final static String url_hash_add = "/redis/hash/add";
|
|
|
+
|
|
|
+ public final static String url_hash_hvals = "/redis/hash/hvals";
|
|
|
+
|
|
|
+ public final static String url_hash_hdel = "/redis/hash/hdel";
|
|
|
+
|
|
|
@Value("${redis.host}")
|
|
|
private String host;
|
|
|
|
|
@@ -30,7 +40,7 @@ public class RedisClient {
|
|
|
@Resource
|
|
|
private CustomHttpClient customHttpClient;
|
|
|
|
|
|
- public void add(String key, String value){
|
|
|
+ public void add(String key, String value){
|
|
|
String url = host + url_str_add;
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
params.put("key", this.genKey(key));
|
|
@@ -39,7 +49,7 @@ public class RedisClient {
|
|
|
if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
throw new RuntimeException("redis add string error");
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
public String get(String key){
|
|
|
String url = host + url_str_get;
|
|
@@ -52,6 +62,74 @@ public class RedisClient {
|
|
|
return jsonObject.getString("data");
|
|
|
}
|
|
|
|
|
|
+ public void sSet(String key, Set<String> values){
|
|
|
+ String url = host + url_set_add;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("key", this.genKey(key));
|
|
|
+ params.put("values", JSON.toJSONString(values));
|
|
|
+ JSONObject jsonObject = customHttpClient.postJson(url, params);
|
|
|
+ if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
+ throw new RuntimeException("redis add set error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void hmset(String key, Map<String, String> values){
|
|
|
+ String url = host + url_hash_add;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("key", this.genKey(key));
|
|
|
+ params.put("values", values);
|
|
|
+ JSONObject jsonObject = customHttpClient.postJson(url, params);
|
|
|
+ if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
+ throw new RuntimeException("redis hash add error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Map<String, String> hmget(String key){
|
|
|
+ String url = host + url_hash_hscan;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("key", this.genKey(key));
|
|
|
+ JSONObject jsonObject = customHttpClient.postJson(url, params);
|
|
|
+ if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
+ throw new RuntimeException("redis hash hscan error");
|
|
|
+ }
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ // 转换为Map<String, String>
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ for (String k : data.keySet()) {
|
|
|
+ map.put(key, jsonObject.getString(key));
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> hMultiGet(String key){
|
|
|
+ String url = host + url_hash_hvals;
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("key", this.genKey(key));
|
|
|
+ JSONObject jsonObject = customHttpClient.postJson(url, params);
|
|
|
+ if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
+ throw new RuntimeException("redis hash get hvals error");
|
|
|
+ }
|
|
|
+ List<String> data = jsonObject.getJSONArray("data").toJavaList(String.class);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void hdel(String key, List<String> fields){
|
|
|
+ if(CollUtil.isEmpty(fields)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fields.stream().forEach(field->{
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("key", this.genKey(key));
|
|
|
+ params.put("field", field);
|
|
|
+ String url = host + url_hash_hdel;
|
|
|
+ JSONObject jsonObject = customHttpClient.postJson(url, params);
|
|
|
+ if(Objects.isNull(jsonObject) || !"0".equals(jsonObject.getString("status"))){
|
|
|
+ throw new RuntimeException("redis hash hdel error");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
private String genKey(String key){
|
|
|
return sysCode + "_" + serverName + "_" + key;
|
|
|
}
|