From b02ace7aa9a5e046d533cf80afa8ec14c40fe133 Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期四, 24 七月 2025 17:56:00 +0800
Subject: [PATCH] 直流电阻:解析mqtt订阅的数据保存到redis
---
cnas-device/src/main/java/com/ruoyi/device/service/impl/CollectBridgeServiceImpl.java | 25 +
cnas-device/src/main/java/com/ruoyi/device/constant/DCResistanceMqttConstants.java | 14 +
ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java | 631 ++++++++++++++++++++++++++++++++++++++++++++++++
cnas-device/src/main/java/com/ruoyi/device/vo/DCResistanceMqttVO.java | 38 ++
cnas-device/src/main/java/com/ruoyi/device/service/CollectBridgeService.java | 2
cnas-device/src/main/java/com/ruoyi/device/mqtt/MQCallback.java | 15 +
ruoyi-admin-ztns/src/main/resources/application-druid.yml | 15
7 files changed, 731 insertions(+), 9 deletions(-)
diff --git a/cnas-device/src/main/java/com/ruoyi/device/constant/DCResistanceMqttConstants.java b/cnas-device/src/main/java/com/ruoyi/device/constant/DCResistanceMqttConstants.java
new file mode 100644
index 0000000..421baf0
--- /dev/null
+++ b/cnas-device/src/main/java/com/ruoyi/device/constant/DCResistanceMqttConstants.java
@@ -0,0 +1,14 @@
+package com.ruoyi.device.constant;
+
+/**
+ * 鐩存祦鐢甸樆mqtt娣戝悰灞炴�с�恉ataStream銆戠殑鏍囪瘑甯搁噺
+ */
+public class DCResistanceMqttConstants {
+
+ public final static String NSDQCS_DQCS_SN = "NSDQCS.DQCS.SN";
+
+ public final static String NSDQCS_DQCS_DZZ = "NSDQCS.DQCS.DZZ";
+
+ public final static String NSDQCS_DQCS_64 = "NSDQCS.DQCS.64";
+
+}
diff --git a/cnas-device/src/main/java/com/ruoyi/device/mqtt/MQCallback.java b/cnas-device/src/main/java/com/ruoyi/device/mqtt/MQCallback.java
index 51fb42a..9cc678f 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/mqtt/MQCallback.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/mqtt/MQCallback.java
@@ -1,9 +1,15 @@
package com.ruoyi.device.mqtt;
+import cn.hutool.core.collection.CollectionUtil;
+import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.TypeReference;
+import com.ruoyi.device.constant.DCResistanceMqttConstants;
import com.ruoyi.device.service.CollectBridgeService;
+import com.ruoyi.device.vo.DCResistanceMqttVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
@@ -11,6 +17,9 @@
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
@Component
@Slf4j
@@ -71,9 +80,11 @@
public void messageArrived(String topic, MqttMessage message) throws Exception {
try {
String parse = new String(message.getPayload());
- JSONObject jsonObject = JSONObject.parseObject(parse);
+// JSONObject jsonObject = JSONObject.parseObject(parse);
// 濉厖閲囬泦鏁版嵁
- mqCallback.collectBridgeService.addBridgeValue(jsonObject);
+// mqCallback.collectBridgeService.addBridgeValue(jsonObject);
+ //鑰愪笣锛氱洿娴佺數闃绘暟鎹В鏋�
+ mqCallback.collectBridgeService.addBridgeValueByNS(parse);
} catch (Exception e) {
e.printStackTrace();
diff --git a/cnas-device/src/main/java/com/ruoyi/device/service/CollectBridgeService.java b/cnas-device/src/main/java/com/ruoyi/device/service/CollectBridgeService.java
index 3a6ce16..39c7782 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/service/CollectBridgeService.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/service/CollectBridgeService.java
@@ -17,5 +17,7 @@
* @param jsonObject
*/
void addBridgeValue(JSONObject jsonObject);
+
+ void addBridgeValueByNS(String jsonStr);
}
diff --git a/cnas-device/src/main/java/com/ruoyi/device/service/impl/CollectBridgeServiceImpl.java b/cnas-device/src/main/java/com/ruoyi/device/service/impl/CollectBridgeServiceImpl.java
index a1acb6e..6024f31 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/service/impl/CollectBridgeServiceImpl.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/service/impl/CollectBridgeServiceImpl.java
@@ -1,20 +1,29 @@
package com.ruoyi.device.service.impl;
+import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.RedisUtil;
+import com.ruoyi.device.constant.DCResistanceMqttConstants;
import com.ruoyi.device.mapper.CollectBridgeMapper;
import com.ruoyi.device.pojo.CollectBridge;
import com.ruoyi.device.service.CollectBridgeService;
+import com.ruoyi.device.vo.DCResistanceMqttVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
+import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
/**
* 鏁板瓧鐢垫ˉ閲囬泦
@@ -97,6 +106,22 @@
}
}
+ @Override
+ public void addBridgeValueByNS(String jsonStr) {
+ Map<String, Object> dataMap = JSONObject.parseObject(jsonStr, new TypeReference<Map<String, Object>>(){}.getType());
+ if(CollectionUtil.isNotEmpty(dataMap) && Objects.nonNull(dataMap.get("data"))){
+ List<DCResistanceMqttVO> dataList = JSONArray.parseObject(dataMap.get("data").toString(), new TypeReference<List<DCResistanceMqttVO>>(){}.getType());
+ //杩囨护鍑哄寘鍚疄闄呯數闃诲�肩殑瀵硅薄
+ DCResistanceMqttVO dcResistanceMqttVO = dataList.stream().filter(f -> StringUtils.equals(f.getDataStream(), DCResistanceMqttConstants.NSDQCS_DQCS_DZZ)).findFirst().orElse(null);
+ if(Objects.nonNull(dcResistanceMqttVO)){
+ List<DCResistanceMqttVO.DataPoint> dataPoints = dcResistanceMqttVO.getDataPoints();
+ BigDecimal value = CollectionUtil.isNotEmpty(dataPoints)?dataPoints.get(0).getValue():BigDecimal.ZERO;
+ //淇濆瓨瀹為檯鐢甸樆鍊煎埌redis
+ RedisUtil.set(DCResistanceMqttConstants.NSDQCS_DQCS_DZZ,value.doubleValue());
+ }
+ }
+ }
+
public static boolean isWithinTenMinutes(LocalDateTime dateTime1, LocalDateTime dateTime2) {
Duration duration = Duration.between(dateTime1, dateTime2);
long minutesDifference = Math.abs(duration.toMinutes());
diff --git a/cnas-device/src/main/java/com/ruoyi/device/vo/DCResistanceMqttVO.java b/cnas-device/src/main/java/com/ruoyi/device/vo/DCResistanceMqttVO.java
new file mode 100644
index 0000000..e19160d
--- /dev/null
+++ b/cnas-device/src/main/java/com/ruoyi/device/vo/DCResistanceMqttVO.java
@@ -0,0 +1,38 @@
+package com.ruoyi.device.vo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.List;
+
+/**
+ * 鐩存祦鐢甸樆锛氳В鏋恗qtt鏁版嵁鍝嶅簲瀵硅薄
+ */
+@Data
+public class DCResistanceMqttVO implements Serializable {
+
+ /**
+ * 鏁版嵁绫诲瀷鏍囪瘑
+ */
+ private String dataStream;
+
+ /**
+ * 鏁版嵁鑺傜偣瀵硅薄
+ */
+ private List<DataPoint> dataPoints;
+
+ @Data
+ public class DataPoint {
+ /**
+ * 鏃堕棿鎴冲瓧绗︿覆
+ */
+ private String time;
+
+ /**
+ * 瀹為檯鍊�
+ */
+ private BigDecimal value;
+ }
+
+}
diff --git a/ruoyi-admin-ztns/src/main/resources/application-druid.yml b/ruoyi-admin-ztns/src/main/resources/application-druid.yml
index 87bd38b..0cee7d2 100644
--- a/ruoyi-admin-ztns/src/main/resources/application-druid.yml
+++ b/ruoyi-admin-ztns/src/main/resources/application-druid.yml
@@ -8,7 +8,7 @@
druid:
# 涓诲簱鏁版嵁婧�
master:
- url: jdbc:mysql://localhost:3306/center-lims-ztns?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+ url: jdbc:mysql://localhost:3307/center-lims-ztns?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# 浠庡簱鏁版嵁婧�
@@ -72,6 +72,7 @@
database: 1
# # 瀵嗙爜
# password: root2022!
+ password: 123456
# 杩炴帴瓒呮椂鏃堕棿
timeout: 10s
lettuce:
@@ -152,7 +153,7 @@
examiningUrl: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=21993616-9966-4fe4-81b0-68e99a40c964
ifs:
- contract: ZTZB
+ contract: ZTNS
contract-key-get: 4ttDeLKNsZuhstjtROMcRE1USNFXKdFYE7lQ2p1m5Bo=
contract-key-post: 4ttDeLKNsZuhstjtROMcRE1USNFXKdFYE7lQ2p1m5Bo=
custorder: http://192.168.20.47/PurchService.ashx?
@@ -162,12 +163,12 @@
# 澶囨敞 18083瀵嗙爜锛歾ttZTT123!@
mqtt:
url: tcp://mqtt-ztt.zttiot.com:1883 # 鏈嶅姟鍣╥p
- username: forwarder # MQTT-鏈嶅姟绔�-鐢ㄦ埛鍚�,鍚庢湡浼氫慨鏀逛负鐢ㄦ埛鐧诲綍鍚�
- password: ZTlink12345 # MQTT-鏈嶅姟绔�-瀵嗙爜锛�,鍚庢湡浼氫慨鏀逛负鐢ㄦ埛鐧诲綍瀵嗙爜
+ username: 2455220 # MQTT-鏈嶅姟绔�-鐢ㄦ埛鍚�,鍚庢湡浼氫慨鏀逛负鐢ㄦ埛鐧诲綍鍚�
+ password: 108300 # MQTT-鏈嶅姟绔�-瀵嗙爜锛�,鍚庢湡浼氫慨鏀逛负鐢ㄦ埛鐧诲綍瀵嗙爜
timeout: 100 # 瓒呮椂鏃堕棿 锛堝崟浣嶏細绉掞級
keepalive: 60 # 蹇冭烦 锛堝崟浣嶏細绉掞級
qos: 1 # 蹇冭烦鍖呯骇鍒�
completion-timeout: 3000 # 杩炴帴瓒呮椂鏃堕棿锛堝崟浣嶏細绉掞級
- clientId: javaPublish # clientId
- subscribe: /ztt/v3/2443521/publish # 璁㈤槄涓婚
- client: false # 濡傛灉寮�鍙戦渶瑕佸惎鍔ㄦ祴璇曪紝闇�瑕佹敼涓篺alse涓嶇劧浼氫竴鐩存姤閿�
+ clientId: ztns # clientId
+ subscribe: /ztt/v3/2455220/publish # 璁㈤槄涓婚
+ client: true # 濡傛灉寮�鍙戦渶瑕佸惎鍔ㄦ祴璇曪紝闇�瑕佹敼涓篺alse涓嶇劧浼氫竴鐩存姤閿�
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java
new file mode 100644
index 0000000..ad77b81
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java
@@ -0,0 +1,631 @@
+package com.ruoyi.common.utils;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+import org.springframework.stereotype.Component;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class RedisUtil {
+ private static RedisTemplate<String, Object> redisTemplate;
+ public static RedisTemplate<String, Object> getRedisTemplate() {
+ return redisTemplate;
+ }
+ @Autowired
+ private RedisUtil(RedisConnectionFactory redisConnectionFactory){
+ // redisTemplate妯℃澘鍒濆鍖�
+ redisTemplate = new RedisTemplate<>();
+ redisTemplate.setConnectionFactory(redisConnectionFactory);
+
+ // 灏嗗璞″簭鍒楀寲
+ ObjectMapper om=new ObjectMapper();
+ om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+ om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
+ ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+
+ // String鐨勫簭鍒楀寲
+ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
+ // json搴忓垪鍖栭厤缃�
+ Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer<>(Object.class);
+ jackson2JsonRedisSerializer.setObjectMapper(om);
+ //key閲囩敤String鐨勫簭鍒楀寲鏂瑰紡
+ redisTemplate.setKeySerializer(stringRedisSerializer);
+ //hash鐨刱ey涔熼噰鐢⊿tring 鐨勫簭鍒楀寲鏂瑰紡
+ redisTemplate.setHashKeySerializer(stringRedisSerializer);
+ //value鐨勫簭鍒楀寲鏂瑰紡閲囩敤jackson鐨勬柟寮�
+ redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
+// //hash鐨剉alue搴忓垪鍖栨柟寮忛噰鐢╦ackson
+ redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
+ redisTemplate.afterPropertiesSet();
+ }
+
+ // =============================common============================
+ /**
+ * 鎸囧畾缂撳瓨澶辨晥鏃堕棿
+ * @param key 閿�
+ * @param time 鏃堕棿(绉�)
+ */
+ public static boolean expire(String key, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.expire(key, time, TimeUnit.SECONDS);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * 鏍规嵁key 鑾峰彇杩囨湡鏃堕棿
+ * @param key 閿� 涓嶈兘涓簄ull
+ * @return 鏃堕棿(绉�) 杩斿洖0浠h〃涓烘案涔呮湁鏁�
+ */
+ public static long getExpire(String key) {
+ return redisTemplate.getExpire(key, TimeUnit.SECONDS);
+ }
+
+
+ /**
+ * 鍒ゆ柇key鏄惁瀛樺湪
+ * @param key 閿�
+ * @return true 瀛樺湪 false涓嶅瓨鍦�
+ */
+ public static boolean hasKey(String key) {
+ try {
+ return redisTemplate.hasKey(key);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 鍒犻櫎缂撳瓨
+ * @param key 鍙互浼犱竴涓�� 鎴栧涓�
+ */
+// @SuppressWarnings("unchecked")
+ public static void del(String... key) {
+ if (key != null && key.length > 0) {
+ if (key.length == 1) {
+ redisTemplate.delete(key[0]);
+ } else {
+ redisTemplate.delete(Arrays.asList(key));
+// redisTemplate.delete(CollectionUtils.arrayToList(key));
+ }
+ }
+ }
+
+ /**
+ * 妯$硦鍖归厤鍒犻櫎澶氫釜缂撳瓨
+ * @param key 鍙互浼犱竴涓�� 鎴栧涓�
+ */
+ public static void delsLike(String key) {
+ Set<String> keys = redisTemplate.keys(key);
+ redisTemplate.delete(keys);
+ }
+
+ /**
+ * 鑾峰彇骞跺垹闄ょ紦瀛�
+ * @param key 閿�
+ * @return 鍊�
+ */
+ public static Object getAndDelete(String key) {
+ try{
+ return key == null ? null : get(key);
+ }finally {
+ del(key);
+ }
+ }
+
+ // ============================String=============================
+
+ /**
+ * 鏅�氱紦瀛樿幏鍙�
+ * @param key 閿�
+ * @return 鍊�
+ */
+ public static Object get(String key) {
+ return key == null ? null : redisTemplate.opsForValue().get(key);
+ }
+
+ /**
+ * 鏅�氱紦瀛樻斁鍏�
+ * @param key 閿�
+ * @param value 鍊�
+ * @return true鎴愬姛 false澶辫触
+ */
+
+ public static boolean set(String key, Object value) {
+ try {
+ redisTemplate.opsForValue().set(key, value);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 鏅�氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿
+ * @param key 閿�
+ * @param value 鍊�
+ * @param time 鏃堕棿(绉�) time瑕佸ぇ浜�0 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡
+ * @return true鎴愬姛 false 澶辫触
+ */
+
+ public static boolean set(String key, Object value, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
+ } else {
+ // 鍙慨鏀瑰弬鏁颁笉淇敼ttl
+ redisTemplate.opsForValue().set(key, value, 0);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 閫掑
+ * @param key 閿�
+ * @param delta 瑕佸鍔犲嚑(澶т簬0)
+ */
+ public static long incr(String key, long delta) {
+ if (delta < 0) {
+ throw new RuntimeException("閫掑鍥犲瓙蹇呴』澶т簬0");
+ }
+ return redisTemplate.opsForValue().increment(key, delta);
+ }
+
+
+ /**
+ * 閫掑噺
+ * @param key 閿�
+ * @param delta 瑕佸噺灏戝嚑(灏忎簬0)
+ */
+ public static long decr(String key, long delta) {
+ if (delta < 0) {
+ throw new RuntimeException("閫掑噺鍥犲瓙蹇呴』澶т簬0");
+ }
+ return redisTemplate.opsForValue().increment(key, -delta);
+ }
+
+
+ // ================================Map=================================
+
+ /**
+ * HashGet
+ * @param key 閿� 涓嶈兘涓簄ull
+ * @param item 椤� 涓嶈兘涓簄ull
+ */
+ public static Object hget(String key, String item) {
+ return redisTemplate.opsForHash().get(key, item);
+ }
+
+ /**
+ * 鑾峰彇hashKey瀵瑰簲鐨勬墍鏈夐敭鍊�
+ * @param key 閿�
+ * @return 瀵瑰簲鐨勫涓敭鍊�
+ */
+ public static Map<Object, Object> hmget(String key) {
+ return redisTemplate.opsForHash().entries(key);
+ }
+
+ /**
+ * HashSet
+ * @param key 閿�
+ * @param map 瀵瑰簲澶氫釜閿��
+ */
+ public static boolean hmset(String key, Map<String, Object> map) {
+ try {
+ redisTemplate.opsForHash().putAll(key, map);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * HashSet 骞惰缃椂闂�
+ * @param key 閿�
+ * @param map 瀵瑰簲澶氫釜閿��
+ * @param time 鏃堕棿(绉�)
+ * @return true鎴愬姛 false澶辫触
+ */
+ public static boolean hmset(String key, Map<String, Object> map, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.opsForHash().putAll(key, map);
+ expire(key, time);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 鍚戜竴寮爃ash琛ㄤ腑鏀惧叆鏁版嵁,濡傛灉涓嶅瓨鍦ㄥ皢鍒涘缓
+ *
+ * @param key 閿�
+ * @param item 椤�
+ * @param value 鍊�
+ * @return true 鎴愬姛 false澶辫触
+ */
+ public static boolean hset(String key, String item, Object value) {
+ try {
+ redisTemplate.opsForHash().put(key, item, value);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * 鍚戜竴寮爃ash琛ㄤ腑鏀惧叆鏁版嵁,濡傛灉涓嶅瓨鍦ㄥ皢鍒涘缓
+ *
+ * @param key 閿�
+ * @param item 椤�
+ * @param value 鍊�
+ * @param time 鏃堕棿(绉�) 娉ㄦ剰:濡傛灉宸插瓨鍦ㄧ殑hash琛ㄦ湁鏃堕棿,杩欓噷灏嗕細鏇挎崲鍘熸湁鐨勬椂闂�
+ * @return true 鎴愬姛 false澶辫触
+ */
+ public static boolean hset(String key, String item, Object value, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.opsForHash().put(key, item, value);
+ expire(key, time);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ /**
+ * 鍒犻櫎hash琛ㄤ腑鐨勫��
+ *
+ * @param key 閿� 涓嶈兘涓簄ull
+ * @param item 椤� 鍙互浣垮涓� 涓嶈兘涓簄ull
+ */
+ public static void hdel(String key, Object... item) {
+ redisTemplate.opsForHash().delete(key, item);
+ }
+
+
+ /**
+ * 鍒ゆ柇hash琛ㄤ腑鏄惁鏈夎椤圭殑鍊�
+ *
+ * @param key 閿� 涓嶈兘涓簄ull
+ * @param item 椤� 涓嶈兘涓簄ull
+ * @return true 瀛樺湪 false涓嶅瓨鍦�
+ */
+ public static boolean hHasKey(String key, String item) {
+ return redisTemplate.opsForHash().hasKey(key, item);
+ }
+
+
+ /**
+ * hash閫掑 濡傛灉涓嶅瓨鍦�,灏变細鍒涘缓涓�涓� 骞舵妸鏂板鍚庣殑鍊艰繑鍥�
+ *
+ * @param key 閿�
+ * @param item 椤�
+ * @param by 瑕佸鍔犲嚑(澶т簬0)
+ */
+ public static double hincr(String key, String item, double by) {
+ return redisTemplate.opsForHash().increment(key, item, by);
+ }
+
+
+ /**
+ * hash閫掑噺
+ *
+ * @param key 閿�
+ * @param item 椤�
+ * @param by 瑕佸噺灏戣(灏忎簬0)
+ */
+ public static double hdecr(String key, String item, double by) {
+ return redisTemplate.opsForHash().increment(key, item, -by);
+ }
+
+
+ // ============================set=============================
+
+ /**
+ * 鏍规嵁key鑾峰彇Set涓殑鎵�鏈夊��
+ * @param key 閿�
+ */
+ public static Set<Object> sGet(String key) {
+ try {
+ return redisTemplate.opsForSet().members(key);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+
+ /**
+ * 鏍规嵁value浠庝竴涓猻et涓煡璇�,鏄惁瀛樺湪
+ *
+ * @param key 閿�
+ * @param value 鍊�
+ * @return true 瀛樺湪 false涓嶅瓨鍦�
+ */
+ public static boolean sHasKey(String key, Object value) {
+ try {
+ return redisTemplate.opsForSet().isMember(key, value);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 灏嗘暟鎹斁鍏et缂撳瓨
+ *
+ * @param key 閿�
+ * @param values 鍊� 鍙互鏄涓�
+ * @return 鎴愬姛涓暟
+ */
+ public static long sSet(String key, Object... values) {
+ try {
+ return redisTemplate.opsForSet().add(key, values);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+
+ /**
+ * 灏唖et鏁版嵁鏀惧叆缂撳瓨
+ *
+ * @param key 閿�
+ * @param time 鏃堕棿(绉�)
+ * @param values 鍊� 鍙互鏄涓�
+ * @return 鎴愬姛涓暟
+ */
+ public static long sSetAndTime(String key, long time, Object... values) {
+ try {
+ Long count = (long)values.length;
+ if (time > 0) {
+ count = redisTemplate.opsForSet().add(key, values);
+ expire(key, time);
+ }
+ return count;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+
+ /**
+ * 鑾峰彇set缂撳瓨鐨勯暱搴�
+ *
+ * @param key 閿�
+ */
+ public static long sGetSetSize(String key) {
+ try {
+ return redisTemplate.opsForSet().size(key);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+
+ /**
+ * 绉婚櫎鍊间负value鐨�
+ *
+ * @param key 閿�
+ * @param values 鍊� 鍙互鏄涓�
+ * @return 绉婚櫎鐨勪釜鏁�
+ */
+
+ public static long setRemove(String key, Object... values) {
+ try {
+ return redisTemplate.opsForSet().remove(key, values);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+ // ===============================list=================================
+
+ /**
+ * 鑾峰彇list缂撳瓨鐨勫唴瀹�
+ *
+ * @param key 閿�
+ * @param start 寮�濮�
+ * @param end 缁撴潫 0 鍒� -1浠h〃鎵�鏈夊��
+ */
+ public static List<Object> lGet(String key, long start, long end) {
+ try {
+ return redisTemplate.opsForList().range(key, start, end);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+
+ /**
+ * 鑾峰彇list缂撳瓨鐨勯暱搴�
+ *
+ * @param key 閿�
+ */
+ public static long lGetListSize(String key) {
+ try {
+ return redisTemplate.opsForList().size(key);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+
+
+ /**
+ * 閫氳繃绱㈠紩 鑾峰彇list涓殑鍊�
+ *
+ * @param key 閿�
+ * @param index 绱㈠紩 index>=0鏃讹紝 0 琛ㄥご锛�1 绗簩涓厓绱狅紝渚濇绫绘帹锛沬ndex<0鏃讹紝-1锛岃〃灏撅紝-2鍊掓暟绗簩涓厓绱狅紝渚濇绫绘帹
+ */
+ public static Object lGetIndex(String key, long index) {
+ try {
+ return redisTemplate.opsForList().index(key, index);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+
+ /**
+ * 灏唋ist鏀惧叆缂撳瓨
+ *
+ * @param key 閿�
+ * @param value 鍊�
+ */
+ public static boolean lSet(String key, Object value) {
+ try {
+ redisTemplate.opsForList().rightPush(key, value);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 灏唋ist鏀惧叆缂撳瓨
+ * @param key 閿�
+ * @param value 鍊�
+ * @param time 鏃堕棿(绉�)
+ */
+ public static boolean lSet(String key, Object value, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.opsForList().rightPush(key, value);
+ expire(key, time);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ }
+
+
+ /**
+ * 灏唋ist鏀惧叆缂撳瓨
+ *
+ * @param key 閿�
+ * @param value 鍊�
+ * @return true 瀛樻斁鎴愬姛 false瀛樻斁澶辫触
+ */
+ public static boolean lSet(String key, List<Object> value) {
+ try {
+ redisTemplate.opsForList().rightPushAll(key, value);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ }
+
+
+ /**
+ * 灏唋ist鏀惧叆缂撳瓨
+ *
+ * @param key 閿�
+ * @param value 鍊�
+ * @param time 鏃堕棿(绉�)
+ * @return true 瀛樻斁鎴愬姛 false瀛樻斁澶辫触
+ */
+ public static boolean lSet(String key, List<Object> value, long time) {
+ try {
+ if (time > 0) {
+ redisTemplate.opsForList().rightPushAll(key, value);
+ expire(key, time);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 鏍规嵁绱㈠紩淇敼list涓殑鏌愭潯鏁版嵁
+ *
+ * @param key 閿�
+ * @param index 绱㈠紩
+ * @param value 鍊�
+ * @return true 瀛樻斁鎴愬姛 false瀛樻斁澶辫触
+ */
+
+ public static boolean lUpdateIndex(String key, long index, Object value) {
+ try {
+ redisTemplate.opsForList().set(key, index, value);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+
+ /**
+ * 绉婚櫎N涓�间负value
+ *
+ * @param key 閿�
+ * @param count 绉婚櫎澶氬皯涓�
+ * @param value 鍊�
+ * @return 绉婚櫎鐨勪釜鏁�
+ */
+
+ public static long lRemove(String key, long count, Object value) {
+ try {
+ return redisTemplate.opsForList().remove(key, count, value);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+}
+
--
Gitblit v1.9.3