Java實(shí)現(xiàn)RedisUtils操作五大集合(增刪改查)
前排提示,我在這個(gè)工具類加了@Component注解,如果在springboot的項(xiàng)目使用,記得通過@Autowired注入使用。
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import org.springframework.stereotype.Component;import java.io.Serializable;import java.util.List;import java.util.Set; @Componentpublic class RedisUtils { @Autowired private RedisTemplate redisTemplate; /** * 寫入String型 [ 鍵,值] * * @param key * @param value * @return */ public boolean set(final String key, Object value) {boolean result = false;try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true;} catch (Exception e) { e.printStackTrace();}return result; } /** * 寫入String型,順便帶有過期時(shí)間 [ 鍵,值] * * @param key * @param value * @return */ public boolean setWithTime(final String key, Object value,int seconds) {boolean result = false;try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value,seconds, TimeUnit.SECONDS); result = true;} catch (Exception e) { e.printStackTrace();}return result; } /** * 批量刪除對(duì)應(yīng)的value * * @param keys */ public void remove(final String... keys) {for (String key : keys) { remove(key);} } /** * 批量刪除key * * @param pattern */ public void removePattern(final String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);if (keys.size() > 0) redisTemplate.delete(keys); } /** * 刪除對(duì)應(yīng)的value * * @param key */ public void remove(final String key) {if (exists(key)) { redisTemplate.delete(key);} } /** * 判斷緩存中是否有對(duì)應(yīng)的value * * @param key * @return */ public boolean exists(final String key) {return redisTemplate.hasKey(key); } /** * 讀取緩存 * * @param key * @return */ public Object get(final String key) {Object result = null;ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();result = operations.get(key);return result; } /** * 哈希 添加 * hash 一個(gè)鍵值(key->value)對(duì)集合 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * Hash獲取數(shù)據(jù) * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();return hash.get(key, hashKey); } /** * 列表添加 * list:lpush key value1 * * @param k * @param v */ public void lPush(String k, Object v) {ListOperations<String, Object> list = redisTemplate.opsForList();list.rightPush(k, v); } /** * 列表List獲取 * lrange: key 0 10 (讀取的個(gè)數(shù) 從0開始 讀取到下標(biāo)為10 的數(shù)據(jù)) * * @param k * @param l * @param l1 * @return */ public List<Object> lRange(String k, long l, long l1) {ListOperations<String, Object> list = redisTemplate.opsForList();return list.range(k, l, l1); } /** * Set集合添加 * * @param key * @param value */ public void add(String key, Object value) {SetOperations<String, Object> set = redisTemplate.opsForSet();set.add(key, value); } /** * Set 集合獲取 * * @param key * @return */ public Set<Object> setMembers(String key) { SetOperations<String, Object> set = redisTemplate.opsForSet(); return set.members(key); } /** * Sorted set :有序集合添加 * * @param key * @param value * @param scoure */ public void zAdd(String key, Object value, double scoure) {ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();zset.add(key, value, scoure); } /** * Sorted set:有序集合獲取 * * @param key * @param scoure * @param scoure1 * @return */ public Set<Object> rangeByScore(String key, double scoure, double scoure1) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); return zset.rangeByScore(key, scoure, scoure1); } /** * 根據(jù)key獲取Set中的所有值 * * @param key 鍵 * @return */ public Set<Integer> sGet(String key) {try { return redisTemplate.opsForSet().members(key);} catch (Exception e) { e.printStackTrace(); return null;} } /** * 根據(jù)value從一個(gè)set中查詢,是否存在 * * @param key 鍵 * @param value 值 * @return true 存在 false不存在 */ public boolean sHasKey(String key, Object value) {try { return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) { e.printStackTrace(); return false;} } }
到此這篇關(guān)于Java實(shí)現(xiàn)RedisUtils操作五大集合(增刪改查)的文章就介紹到這了,更多相關(guān)Java RedisUtils操作內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)2. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)3. jsp 實(shí)現(xiàn)的簡(jiǎn)易mvc模式示例4. jscript與vbscript 操作XML元素屬性的代碼5. JSP開發(fā)之hibernate之單向多對(duì)一關(guān)聯(lián)的實(shí)例6. 基于PHP做個(gè)圖片防盜鏈7. XML在語音合成中的應(yīng)用8. Jsp servlet驗(yàn)證碼工具類分享9. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
