From a0536b4aac5867173d6b0280cddbf87cb52de78b Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期四, 16 十月 2025 18:22:30 +0800
Subject: [PATCH] Merge branch 'dev' of http://120.76.84.145:10101/gitblit/r/java/ZhaoYangChuXing
---
MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java | 240 +++++++++++++++++++++--------------------------------------
1 files changed, 87 insertions(+), 153 deletions(-)
diff --git a/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java b/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java
index 61d39a8..ce79415 100644
--- a/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java
+++ b/MessagePushTravel/src/main/java/com/sinata/push/util/RedisUtil.java
@@ -1,14 +1,16 @@
package com.sinata.push.util;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.geo.Point;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
-import redis.clients.jedis.Jedis;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.Pipeline;
+import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
/**
@@ -16,25 +18,24 @@
*/
@Component
public class RedisUtil {
-
+
@Autowired
- private JedisPool jedisPool;
-
-
+ private RedisTemplate redisTemplate;
+
+
+
/**
* 向redis中存储字符串没有过期时间
* @param key
* @param value
*/
public void setStrValue(String key, String value){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- String set = resource.set(key, value);
- resource.close();
+ if(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){
+ redisTemplate.opsForValue().set(key, value);
}
}
-
-
+
+
/**
* 以分钟为单位设置存储值(设置过期时间)
* @param key
@@ -42,174 +43,107 @@
* @param time 秒
*/
public void setStrValue(String key, String value, int time){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- String setex = resource.setex(key, time, value);
- resource.close();
+ if(!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)){
+ redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
}
-
-
+
+
/**
* 从redis中获取值
* @param key
* @return
*/
public String getValue(String key){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- String data = resource.get(key);
- resource.close();
+ if(!StringUtils.isEmpty(key)){
+ String data = (String) redisTemplate.opsForValue().get(key);
return data;
}
return null;
}
-
-
- /**
- * 批量获取
- * @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();
- resource.close();
- 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;
- }
-
-
- /**
- * 从右侧获取数据
- * @param key
- * @return
- */
- public String getValueList(String key){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- String s = resource.lpop(key);
- resource.close();
- return s;
- }
- return null;
- }
-
-
-
-
-
-
- /**
- * 从左侧添加数据到list中
- * @param key
- * @param s
- */
- public void addValueList(String key, String s){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- resource.rpush(key, s);
- resource.close();
- }
- }
-
-
-
-
-
+
+
+
+
/**
* 删除key
* @param key
*/
public void remove(String key){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- Long del = resource.del(key);
- resource.close();
+ if(!StringUtils.isEmpty(key)){
+ redisTemplate.delete(key);
}
}
-
-
+
+
+
+
+
+
+
/**
- * 删除list集合数据
+ * redis加锁
* @param key
- * @param count
* @param value
+ * @param time
+ * @return
*/
- public void delValueList(String key, long count, String value){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- Long lrem = resource.lrem(key, count, value);
- resource.close();
+ public boolean lock(String key, String value, int time){
+ if(!StringUtils.isEmpty(key)){
+ key += "_lock";
+ return redisTemplate.opsForValue().setIfAbsent(key, value);
}
+ return false;
+ }
+
+ /**
+ * 获取redis锁
+ * @param time
+ * @return
+ */
+ public boolean lock(int time){
+ String uuid = UUID.randomUUID().toString();
+ return lock("redis", uuid, time);
+ }
+
+
+ public boolean lock(String key, int time){
+ String uuid = UUID.randomUUID().toString();
+ return lock(key, uuid, time);
+ }
+
+
+ /**
+ * redis释放锁
+ * @param key
+ * @return
+ */
+ public boolean unlock(String key){
+ if(!StringUtils.isEmpty(key)){
+ key += "_lock";
+ return redisTemplate.delete(key);
+ }
+ return false;
+ }
+
+ /**
+ * 删除锁
+ * @return
+ */
+ public boolean unlock(){
+ return unlock("redis");
}
/**
- * 获取list集合数量
- * @param key
- * @return
+ * 添加地理空间索引坐标
+ * @param k
+ * @param lon
+ * @param lat
+ * @param object
*/
- public Long getListCount(String key){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- Long length = resource.llen(key);
- resource.close();
- return length;
- }
- return 0L;
- }
-
-
- /**
- * 获取集合内容
- * @param key
- * @param start
- * @param end
- * @return
- */
- public List<String> getList(String key, long start, long end){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- List<String> lrange = resource.lrange(key, start, end);
- resource.close();
- return lrange;
- }
- return new ArrayList<>();
- }
-
-
- /**
- * 获取list所有数据
- * @param key
- * @return
- */
- public List<String> getAllList(String key){
- if(StringUtil.isNotEmpty(key)){
- Jedis resource = jedisPool.getResource();
- Long llen = resource.llen(key);
- List<String> lrange = resource.lrange(key, 0, llen - 1);
- resource.close();
- return lrange;
- }
- return new ArrayList<>();
+ public void addGeo(String k, Double lon, Double lat, String object){
+ Point point = new Point(lon, lat);
+ redisTemplate.opsForGeo().add(k, point, object);
}
}
--
Gitblit v1.7.1