SpringBoot整合Redis的示例
redis是最常用的緩存數(shù)據(jù)庫(kù),常用于存儲(chǔ)用戶登錄token、臨時(shí)數(shù)據(jù)、定時(shí)相關(guān)數(shù)據(jù)等。
redis是單線程的,所以redis的操作是原子性的,這樣可以保證不會(huì)出現(xiàn)并發(fā)問(wèn)題。
redis基于內(nèi)存,速度非常快,據(jù)測(cè)試,redis讀的速度是110000次/s,寫(xiě)的速度是81000次/s
本節(jié)介紹SpringBoot引入redis,以及使用RedisTemplate來(lái)操作redis數(shù)據(jù)。
采用SpringBoot 2.1.9.RELEASE,對(duì)應(yīng)示例代碼在:https://github.com/laolunsi/spring-boot-examples/tree/master/07-spring-boot-redis-demo
一、A Simple Demo-使用SpringBoot連接redis
maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
yml:
server: port: 8867spring: redis: host: localhost port: 6379 #password: ’’ database: 6
測(cè)試類(lèi):
@SpringBootTest@RunWith(SpringRunner.class)public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void testRedis() { String key = 'hello'; redisTemplate.opsForValue().set('hello', '你好'); String res = (String) redisTemplate.opsForValue().get(key); System.out.println(res); }}
執(zhí)行結(jié)果:
看一下redis:
這里存在一個(gè)問(wèn)題:默認(rèn)的存儲(chǔ)方式導(dǎo)致key在redis-manager里面顯示出來(lái)是亂碼的,并且存儲(chǔ)結(jié)果是二進(jìn)制了。這樣不利用我們查看redis里面的數(shù)據(jù)。
我們需要自定義redis存儲(chǔ)的序列化規(guī)則。
二、解決RedisTemplate默認(rèn)序列化的問(wèn)題
完善一下maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
定義RedisConfig類(lèi):
/** * redis配置 * 主要是配置Redis的序列化規(guī)則,替換默認(rèn)的jdkSerializer * key的序列化規(guī)則用StringRedisSerializer * value的序列化規(guī)則用Jackson2JsonRedisSerializer */@Configurationpublic class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); // 使用Jackson2JsonRedisSerialize替換默認(rèn)序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 設(shè)置key和value的序列化規(guī)則 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }}
刪除之前的key,重新執(zhí)行一下test方法:
下面來(lái)演示一下SpringBoot使用RedisTemplate進(jìn)行redis數(shù)據(jù)的操作
三、基于SpringBoot的redis操作——key/list/hash
RedisTemplate內(nèi)置redis操作如下:
這里主要展示value/hash/list三種用法:
3.1 RedisTemplate.opsForValue
@Test public void testKeyOps() { // 測(cè)試redis操作key-value形式 Set<String> keySet = new HashSet<>(); String key1 = 'name'; keySet.add(key1); // 存儲(chǔ)簡(jiǎn)單的key-value,并設(shè)置過(guò)期時(shí)間 redisTemplate.opsForValue().set(key1, 'eknown', 1, TimeUnit.MINUTES); String key2 = 'token:user1'; String key3 = 'token:user2'; keySet.add(key2); keySet.add(key3); // redisTemplate.opsForValue().set(key2, '{'name':'eknown'}, 'role':'admin''); redisTemplate.opsForValue().set(key3, '{'name':'test'}, 'role':'test''); // 根據(jù)key的集合獲取多個(gè)value List<String> valueList = redisTemplate.opsForValue().multiGet(keySet); for (String value : valueList) { System.out.println(value); } }
執(zhí)行結(jié)果:
redis中的數(shù)據(jù):
redis中的key顯示出了一個(gè)層級(jí)關(guān)系,這個(gè)小技巧對(duì)于實(shí)際項(xiàng)目有個(gè)非常好的作用:通過(guò)prefix:suffix這樣的形式,可以將redis中存儲(chǔ)的數(shù)據(jù)分出層級(jí)。
3.2 RedisTemplate.opsForHash
清空該database下的數(shù)據(jù),測(cè)試redisTemplate.opsForHash:
@Test public void testHashOps() { String key = 'hash'; // 單次往hash中存放一個(gè)數(shù)據(jù) redisTemplate.opsForHash().put(key, '1', '你好'); Map<String, Object> map = new HashMap<>(); map.put('2', 'hello'); map.put('3a', 'china1=2'); // 一次性向hash中存放一個(gè)map redisTemplate.opsForHash().putAll(key, map); // 獲取hash下的所有key和value Map<String, Object> resultMap = redisTemplate.opsForHash().entries(key); for (String hashKey : resultMap.keySet()) { System.out.println(hashKey + ': ' + resultMap.get(hashKey)); } }
執(zhí)行結(jié)果:
redis:
3.3 RedisTemplate.opsForList
@Test public void testListOps() { String listKey = 'list'; redisTemplate.opsForList().leftPush(listKey, 'first value'); // 從list最左邊插入數(shù)據(jù) redisTemplate.opsForList().leftPush(listKey, 'second value but left'); redisTemplate.opsForList().rightPush(listKey, 3); // 從list最右邊插入數(shù)據(jù) List<Object> list = new ArrayList<>(); list.add('hello'); list.add('http://www.eknown.cn'); list.add(23344); list.add(false); redisTemplate.opsForList().rightPushAll(listKey, list); // 從list右邊批量插入數(shù)據(jù) long size = redisTemplate.opsForList().size(listKey); if (size > 0) { for (int i = 0; i < size -1 ; i++) {// 從list最左邊開(kāi)始讀取list中的數(shù)據(jù),注意pop會(huì)導(dǎo)致出棧,也就是數(shù)據(jù)被取出來(lái)了(redis中就沒(méi)有這個(gè)值了)// 此處我們讀取size-1條數(shù)據(jù),僅留下最后一條數(shù)據(jù)System.out.println(i + ':' + redisTemplate.opsForList().leftPop(listKey).toString()); } } }
執(zhí)行上面的腳本,注意在最后的讀取list數(shù)據(jù)代碼前面加一個(gè)斷點(diǎn),此時(shí)redis中是這樣的:
放開(kāi)斷點(diǎn),程序繼續(xù)執(zhí)行,控制臺(tái)如下:
注意,此時(shí)redis中僅剩余最后一條數(shù)據(jù),這是由于pop的問(wèn)題,list中的數(shù)據(jù)被讀取并刪除了:
好了,這一節(jié)主要講了SpringBoot引入redis,以及使用redis的一些基本操作和相關(guān)技巧,在此基礎(chǔ)上,我們可以讓我們的項(xiàng)目變得更加快速、靈活!
以上就是SpringBoot整合Redis的示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Redis的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 如何利用python操作注冊(cè)表2. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 詳解瀏覽器的緩存機(jī)制6. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享7. xml中的空格之完全解說(shuō)8. 如何在jsp界面中插入圖片9. phpstudy apache開(kāi)啟ssi使用詳解10. JSP之表單提交get和post的區(qū)別詳解及實(shí)例
