Springboot如何操作redis數(shù)據(jù)
StringRedisTemplate與RedisTemplate區(qū)別點(diǎn)
兩者的關(guān)系是StringRedisTemplate繼承RedisTemplate。
兩者的數(shù)據(jù)是不共通的;也就是說(shuō)StringRedisTemplate只能管理StringRedisTemplate里面的數(shù)據(jù),RedisTemplate只能管理RedisTemplate中的數(shù)據(jù)。
其實(shí)他們兩者之間的區(qū)別主要在于他們使用的序列化類(lèi):
RedisTemplate使用的是JdkSerializationRedisSerializer 存入數(shù)據(jù)會(huì)將數(shù)據(jù)先序列化成字節(jié)數(shù)組然后在存入Redis數(shù)據(jù)庫(kù)。
StringRedisTemplate使用的是StringRedisSerializer
使用時(shí)注意事項(xiàng):
當(dāng)你的redis數(shù)據(jù)庫(kù)里面本來(lái)存的是字符串?dāng)?shù)據(jù)或者你要存取的數(shù)據(jù)就是字符串類(lèi)型數(shù)據(jù)的時(shí)候,那么你就使用
StringRedisTemplate即可。
但是如果你的數(shù)據(jù)是復(fù)雜的對(duì)象類(lèi)型,而取出的時(shí)候又不想做任何的數(shù)據(jù)轉(zhuǎn)換,直接從Redis里面取出一個(gè)對(duì)象,那么使用
RedisTemplate是更好的選擇。
RedisTemplate使用時(shí)常見(jiàn)問(wèn)題:
redisTemplate 中存取數(shù)據(jù)都是字節(jié)數(shù)組。當(dāng)redis中存入的數(shù)據(jù)是可讀形式而非字節(jié)數(shù)組時(shí),使用redisTemplate取值的時(shí)候會(huì)無(wú)法獲取導(dǎo)出數(shù)據(jù),獲得的值為null??梢允褂?StringRedisTemplate 試試。
RedisTemplate中定義了5種數(shù)據(jù)結(jié)構(gòu)操作
redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash(); //操作hash redisTemplate.opsForList(); //操作list redisTemplate.opsForSet(); //操作set redisTemplate.opsForZSet(); //操作有序setStringRedisTemplate常用操作
stringRedisTemplate.opsForValue().set('test', '100',60*10,TimeUnit.SECONDS);//向redis里存入數(shù)據(jù)和設(shè)置緩存時(shí)間 stringRedisTemplate.boundValueOps('test').increment(-1);//val做-1操作 stringRedisTemplate.opsForValue().get('test')//根據(jù)key獲取緩存中的val stringRedisTemplate.boundValueOps('test').increment(1);//val +1 stringRedisTemplate.getExpire('test')//根據(jù)key獲取過(guò)期時(shí)間 stringRedisTemplate.getExpire('test',TimeUnit.SECONDS)//根據(jù)key獲取過(guò)期時(shí)間并換算成指定單位 stringRedisTemplate.delete('test');//根據(jù)key刪除緩存 stringRedisTemplate.hasKey('546545');//檢查key是否存在,返回boolean值 stringRedisTemplate.opsForSet().add('red_123', '1','2','3');//向指定key中存放set集合 stringRedisTemplate.expire('red_123',1000 , TimeUnit.MILLISECONDS);//設(shè)置過(guò)期時(shí)間 stringRedisTemplate.opsForSet().isMember('red_123', '1')//根據(jù)key查看集合中是否存在指定數(shù)據(jù) stringRedisTemplate.opsForSet().members('red_123');//根據(jù)key獲取set集合StringRedisTemplate的使用
springboot中使用注解@Autowired 即可
@Autowiredpublic StringRedisTemplate stringRedisTemplate;
使用樣例:
@RestController@RequestMapping('/user')public class UserResource { private static final Logger log = LoggerFactory.getLogger(UserResource.class); @Autowired private UserService userService; @Autowired public StringRedisTemplate stringRedisTemplate; @RequestMapping('/num') public String countNum() { String userNum = stringRedisTemplate.opsForValue().get('userNum'); if(StringUtils.isNull(userNum)){ stringRedisTemplate.opsForValue().set('userNum', userService.countNum().toString()); } return userNum; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作2. Python 忽略文件名編碼的方法3. Java Media Framework 基礎(chǔ)教程4. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis5. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題6. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)7. 在Mac中配置Python虛擬環(huán)境過(guò)程解析8. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML9. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查10. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析
