SpringBoot整合Redis的示例
redis是最常用的緩存數(shù)據(jù)庫,常用于存儲(chǔ)用戶登錄token、臨時(shí)數(shù)據(jù)、定時(shí)相關(guān)數(shù)據(jù)等。
redis是單線程的,所以redis的操作是原子性的,這樣可以保證不會(huì)出現(xiàn)并發(fā)問題。
redis基于內(nèi)存,速度非???,據(jù)測(cè)試,redis讀的速度是110000次/s,寫的速度是81000次/s
本節(jié)介紹SpringBoot引入redis,以及使用RedisTemplate來操作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: 8867 spring: redis: host: localhost port: 6379 #password: '' database: 6
測(cè)試類:
@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è)問題:默認(rèn)的存儲(chǔ)方式導(dǎo)致key在redis-manager里面顯示出來是亂碼的,并且存儲(chǔ)結(jié)果是二進(jìn)制了。這樣不利用我們查看redis里面的數(shù)據(jù)。
我們需要自定義redis存儲(chǔ)的序列化規(guī)則。
二、解決RedisTemplate默認(rè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類:
/** * redis配置 * 主要是配置Redis的序列化規(guī)則,替換默認(rèn)的jdkSerializer * key的序列化規(guī)則用StringRedisSerializer * value的序列化規(guī)則用Jackson2JsonRedisSerializer */ @Configuration public 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方法:
下面來演示一下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è)置過期時(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è)非常好的作用:通過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最左邊開始讀取list中的數(shù)據(jù),注意pop會(huì)導(dǎo)致出棧,也就是數(shù)據(jù)被取出來了(redis中就沒有這個(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中是這樣的:
放開斷點(diǎn),程序繼續(xù)執(zhí)行,控制臺(tái)如下:
注意,此時(shí)redis中僅剩余最后一條數(shù)據(jù),這是由于pop的問題,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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Security 自動(dòng)踢掉前一個(gè)登錄用戶的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Security 自動(dòng)踢掉前一個(gè)登錄用戶的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Spring Boot 之HelloWorld開發(fā)案例
這篇文章主要介紹了Spring Boot 之HelloWorld開發(fā)案例,需要的朋友可以參考下2017-04-04java 反射getClass .class 的使用方法示例
這篇文章主要介紹了java 反射getClass .class 的使用方法,結(jié)合實(shí)例形式分析了java類反射機(jī)制的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11Intellij idea下使用不同tomcat編譯maven項(xiàng)目的服務(wù)器路徑方法詳解
今天小編就為大家分享一篇關(guān)于Intellij idea下使用不同tomcat編譯maven項(xiàng)目的服務(wù)器路徑方法詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(概要分析)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02