From db287aac9eb5032b1158b405a9b4807301467ed3 Mon Sep 17 00:00:00 2001 From: zhouxiang <zhouxiang> Date: 星期四, 24 三月 2022 17:48:03 +0800 Subject: [PATCH] 添加redis获取整个hashKey数据和单个hashKey数据方法 --- src/main/java/com/hx/redis/RedisUtil.java | 128 ++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 121 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..dbcfa16 100644 --- a/src/main/java/com/hx/redis/RedisUtil.java +++ b/src/main/java/com/hx/redis/RedisUtil.java @@ -4,10 +4,13 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.Map; import java.util.concurrent.TimeUnit; -/** 鏂囦欢澶勭悊宸ュ叿 +/** + * 鏂囦欢澶勭悊宸ュ叿 + * * @author wangrenhuang * @Date 2021-10-19 */ @@ -20,9 +23,9 @@ private RedisTemplate<String, Object> redisTemplate; - /** * [鍒ゆ柇key鏄惁瀛樺湪] + * * @param key 閿� * @return true 瀛樺湪 false涓嶅瓨鍦� */ @@ -36,14 +39,17 @@ /** * [鏅�氱紦瀛樿幏鍙朷 + * * @param key 閿� * @return 鍊� */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } + /** * [鏅�氱紦瀛樺垹闄 + * * @param key 閿� * @return 鍊� */ @@ -51,13 +57,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 +81,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 +101,110 @@ } } -} + /** + * 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; + + } + + } + + + /** + * 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,String key) { + try { + return redisTemplate.opsForHash().get(hashKey,key); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + +} -- Gitblit v1.8.0