RedisTemplate常用方法總結(jié)
Redis常用的數(shù)據(jù)類型
String
Hash
List
Set
zSet
Sorted set
String類型
判斷是否有key所對應(yīng)的值,有則返回true,沒有則返回false
redisTemplate.hasKey(key)
有則取出key值所對應(yīng)的值
redisTemplate.opsForValue().get(key)
刪除單個(gè)key值
redisTemplate.delete(key)
批量刪除key
redisTemplate.delete(keys)?//其中keys:Collection?keys
將當(dāng)前傳入的key值序列化為byte[]類型
redisTemplate.dump(key)
設(shè)置過期時(shí)間
public?Boolean?expire(String?key,?long?timeout,?TimeUnit?unit)?{
????return?redisTemplate.expire(key,?timeout,?unit);
?}
public?Boolean?expireAt(String?key,?Date?date)?{
????return?redisTemplate.expireAt(key,?date);
??}
查找匹配的key值,返回一個(gè)Set集合類型
public?Set<String>?getPatternKey(String?pattern)?{
????return?redisTemplate.keys(pattern);
}
修改redis中key的名稱
public?void?renameKey(String?oldKey,?String?newKey)?{
????redisTemplate.rename(oldKey,?newKey);
}
返回傳入key所存儲的值的類型
public?DataType?getKeyType(String?key)?{
????return?redisTemplate.type(key);
}
如果舊值存在時(shí),將舊值改為新值
public?Boolean?renameOldKeyIfAbsent(String?oldKey,?String?newKey)?{
????return?redisTemplate.renameIfAbsent(oldKey,?newKey);
}
從redis中隨機(jī)取出一個(gè)key
redisTemplate.randomKey()
返回當(dāng)前key所對應(yīng)的剩余過期時(shí)間
?public?Long?getExpire(String?key)?{
????return?redisTemplate.getExpire(key);
}
返回剩余過期時(shí)間并且指定時(shí)間單位
public?Long?getExpire(String?key,?TimeUnit?unit)?{
????return?redisTemplate.getExpire(key,?unit);
}
將key持久化保存
public?Boolean?persistKey(String?key)?{
????return?redisTemplate.persist(key);
}
將當(dāng)前數(shù)據(jù)庫的key移動到指定redis中數(shù)據(jù)庫當(dāng)中
public?Boolean?moveToDbIndex(String?key,?int?dbIndex)?{
????return?redisTemplate.move(key,?dbIndex);
}
設(shè)置當(dāng)前的key以及value值
redisTemplate.opsForValue().set(key,?value)
設(shè)置當(dāng)前的key以及value值并且設(shè)置過期時(shí)間
redisTemplate.opsForValue().set(key,?value,?timeout,?unit)
返回key中字符串的子字符
public?String?getCharacterRange(String?key,?long?start,?long?end)?{
????return?redisTemplate.opsForValue().get(key,?start,?end);
}
將舊的key設(shè)置為value,并且返回舊的key
public?String?setKeyAsValue(String?key,?String?value)?{
????return?redisTemplate.opsForValue().getAndSet(key,?value);
}
批量獲取值
?public?List<String>?multiGet(Collection<String>?keys)?{
????return?redisTemplate.opsForValue().multiGet(keys);
?}
在原有的值基礎(chǔ)上新增字符串到末尾
redisTemplate.opsForValue().append(key,?value)
以增量的方式將double值存儲在變量中
?public?Double?incrByDouble(String?key,?double?increment)?{
????return?redisTemplate.opsForValue().increment(key,?increment);
?}
通過increment(K key, long delta)方法以增量方式存儲long值(正值則自增,負(fù)值則自減)
public?Long?incrBy(String?key,?long?increment)?{
????return?redisTemplate.opsForValue().increment(key,?increment);
}
如果對應(yīng)的map集合名稱不存在,則添加否則不做修改
Map?valueMap?=?new?HashMap();??
valueMap.put("valueMap1","map1");??
valueMap.put("valueMap2","map2");??
valueMap.put("valueMap3","map3");??
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);?
設(shè)置map集合到redis
Map?valueMap?=?new?HashMap();??
valueMap.put("valueMap1","map1");??
valueMap.put("valueMap2","map2");??
valueMap.put("valueMap3","map3");??
redisTemplate.opsForValue().multiSet(valueMap);??
獲取字符串的長度
redisTemplate.opsForValue().size(key)
用 value 參數(shù)覆寫給定 key 所儲存的字符串值,從偏移量 offset 開始
redisTemplate.opsForValue().set(key,?value,?offset)
重新設(shè)置key對應(yīng)的值,如果存在返回false,否則返回true
redisTemplate.opsForValue().setIfAbsent(key,?value)
將值 value 關(guān)聯(lián)到 key,并將 key 的過期時(shí)間設(shè)為 timeout
redisTemplate.opsForValue().set(key,?value,?timeout,?unit)
將二進(jìn)制第offset位值變?yōu)関alue
redisTemplate.opsForValue().setBit(key,?offset,?value)
對key所儲存的字符串值,獲取指定偏移量上的位(bit)
redisTemplate.opsForValue().getBit(key,?offset)
Hash類型
Redis hash 是一個(gè)string類型的field和value的映射表,hash特別適合用于存儲對象。
Redis 中每個(gè) hash 可以存儲 2^32 - 1 鍵值對(40多億)。
獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null。
redisTemplate.opsForHash().get(key,?field)
獲取變量中的鍵值對
public?Map<Object,?Object>?hGetAll(String?key)?{
????return?redisTemplate.opsForHash().entries(key);
}
新增hashMap值
redisTemplate.opsForHash().put(key,?hashKey,?value)
以map集合的形式添加鍵值對
public?void?hPutAll(String?key,?Map<String,?String>?maps)?{
????redisTemplate.opsForHash().putAll(key,?maps);
}
僅當(dāng)hashKey不存在時(shí)才設(shè)置
public?Boolean?hashPutIfAbsent(String?key,?String?hashKey,?String?value)?{
????return?redisTemplate.opsForHash().putIfAbsent(key,?hashKey,?value);
}
刪除一個(gè)或者多個(gè)hash表字段
public?Long?hashDelete(String?key,?Object...?fields)?{
????return?redisTemplate.opsForHash().delete(key,?fields);
}
查看hash表中指定字段是否存在
public?boolean?hashExists(String?key,?String?field)?{
????return?redisTemplate.opsForHash().hasKey(key,?field);
}
給哈希表key中的指定字段的整數(shù)值加上增量increment
public?Long?hashIncrBy(String?key,?Object?field,?long?increment)?{
????return?redisTemplate.opsForHash().increment(key,?field,?increment);
}
?public?Double?hIncrByDouble(String?key,?Object?field,?double?delta)?{
????return?redisTemplate.opsForHash().increment(key,?field,?delta);
}
獲取所有hash表中字段
redisTemplate.opsForHash().keys(key)
獲取hash表中字段的數(shù)量
redisTemplate.opsForHash().size(key)
獲取hash表中存在的所有的值
public?List<Object>?hValues(String?key)?{
????return?redisTemplate.opsForHash().values(key);
}
匹配獲取鍵值對,ScanOptions.NONE為獲取全部鍵對
public?CursorObject,?Object>>?hashScan(String?key,?ScanOptions?options)?{
????return?redisTemplate.opsForHash().scan(key,?options);
}
List類型
通過索引獲取列表中的元素
redisTemplate.opsForList().index(key,?index)
獲取列表指定范圍內(nèi)的元素(start開始位置, 0是開始位置,end 結(jié)束位置, -1返回所有)
redisTemplate.opsForList().range(key,?start,?end)
存儲在list的頭部,即添加一個(gè)就把它放在最前面的索引處
redisTemplate.opsForList().leftPush(key,?value)
把多個(gè)值存入List中(value可以是多個(gè)值,也可以是一個(gè)Collection value)
redisTemplate.opsForList().leftPushAll(key,?value)
List存在的時(shí)候再加入
redisTemplate.opsForList().leftPushIfPresent(key,?value)
如果pivot處值存在則在pivot前面添加
redisTemplate.opsForList().leftPush(key,?pivot,?value)
按照先進(jìn)先出的順序來添加(value可以是多個(gè)值,或者是Collection var2)
redisTemplate.opsForList().rightPush(key,?value)
redisTemplate.opsForList().rightPushAll(key,?value)
在pivot元素的右邊添加值
redisTemplate.opsForList().rightPush(key,?pivot,?value)
設(shè)置指定索引處元素的值
redisTemplate.opsForList().set(key,?index,?value)
移除并獲取列表中第一個(gè)元素(如果列表沒有元素會阻塞列表直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止)
redisTemplate.opsForList().leftPop(key)
redisTemplate.opsForList().leftPop(key,?timeout,?unit)
移除并獲取列表最后一個(gè)元素
redisTemplate.opsForList().rightPop(key)
redisTemplate.opsForList().rightPop(key,?timeout,?unit)
從一個(gè)隊(duì)列的右邊彈出一個(gè)元素并將這個(gè)元素放入另一個(gè)指定隊(duì)列的最左邊
redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,?destinationKey)
redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,?destinationKey,?timeout,?unit)
刪除集合中值等于value的元素(index=0, 刪除所有值等于value的元素; index>0, 從頭部開始刪除第一個(gè)值等于value的元素; index<0, 從尾部開始刪除第一個(gè)值等于value的元素)
redisTemplate.opsForList().remove(key,?index,?value)
將List列表進(jìn)行剪裁
redisTemplate.opsForList().trim(key,?start,?end)
獲取當(dāng)前key的List列表長度
redisTemplate.opsForList().size(key)
Set類型
添加元素
redisTemplate.opsForSet().add(key,?values)
移除元素(單個(gè)值、多個(gè)值)
redisTemplate.opsForSet().remove(key,?values)
刪除并且返回一個(gè)隨機(jī)的元素
redisTemplate.opsForSet().pop(key)
獲取集合的大小
redisTemplate.opsForSet().size(key)
判斷集合是否包含value
redisTemplate.opsForSet().isMember(key,?value)
獲取兩個(gè)集合的交集(key對應(yīng)的無序集合與otherKey對應(yīng)的無序集合求交集)
redisTemplate.opsForSet().intersect(key,?otherKey)
獲取多個(gè)集合的交集(Collection var2)
redisTemplate.opsForSet().intersect(key,?otherKeys)
key集合與otherKey集合的交集存儲到destKey集合中(其中otherKey可以為單個(gè)值或者集合)
redisTemplate.opsForSet().intersectAndStore(key,?otherKey,?destKey)
key集合與多個(gè)集合的交集存儲到destKey無序集合中
redisTemplate.opsForSet().intersectAndStore(key,?otherKeys,?destKey)
獲取兩個(gè)或者多個(gè)集合的并集(otherKeys可以為單個(gè)值或者是集合)
redisTemplate.opsForSet().union(key,?otherKeys)
key集合與otherKey集合的并集存儲到destKey中(otherKeys可以為單個(gè)值或者是集合)
redisTemplate.opsForSet().unionAndStore(key,?otherKey,?destKey)
獲取兩個(gè)或者多個(gè)集合的差集(otherKeys可以為單個(gè)值或者是集合)
redisTemplate.opsForSet().difference(key,?otherKeys)
差集存儲到destKey中(otherKeys可以為單個(gè)值或者集合)
redisTemplate.opsForSet().differenceAndStore(key,?otherKey,?destKey)
隨機(jī)獲取集合中的一個(gè)元素
redisTemplate.opsForSet().randomMember(key)
獲取集合中的所有元素
redisTemplate.opsForSet().members(key)
隨機(jī)獲取集合中count個(gè)元素
redisTemplate.opsForSet().randomMembers(key,?count)
獲取多個(gè)key無序集合中的元素(去重),count表示個(gè)數(shù)
redisTemplate.opsForSet().distinctRandomMembers(key,?count)
遍歷set類似于Interator(ScanOptions.NONE為顯示所有的)
redisTemplate.opsForSet().scan(key,?options)
zSet類型
ZSetOperations提供了一系列方法對有序集合進(jìn)行操作
添加元素(有序集合是按照元素的score值由小到大進(jìn)行排列)
redisTemplate.opsForZSet().add(key,?value,?score)
刪除對應(yīng)的value,value可以為多個(gè)值
redisTemplate.opsForZSet().remove(key,?values)
增加元素的score值,并返回增加后的值
redisTemplate.opsForZSet().incrementScore(key,?value,?delta)
返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
redisTemplate.opsForZSet().rank(key,?value)
返回元素在集合的排名,按元素的score值由大到小排列
redisTemplate.opsForZSet().reverseRank(key,?value)
獲取集合中給定區(qū)間的元素(start 開始位置,end 結(jié)束位置, -1查詢所有)
redisTemplate.opsForZSet().reverseRangeWithScores(key,?start,end)
按照Score值查詢集合中的元素,結(jié)果從小到大排序
redisTemplate.opsForZSet().reverseRangeByScore(key,?min,?max)
redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,?min,?max)
//返回值為:Set>
從高到低的排序集中獲取分?jǐn)?shù)在最小和最大值之間的元素
redisTemplate.opsForZSet().reverseRangeByScore(key,?min,?max,?start,?end)
根據(jù)score值獲取集合元素?cái)?shù)量
redisTemplate.opsForZSet().count(key,?min,?max)
獲取集合的大小
redisTemplate.opsForZSet().size(key)
redisTemplate.opsForZSet().zCard(key)
獲取集合中key、value元素對應(yīng)的score值
redisTemplate.opsForZSet().score(key,?value)
移除指定索引位置處的成員
redisTemplate.opsForZSet().removeRange(key,?start,?end)
移除指定score范圍的集合成員
redisTemplate.opsForZSet().removeRangeByScore(key,?min,?max)
獲取key和otherKey的并集并存儲在destKey中(其中otherKeys可以為單個(gè)字符串或者字符串集合)
redisTemplate.opsForZSet().unionAndStore(key,?otherKey,?destKey)
獲取key和otherKey的交集并存儲在destKey中(其中otherKeys可以為單個(gè)字符串或者字符串集合)
redisTemplate.opsForZSet().intersectAndStore(key,?otherKey,?destKey)
遍歷集合(和iterator一模一樣)
CursorObject>>?scan?=?opsForZSet.scan("test3",?ScanOptions.NONE);
????????while?(scan.hasNext()){
????????????ZSetOperations.TypedTuple<Object>?item?=?scan.next();
????????????System.out.println(item.getValue()?+?":"?+?item.getScore());
????????}
