RedisTemplate常用方法大全(面試必備)
更新時間:2024年05月12日 10:44:31 作者:逆流°只是風景-bjhxcc
RedisTemplate是SpringData Redis提供的一個類,本文主要介紹了RedisTemplate常用方法大全,具有一定的參考價值,感興趣的可以了解一下
Redis常用的數(shù)據(jù)類型:String、Hash、List、Set、zSet

1.RedisTemplate常用方法
redisTemplate.hasKey(key); //判斷是否有key所對應的值,有則返回true,沒有則返回false redisTemplate.opsForValue().get(key); //有則取出key值所對應的值 redisTemplate.delete(key); //刪除單個key值 redisTemplate.delete(keys); //其中keys:Collection<K> keys redisTemplate.dump(key); //將當前傳入的key值序列化為byte[]類型 redisTemplate.expire(key, timeout, unit); //設置過期時間 redisTemplate.expireAt(key, date); //設置過期時間 redisTemplate.keys(pattern); //查找匹配的key值,返回一個Set集合類型 redisTemplate.rename(oldKey, newKey); //返回傳入key所存儲的值的類型 redisTemplate.renameIfAbsent(oldKey, newKey); //如果舊值存在時,將舊值改為新值 redisTemplate.randomKey(); //從redis中隨機取出一個key redisTemplate.getExpire(key); //返回當前key所對應的剩余過期時間 redisTemplate.getExpire(key, unit); //返回剩余過期時間并且指定時間單位 redisTemplate.persist(key); //將key持久化保存 redisTemplate.move(key, dbIndex); //將當前數(shù)據(jù)庫的key移動到指定redis中數(shù)據(jù)庫當中
2.String類型
ValueOperations opsForValue = redisTemplate.opsForValue();
opsForValue.set(key, value); //設置當前的key以及value值
opsForValue.set(key, value, offset);//用 value 參數(shù)覆寫給定 key 所儲存的字符串值,從偏移量 offset 開始
opsForValue.set(key, value, timeout, unit); //設置當前的key以及value值并且設置過期時間
opsForValue.setBit(key, offset, value); //將二進制第offset位值變?yōu)関alue
opsForValue.setIfAbsent(key, value);//重新設置key對應的值,如果存在返回false,否則返回true
opsForValue.get(key, start, end); //返回key中字符串的子字符
opsForValue.getAndSet(key, value); //將舊的key設置為value,并且返回舊的key
opsForValue.multiGet(keys); //批量獲取值
opsForValue.size(key); //獲取字符串的長度
opsForValue.append(key, value); //在原有的值基礎上新增字符串到末尾
opsForValue.increment(key,double increment);//以增量的方式將double值存儲在變量中
opsForValue.increment(key,long increment); //通過increment(K key, long delta)方法以增量方式存儲long值(正值則自增,負值則自減)
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
opsForValue.multiSetIfAbsent(valueMap); //如果對應的map集合名稱不存在,則添加否則不做修改
opsForValue.multiSet(valueMap); //設置map集合到redis
3.Hash類型
HashOperations opsForHash = redisTemplate.opsForHash(); opsForHash.get(key, field); //獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null opsForHash.entries(key); //獲取變量中的鍵值對 opsForHash.put(key, hashKey, value); //新增hashMap值 opsForHash.putAll(key, maps); //以map集合的形式添加鍵值對 opsForHash.putIfAbsent(key, hashKey, value); //僅當hashKey不存在時才設置 opsForHash.delete(key, fields); //刪除一個或者多個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); //匹配獲取鍵值對,ScanOptions.NONE為獲取全部鍵對
4.List類型
ListOperations opsForList = redisTemplate.opsForList(); opsForList.index(key, index); //通過索引獲取列表中的元素 opsForList.range(key, start, end); //獲取列表指定范圍內的元素(start開始位置, 0是開始位置,end 結束位置, -1返回所有) opsForList.leftPush(key, value); //存儲在list的頭部,即添加一個就把它放在最前面的索引處 opsForList.leftPush(key, pivot, value); //如果pivot處值存在則在pivot前面添加 opsForList.leftPushAll(key, value); //把多個值存入List中(value可以是多個值,也可以是一個Collection value) opsForList.leftPushIfPresent(key, value); //List存在的時候再加入 opsForList.rightPush(key, value); //按照先進先出的順序來添加(value可以是多個值,或者是Collection var2) opsForList.rightPushAll(key, value); //在pivot元素的右邊添加值 opsForList.set(key, index, value); //設置指定索引處元素的值 opsForList.trim(key, start, end); //將List列表進行剪裁 opsForList.size(key); //獲取當前key的List列表長度 //移除并獲取列表中第一個元素(如果列表沒有元素會阻塞列表直到等待超時或發(fā)現(xiàn)可彈出元素為止) opsForList.leftPop(key); opsForList.leftPop(key, timeout, unit); //移除并獲取列表最后一個元素 opsForList.rightPop(key); opsForList.rightPop(key, timeout, unit); //從一個隊列的右邊彈出一個元素并將這個元素放入另一個指定隊列的最左邊 opsForList.rightPopAndLeftPush(sourceKey, destinationKey); opsForList.rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit); //刪除集合中值等于value的元素(index=0, 刪除所有值等于value的元素; index>0, 從頭部開始刪除第一個值等于value的元素; index<0, 從尾部開始刪除第一個值等于value的元素) opsForList.remove(key, index, value);
5.Set類型
SetOperations opsForSet = redisTemplate.opsForSet(); opsForSet.add(key, values); //添加元素 opsForSet.remove(key, values); //移除元素(單個值、多個值) opsForSet.pop(key); //刪除并且返回一個隨機的元素 opsForSet.size(key); //獲取集合的大小 opsForSet.isMember(key, value); //判斷集合是否包含value opsForSet.intersect(key, otherKey); //獲取兩個集合的交集(key對應的無序集合與otherKey對應的無序集合求交集) opsForSet.intersect(key, otherKeys);//獲取多個集合的交集(Collection var2) opsForSet.intersectAndStore(key, otherKey, destKey); //key集合與otherKey集合的交集存儲到destKey集合中(其中otherKey可以為單個值或者集合) opsForSet.intersectAndStore(key, otherKeys, destKey); //key集合與多個集合的交集存儲到destKey無序集合中 opsForSet.union(key, otherKeys); //獲取兩個或者多個集合的并集(otherKeys可以為單個值或者是集合) opsForSet.unionAndStore(key, otherKey, destKey); //key集合與otherKey集合的并集存儲到destKey中(otherKeys可以為單個值或者是集合) opsForSet.difference(key, otherKeys); //獲取兩個或者多個集合的差集(otherKeys可以為單個值或者是集合) opsForSet.differenceAndStore(key, otherKey, destKey); //差集存儲到destKey中(otherKeys可以為單個值或者集合) opsForSet.randomMember(key); //隨機獲取集合中的一個元素 opsForSet.members(key); //獲取集合中的所有元素 opsForSet.randomMembers(key, count); //隨機獲取集合中count個元素 opsForSet.distinctRandomMembers(key, count); //獲取多個key無序集合中的元素(去重),count表示個數(shù) opsForSet.scan(key, options); //遍歷set類似于Interator(ScanOptions.NONE為顯示所有的)
6.zSet類型
ZSetOperations提供了一系列方法對有序集合進行操作 ZSetOperations opsForZSet = redisTemplate.opsForZSet(); opsForZSet.add(key, value, score); //添加元素(有序集合是按照元素的score值由小到大進行排列) opsForZSet.remove(key, values); //刪除對應的value,value可以為多個值 opsForZSet.incrementScore(key, value, delta); //增加元素的score值,并返回增加后的值 opsForZSet.rank(key, value); //返回元素在集合的排名,有序集合是按照元素的score值由小到大排列 opsForZSet.reverseRank(key, value); //返回元素在集合的排名,按元素的score值由大到小排列 opsForZSet.reverseRangeWithScores(key, start,end); //獲取集合中給定區(qū)間的元素(start 開始位置,end 結束位置, -1查詢所有) opsForZSet.reverseRangeByScore(key, min, max); //按照Score值查詢集合中的元素,結果從小到大排序 opsForZSet.reverseRangeByScoreWithScores(key, min, max); //返回值為:Set<ZSetOperations.TypedTuple<V>> opsForZSet.count(key, min, max); //根據(jù)score值獲取集合元素數(shù)量 opsForZSet.size(key); //獲取集合的大小 opsForZSet.zCard(key); //獲取集合的大小 opsForZSet.score(key, value); //獲取集合中key、value元素對應的score值 opsForZSet.removeRange(key, start, end); //移除指定索引位置處的成員 opsForZSet.removeRangeByScore(key, min, max); //移除指定score范圍的集合成員 opsForZSet.unionAndStore(key, otherKey, destKey);//獲取key和otherKey的并集并存儲在destKey中(其中otherKeys可以為單個字符串或者字符串集合) opsForZSet.intersectAndStore(key, otherKey, destKey); //獲取key和otherKey的交集并存儲在destKey中(其中otherKeys可以為單個字符串或者字符串集合)
到此這篇關于RedisTemplate常用方法大全(面試必備)的文章就介紹到這了,更多相關RedisTemplate常用方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

