RedisTemplate操作String及Hash數(shù)據(jù)方式
RedisTemplate用法
封裝自己的操作方法
1、單個(gè)key的刪除(我們可以是封裝自己的一個(gè)delete方法,然后將參數(shù)設(shè)置為key,在通過(guò)redisTemplate調(diào)用delete方法刪除)
redisTemplate.delete(key)
2、多個(gè)key的刪除(多個(gè)key刪除則和上面的單key一樣,只不過(guò)是在參數(shù)上設(shè)置為多個(gè)key的方式即可)
redisTemplate.delete(keys)
這里的keys是指的多參數(shù):public void deleteByKey(String ...keys)
3、指定key失效時(shí)間(設(shè)置失效時(shí)間,我們自己定義的方法設(shè)置三個(gè)參數(shù),分比為key,時(shí)間,單位《秒或分》)
redisTemplate.expir(key,time,TimeUnit.MINUTES)
4、根據(jù)key值獲取過(guò)期的時(shí)間(我們自己設(shè)置key參數(shù),然后通key獲取過(guò)期時(shí)間)
redisTemplate.getExpire(key)
5、判斷key是否已經(jīng)存在(經(jīng)常會(huì)用到的key是否存在,則和上面的方法類似,只是設(shè)置個(gè)key參數(shù)值)
redisTemplate.hasKey(key)
String 類型的操作
1、添加緩存
// 通過(guò)redisTemplate設(shè)置值
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);
//通過(guò)BoundValueOperations設(shè)置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.SECOND);
//通過(guò)ValueOperations設(shè)置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.SECOND);2、刪除緩存key
Boolean i = redisTemplate.delete(key)
3、順序遞增
redisTemplate.boundValueOps("key").increment(4L)4、順序遞減
redisTemplate.boundValueOps("key").increment(-4L)Hash 類型數(shù)據(jù)相關(guān)操作
1、添加我們的緩存數(shù)據(jù)
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");2、設(shè)置過(guò)期的時(shí)間
redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.SECOND);
redisTemplate.expire("HashKey",1,TimeUnit.SECOND);3、添加一個(gè)Map類型的數(shù)據(jù)
HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );4、提取所有的的小key值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中使用Preferences 的 API設(shè)置用戶偏好
這篇文章主要介紹了Java中使用Preferences 的 API設(shè)置用戶偏好的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置及獲取非確定名稱配置方法
在SpringBoot項(xiàng)目中,使用@ConfigurationProperties注解可獲取統(tǒng)一前綴的配置,具體做法是創(chuàng)建配置類,使用prefix屬性指定配置的前綴,本文給大家介紹SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置方法,感興趣的朋友跟隨小編一起看看吧2024-09-09
jmeter添加自定函數(shù)的實(shí)例(jmeter5.3+IntelliJ IDEA)
這篇文章主要介紹了jmeter添加自定函數(shù)的實(shí)例(jmeter5.3+IntelliJ IDEA),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

