From dca24adb8ee2fcde5e59c6eaf28555d6cc451496 Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期日, 08 十月 2023 18:04:37 +0800 Subject: [PATCH] 1.修改 --- src/main/java/com/hx/redis/RedisUtil.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 50 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/redis/RedisUtil.java b/src/main/java/com/hx/redis/RedisUtil.java index 27b676c..d9b582b 100644 --- a/src/main/java/com/hx/redis/RedisUtil.java +++ b/src/main/java/com/hx/redis/RedisUtil.java @@ -1,9 +1,11 @@ package com.hx.redis; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -22,6 +24,18 @@ @Resource private RedisTemplate<String, Object> redisTemplate; + /** + * 鍒囨崲鏁版嵁搴� + * @param dataNumber 鏁版嵁搴撶紪鍙� + */ + public void setDataBase(int dataNumber) { + LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory(); + if (connectionFactory != null && dataNumber != connectionFactory.getDatabase()) { + connectionFactory.setDatabase(dataNumber); + this.redisTemplate.setConnectionFactory(connectionFactory); + connectionFactory.resetConnection(); + } + } /** * [鍒ゆ柇key鏄惁瀛樺湪] @@ -125,6 +139,26 @@ } + } + + /** + * 璁剧疆鍒版湡鏃堕棿 + * @param key 瀵瑰簲閿� + * @param time 鏃堕暱 + * @param timeUnit 鏃堕棿鍗曚綅 + * @return + */ + public boolean expire(String key, long time, TimeUnit timeUnit) { + try { + if (time > 0) { + return redisTemplate.expire(key, time, timeUnit); + } + } catch (Exception e) { + e.printStackTrace(); + return false; + + } + return false; } @@ -321,7 +355,7 @@ * @param end 缁撴潫浣嶇疆,-1杩斿洖鎵�鏈� * @return true鎴愬姛 false 澶辫触 */ - public Object listGetRange(String key, long start , long end) { + public List<Object> listGetRange(String key, long start , long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { @@ -362,4 +396,19 @@ } } + /** + * @param key 閿� + * @param value 鍊� + * @param timeOut 鏃堕棿 + * @param unit 鏃堕棿鍗曚綅 + */ + public Boolean setIfAbsent(String key, Object value, long timeOut, TimeUnit unit) { + try { + return redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, unit); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + } -- Gitblit v1.8.0