From 43d11becdfc0d7af00b10e91b962cd0c4738bbf1 Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期一, 11 十二月 2023 10:41:56 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- src/main/java/com/hx/redis/RedisUtil.java | 332 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 325 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/hx/redis/RedisUtil.java b/src/main/java/com/hx/redis/RedisUtil.java index 3b8a943..d9b582b 100644 --- a/src/main/java/com/hx/redis/RedisUtil.java +++ b/src/main/java/com/hx/redis/RedisUtil.java @@ -1,13 +1,18 @@ 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; -/** 鏂囦欢澶勭悊宸ュ叿 +/** + * 鏂囦欢澶勭悊宸ュ叿 + * * @author wangrenhuang * @Date 2021-10-19 */ @@ -19,10 +24,22 @@ @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鏄惁瀛樺湪] + * * @param key 閿� * @return true 瀛樺湪 false涓嶅瓨鍦� */ @@ -36,14 +53,17 @@ /** * [鏅�氱紦瀛樿幏鍙朷 + * * @param key 閿� * @return 鍊� */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } + /** * [鏅�氱紦瀛樺垹闄 + * * @param key 閿� * @return 鍊� */ @@ -51,13 +71,15 @@ try { Boolean aBoolean = redisTemplate.hasKey(key); return aBoolean == false ? true : redisTemplate.delete(key); - }catch (Exception e){ + } catch (Exception e) { return false; } } + /** * [鏅�氱紦瀛樻斁鍏 - * @param key 閿� + * + * @param key 閿� * @param value 鍊� * @return true鎴愬姛 false澶辫触 */ @@ -73,9 +95,10 @@ /** * 鏅�氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿 - * @param key 閿� + * + * @param key 閿� * @param value 鍊� - * @param time 鏃堕棿(绉�) time瑕佸ぇ浜�0 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡 + * @param time 鏃堕棿(绉�) time瑕佸ぇ浜�0 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡 * @return true鎴愬姛 false 澶辫触 */ public boolean set(String key, Object value, long time) { @@ -92,5 +115,300 @@ } } -} + /** + * 26 + * 鎸囧畾缂撳瓨澶辨晥鏃堕棿 + * 27 + * + * @param key 閿� + * 28 + * @param time 鏃堕棿(绉�) + * 29 + * @return 30 + */ + public boolean expire(String key, long time) { + try { + if (time > 0) { + redisTemplate.expire(key, time, TimeUnit.SECONDS); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + + } + + } + + /** + * 璁剧疆鍒版湡鏃堕棿 + * @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; + } + + + /** + * hash 鍒犻櫎鎿嶄綔 + * + * @param key 閿� + * @param valueKey 鍊� + * @return true鎴愬姛 false 澶辫触 + */ + public boolean hashDel(String key, Object... valueKey) { + try { + redisTemplate.opsForHash().delete(key,valueKey); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * hash 璁剧疆hashMapt鎿嶄綔 + * + * @param key 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public boolean hashSetMap(String key, Map<String,Object> map) { + try { + redisTemplate.opsForHash().putAll(key, map); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * hash 璁剧疆鍗曚釜hashMap鎿嶄綔 + * + * @param key 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public boolean hashSet(String hashKey, String key, Object value) { + try { + redisTemplate.opsForHash().put(hashKey,key,value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * hash 鑾峰彇鏁翠釜hashKey鏁版嵁 + * + * @param hashKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Map<Object, Object> hashGetAll(String hashKey) { + try { + return redisTemplate.opsForHash().entries(hashKey); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * hash 鑾峰彇鍗曚釜hashKey + * + * @param hashKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object hashGet(String hashKey,Object key) { + try { + return redisTemplate.opsForHash().get(hashKey,key); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鏂板-宸︽彃鍏� + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object leftPush(String listKey,Object value) { + try { + return redisTemplate.opsForList().leftPush(listKey,value); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鏂板-宸︽彃鍏�(瀛樺湪鎵嶆彃鍏�) + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object leftPushIfPresent(String listKey,Object value) { + try { + return redisTemplate.opsForList().leftPushIfPresent(listKey,value); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鏂板-鍙虫彃鍏�(瀛樺湪鎵嶆彃鍏�) + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object rightPushIfPresent(String listKey,Object value) { + try { + return redisTemplate.opsForList().rightPushIfPresent(listKey,value); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鏂板-鍙虫彃鍏� + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object rightPush(String listKey,Object value) { + try { + return redisTemplate.opsForList().rightPush(listKey,value); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 寮瑰嚭-宸� + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object leftPop(String listKey) { + try { + return redisTemplate.opsForList().leftPop(listKey); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 寮瑰嚭-鍙� + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object rightPop(String listKey) { + try { + return redisTemplate.opsForList().rightPop(listKey); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鑾峰彇绱㈠紩涓嬬殑鍊� + * + * @param key 閿� + * @param index 绱㈠紩 + * @return true鎴愬姛 false 澶辫触 + */ + 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; + } + } + + /** + * list 鑾峰彇闀垮害 + * + * @param listKey 閿� + * @return true鎴愬姛 false 澶辫触 + */ + public Object listSize(String listKey) { + try { + return redisTemplate.opsForList().size(listKey); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * list 鍒犻櫎 + * + * @param listKey 閿� + * @param index index=0, 鍒犻櫎鎵�鏈夊�肩瓑浜巚alue鐨勫厓绱�; index>0, 浠庡ご閮ㄥ紑濮嬪垹闄ょ涓�涓�肩瓑浜巚alue鐨勫厓绱�; index<0, 浠庡熬閮ㄥ紑濮嬪垹闄ょ涓�涓�肩瓑浜巚alue鐨勫厓绱� + * @param value 鍒犻櫎鐨勫�� + * @return true鎴愬姛 false 澶辫触 + */ + public Object listRemove(String listKey,long index,Object value) { + try { + return redisTemplate.opsForList().remove(listKey, index, value); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * @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