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

