From 32e62c64b2e384d11443cb4ae90177d5664dc8d8 Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期五, 17 六月 2022 18:53:31 +0800 Subject: [PATCH] Object返回Json --- src/main/java/com/hx/redis/RedisUtil.java | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 47 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/redis/RedisUtil.java b/src/main/java/com/hx/redis/RedisUtil.java index 1fabfae..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鏄惁瀛樺湪] @@ -298,7 +312,7 @@ } /** - * list 鑾峰彇 + * list 鑾峰彇绱㈠紩涓嬬殑鍊� * * @param key 閿� * @param index 绱㈠紩 @@ -307,6 +321,23 @@ public Object listGet(String key, long index) { try { return redisTemplate.opsForList().index(key,index); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鑾峰彇绱㈠紩涓嬬殑鍊� + * + * @param key 閿� + * @param start 寮�濮嬩綅缃� 0鏄紑濮嬩綅缃� + * @param end 缁撴潫浣嶇疆,-1杩斿洖鎵�鏈� + * @return true鎴愬姛 false 澶辫触 + */ + public List<Object> listGetRange(String key, long start , long end) { + try { + return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; @@ -345,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