RedisTemplate常用方法大全(面試必備)
更新時(shí)間:2024年05月12日 10:44:31 作者:逆流°只是風(fēng)景-bjhxcc
RedisTemplate是SpringData Redis提供的一個(gè)類,本文主要介紹了RedisTemplate常用方法大全,具有一定的參考價(jià)值,感興趣的可以了解一下
Redis常用的數(shù)據(jù)類型:String、Hash、List、Set、zSet
1.RedisTemplate常用方法
redisTemplate.hasKey(key); //判斷是否有key所對(duì)應(yīng)的值,有則返回true,沒有則返回false redisTemplate.opsForValue().get(key); //有則取出key值所對(duì)應(yīng)的值 redisTemplate.delete(key); //刪除單個(gè)key值 redisTemplate.delete(keys); //其中keys:Collection<K> keys redisTemplate.dump(key); //將當(dāng)前傳入的key值序列化為byte[]類型 redisTemplate.expire(key, timeout, unit); //設(shè)置過期時(shí)間 redisTemplate.expireAt(key, date); //設(shè)置過期時(shí)間 redisTemplate.keys(pattern); //查找匹配的key值,返回一個(gè)Set集合類型 redisTemplate.rename(oldKey, newKey); //返回傳入key所存儲(chǔ)的值的類型 redisTemplate.renameIfAbsent(oldKey, newKey); //如果舊值存在時(shí),將舊值改為新值 redisTemplate.randomKey(); //從redis中隨機(jī)取出一個(gè)key redisTemplate.getExpire(key); //返回當(dāng)前key所對(duì)應(yīng)的剩余過期時(shí)間 redisTemplate.getExpire(key, unit); //返回剩余過期時(shí)間并且指定時(shí)間單位 redisTemplate.persist(key); //將key持久化保存 redisTemplate.move(key, dbIndex); //將當(dāng)前數(shù)據(jù)庫的key移動(dòng)到指定redis中數(shù)據(jù)庫當(dāng)中
2.String類型
ValueOperations opsForValue = redisTemplate.opsForValue(); opsForValue.set(key, value); //設(shè)置當(dāng)前的key以及value值 opsForValue.set(key, value, offset);//用 value 參數(shù)覆寫給定 key 所儲(chǔ)存的字符串值,從偏移量 offset 開始 opsForValue.set(key, value, timeout, unit); //設(shè)置當(dāng)前的key以及value值并且設(shè)置過期時(shí)間 opsForValue.setBit(key, offset, value); //將二進(jìn)制第offset位值變?yōu)関alue opsForValue.setIfAbsent(key, value);//重新設(shè)置key對(duì)應(yīng)的值,如果存在返回false,否則返回true opsForValue.get(key, start, end); //返回key中字符串的子字符 opsForValue.getAndSet(key, value); //將舊的key設(shè)置為value,并且返回舊的key opsForValue.multiGet(keys); //批量獲取值 opsForValue.size(key); //獲取字符串的長度 opsForValue.append(key, value); //在原有的值基礎(chǔ)上新增字符串到末尾 opsForValue.increment(key,double increment);//以增量的方式將double值存儲(chǔ)在變量中 opsForValue.increment(key,long increment); //通過increment(K key, long delta)方法以增量方式存儲(chǔ)long值(正值則自增,負(fù)值則自減) Map valueMap = new HashMap(); valueMap.put("valueMap1","map1"); valueMap.put("valueMap2","map2"); valueMap.put("valueMap3","map3"); opsForValue.multiSetIfAbsent(valueMap); //如果對(duì)應(yīng)的map集合名稱不存在,則添加否則不做修改 opsForValue.multiSet(valueMap); //設(shè)置map集合到redis
3.Hash類型
HashOperations opsForHash = redisTemplate.opsForHash(); opsForHash.get(key, field); //獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null opsForHash.entries(key); //獲取變量中的鍵值對(duì) opsForHash.put(key, hashKey, value); //新增hashMap值 opsForHash.putAll(key, maps); //以map集合的形式添加鍵值對(duì) opsForHash.putIfAbsent(key, hashKey, value); //僅當(dāng)hashKey不存在時(shí)才設(shè)置 opsForHash.delete(key, fields); //刪除一個(gè)或者多個(gè)hash表字段 opsForHash.hasKey(key, field); //查看hash表中指定字段是否存在 opsForHash.increment(key, field, long increment); //給哈希表key中的指定字段的整數(shù)值加上增量increment opsForHash.increment(key, field, double increment); //給哈希表key中的指定字段的整數(shù)值加上增量increment opsForHash.keys(key); //獲取所有hash表中字段 opsForHash.values(key); //獲取hash表中存在的所有的值 opsForHash.scan(key, options); //匹配獲取鍵值對(duì),ScanOptions.NONE為獲取全部鍵對(duì)
4.List類型
ListOperations opsForList = redisTemplate.opsForList(); opsForList.index(key, index); //通過索引獲取列表中的元素 opsForList.range(key, start, end); //獲取列表指定范圍內(nèi)的元素(start開始位置, 0是開始位置,end 結(jié)束位置, -1返回所有) opsForList.leftPush(key, value); //存儲(chǔ)在list的頭部,即添加一個(gè)就把它放在最前面的索引處 opsForList.leftPush(key, pivot, value); //如果pivot處值存在則在pivot前面添加 opsForList.leftPushAll(key, value); //把多個(gè)值存入List中(value可以是多個(gè)值,也可以是一個(gè)Collection value) opsForList.leftPushIfPresent(key, value); //List存在的時(shí)候再加入 opsForList.rightPush(key, value); //按照先進(jìn)先出的順序來添加(value可以是多個(gè)值,或者是Collection var2) opsForList.rightPushAll(key, value); //在pivot元素的右邊添加值 opsForList.set(key, index, value); //設(shè)置指定索引處元素的值 opsForList.trim(key, start, end); //將List列表進(jìn)行剪裁 opsForList.size(key); //獲取當(dāng)前key的List列表長度 //移除并獲取列表中第一個(gè)元素(如果列表沒有元素會(huì)阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止) opsForList.leftPop(key); opsForList.leftPop(key, timeout, unit); //移除并獲取列表最后一個(gè)元素 opsForList.rightPop(key); opsForList.rightPop(key, timeout, unit); //從一個(gè)隊(duì)列的右邊彈出一個(gè)元素并將這個(gè)元素放入另一個(gè)指定隊(duì)列的最左邊 opsForList.rightPopAndLeftPush(sourceKey, destinationKey); opsForList.rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit); //刪除集合中值等于value的元素(index=0, 刪除所有值等于value的元素; index>0, 從頭部開始刪除第一個(gè)值等于value的元素; index<0, 從尾部開始刪除第一個(gè)值等于value的元素) opsForList.remove(key, index, value);
5.Set類型
SetOperations opsForSet = redisTemplate.opsForSet(); opsForSet.add(key, values); //添加元素 opsForSet.remove(key, values); //移除元素(單個(gè)值、多個(gè)值) opsForSet.pop(key); //刪除并且返回一個(gè)隨機(jī)的元素 opsForSet.size(key); //獲取集合的大小 opsForSet.isMember(key, value); //判斷集合是否包含value opsForSet.intersect(key, otherKey); //獲取兩個(gè)集合的交集(key對(duì)應(yīng)的無序集合與otherKey對(duì)應(yīng)的無序集合求交集) opsForSet.intersect(key, otherKeys);//獲取多個(gè)集合的交集(Collection var2) opsForSet.intersectAndStore(key, otherKey, destKey); //key集合與otherKey集合的交集存儲(chǔ)到destKey集合中(其中otherKey可以為單個(gè)值或者集合) opsForSet.intersectAndStore(key, otherKeys, destKey); //key集合與多個(gè)集合的交集存儲(chǔ)到destKey無序集合中 opsForSet.union(key, otherKeys); //獲取兩個(gè)或者多個(gè)集合的并集(otherKeys可以為單個(gè)值或者是集合) opsForSet.unionAndStore(key, otherKey, destKey); //key集合與otherKey集合的并集存儲(chǔ)到destKey中(otherKeys可以為單個(gè)值或者是集合) opsForSet.difference(key, otherKeys); //獲取兩個(gè)或者多個(gè)集合的差集(otherKeys可以為單個(gè)值或者是集合) opsForSet.differenceAndStore(key, otherKey, destKey); //差集存儲(chǔ)到destKey中(otherKeys可以為單個(gè)值或者集合) opsForSet.randomMember(key); //隨機(jī)獲取集合中的一個(gè)元素 opsForSet.members(key); //獲取集合中的所有元素 opsForSet.randomMembers(key, count); //隨機(jī)獲取集合中count個(gè)元素 opsForSet.distinctRandomMembers(key, count); //獲取多個(gè)key無序集合中的元素(去重),count表示個(gè)數(shù) opsForSet.scan(key, options); //遍歷set類似于Interator(ScanOptions.NONE為顯示所有的)
6.zSet類型
ZSetOperations提供了一系列方法對(duì)有序集合進(jìn)行操作 ZSetOperations opsForZSet = redisTemplate.opsForZSet(); opsForZSet.add(key, value, score); //添加元素(有序集合是按照元素的score值由小到大進(jìn)行排列) opsForZSet.remove(key, values); //刪除對(duì)應(yīng)的value,value可以為多個(gè)值 opsForZSet.incrementScore(key, value, delta); //增加元素的score值,并返回增加后的值 opsForZSet.rank(key, value); //返回元素在集合的排名,有序集合是按照元素的score值由小到大排列 opsForZSet.reverseRank(key, value); //返回元素在集合的排名,按元素的score值由大到小排列 opsForZSet.reverseRangeWithScores(key, start,end); //獲取集合中給定區(qū)間的元素(start 開始位置,end 結(jié)束位置, -1查詢所有) opsForZSet.reverseRangeByScore(key, min, max); //按照Score值查詢集合中的元素,結(jié)果從小到大排序 opsForZSet.reverseRangeByScoreWithScores(key, min, max); //返回值為:Set<ZSetOperations.TypedTuple<V>> opsForZSet.count(key, min, max); //根據(jù)score值獲取集合元素?cái)?shù)量 opsForZSet.size(key); //獲取集合的大小 opsForZSet.zCard(key); //獲取集合的大小 opsForZSet.score(key, value); //獲取集合中key、value元素對(duì)應(yīng)的score值 opsForZSet.removeRange(key, start, end); //移除指定索引位置處的成員 opsForZSet.removeRangeByScore(key, min, max); //移除指定score范圍的集合成員 opsForZSet.unionAndStore(key, otherKey, destKey);//獲取key和otherKey的并集并存儲(chǔ)在destKey中(其中otherKeys可以為單個(gè)字符串或者字符串集合) opsForZSet.intersectAndStore(key, otherKey, destKey); //獲取key和otherKey的交集并存儲(chǔ)在destKey中(其中otherKeys可以為單個(gè)字符串或者字符串集合)
到此這篇關(guān)于RedisTemplate常用方法大全(面試必備)的文章就介紹到這了,更多相關(guān)RedisTemplate常用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Redis實(shí)現(xiàn)JWT令牌主動(dòng)失效機(jī)制
JWT是一種輕量級(jí)的身份驗(yàn)證和授權(quán)機(jī)制,它是一種 JSON 格式的數(shù)據(jù)串,通常用于客戶端和服務(wù)端之間的單點(diǎn)登錄(Single Sign-On, SSO)場(chǎng)景,本文給大家介紹了如何使用Redis來實(shí)現(xiàn)JWT令牌主動(dòng)失效機(jī)制,需要的朋友可以參考下2024-08-08使用Redis實(shí)現(xiàn)微信步數(shù)排行榜功能
這篇文章主要介紹了使用Redis實(shí)現(xiàn)微信步數(shù)排行榜功能,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06利用Redis如何實(shí)現(xiàn)自動(dòng)補(bǔ)全功能
這篇文章主要給大家介紹了關(guān)于如何利用Redis如何實(shí)現(xiàn)自動(dòng)補(bǔ)全功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Redis?key的過期時(shí)間和永久有效的實(shí)現(xiàn)
在Redis中,鍵可以設(shè)置過期時(shí)間或被永久保存,`EXPIRE`和`PEXPIRE`命令分別用于設(shè)置鍵的過期時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09