From 4c4dc127cdc9c41f2bfb3c138108529856e031b8 Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期四, 07 八月 2025 16:55:23 +0800 Subject: [PATCH] 提交基础版本 --- zuul/src/main/java/com/sinata/zuul/util/RedisUtil.java | 81 +++++----------------------------------- 1 files changed, 10 insertions(+), 71 deletions(-) diff --git a/zuul/src/main/java/com/sinata/zuul/util/RedisUtil.java b/zuul/src/main/java/com/sinata/zuul/util/RedisUtil.java index 3abe256..fcd6813 100644 --- a/zuul/src/main/java/com/sinata/zuul/util/RedisUtil.java +++ b/zuul/src/main/java/com/sinata/zuul/util/RedisUtil.java @@ -1,14 +1,10 @@ package com.sinata.zuul.util; -import org.springframework.beans.factory.annotation.Autowired; +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 java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; /** @@ -17,8 +13,8 @@ @Component public class RedisUtil { - @Autowired - private JedisPool jedisPool; + @Resource + private RedisTemplate<String, Object> redisTemplate; /** @@ -27,11 +23,7 @@ * @param value */ public void setStrValue(String key, String value){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String set = resource.set(key, value); - closeJedis(resource); - } + redisTemplate.opsForValue().set(key, value); } @@ -42,11 +34,7 @@ * @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); - closeJedis(resource); - } + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } @@ -56,47 +44,11 @@ * @return */ public String getValue(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - String data = resource.get(key); - closeJedis(resource); - return data; - } - return null; + return (String) redisTemplate.opsForValue().get(key); } - /** - * 批量获取 - * @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(); - - 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; - } + /** @@ -104,21 +56,8 @@ * @param key */ public void remove(String key){ - if(StringUtil.isNotEmpty(key)){ - Jedis resource = jedisPool.getResource(); - Long del = resource.del(key); - closeJedis(resource); - } + redisTemplate.delete(key); } - /** - * 删除资源 - * @param jedis - */ - public void closeJedis(Jedis jedis){ - if(null != jedis){ - jedis.close(); - } - } } -- Gitblit v1.7.1