From 5acd00fd233757a9c7616b23b195e93e3cdb605a Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期一, 26 十二月 2022 17:06:22 +0800 Subject: [PATCH] 新增时间算法 --- src/main/java/com/hx/redis/RedisUtil.java | 31 ++++++++++++++++++++++++++++++- 1 files changed, 30 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..e17cf25 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鏄惁瀛樺湪] @@ -321,7 +335,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 +376,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