SpringBoot整合redis
Spring Data Redis介紹
Spring Data Redis 是更大的 Spring Data 系列的一部分,它提供了從 Spring 應(yīng)用程序?qū)?Redis 的輕松配置和訪問。它提供了與商店交互的低級(jí)和高級(jí)抽象,使用戶擺脫了對(duì)基礎(chǔ)設(shè)施的擔(dān)憂。
SpringBoot2.x版本,其內(nèi)置的Redis中間件再也不是Jedis了,而是換成了lettuce。
lettuce: Lettuce 是 一種可伸縮,線程安全,完全非阻塞的Redis客戶端,多個(gè)線程可以共享一個(gè)RedisConnection,它利用Netty NIO 框架來(lái)高效地管理多個(gè)連接,從而提供了異步和同步數(shù)據(jù)訪問方式,用于構(gòu)建非阻塞的反應(yīng)性應(yīng)用程序。
Jedis: Jedis 在實(shí)現(xiàn)上是直連 redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個(gè) redis實(shí)例增加 物理連接。 這種方式更加類似于我們 BIO 一條線程連一個(gè)客戶端,并且是阻塞式的,會(huì)一直連接著客戶端等待客戶端的命令
官方文檔:https://docs.spring.io/spring-data/redis/docs/current/reference/html/#reference
引依賴
寫配置-對(duì)于連接的管理
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=0 #超時(shí)時(shí)間 spring.redis.timeout= 1800000 # 連接池最大連接數(shù) spring.redis.lettuce.pool.max-active= 20 # 最大阻塞等待時(shí)間 spring.redis.lettuce.pool.max-wait=-1 # 連接池的最大空閑連接 spring.redis.lettuce.pool.max-idle=5 # 連接池的最小空閑連接 spring.redis.lettuce.pool.min-idle=0
配置類-做序列化的設(shè)置
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * redis配置類,對(duì)key,value進(jìn)行序列化 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/5/6 - 16:49 */ @EnableCaching @Configuration public class RedisConfig { @Bean public RedisTemplate
簡(jiǎn)單使用redis
package com.promsing.redis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 測(cè)試redis的操作 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/5/6 - 16:56 */ @RestController @RequestMapping("redis") public class RedisController { @Autowired private RedisTemplate redisTemplate; @GetMapping public String saveRedis(){ redisTemplate.opsForValue().set("springboot:redis:name","promsing"); Object o = redisTemplate.opsForValue().get("springboot:redis:name"); System.out.println("redis中存儲(chǔ)的內(nèi)容"+o); return o.toString(); } }
其他測(cè)試方法
@RunWith(SpringRunner.class) @SpringBootTest(classes = UserApplication.class) public class RedisTest { @Autowired private StringRedisTemplate redisTemplate;//存儲(chǔ)的數(shù)據(jù)進(jìn)行了序列化,建議使用它 private RedisTemplate redisTemplate2;//存儲(chǔ)的數(shù)據(jù)沒有給序列化 @Test public void testRedis() { // 存儲(chǔ)數(shù)據(jù) this.redisTemplate.opsForValue().set("key2", "value2"); // 獲取數(shù)據(jù) String val = this.redisTemplate.opsForValue().get("key2"); System.out.println("val = " + val); } @Test public void testRedis2() { // 存儲(chǔ)數(shù)據(jù),并指定剩余生命時(shí)間,5小時(shí) this.redisTemplate.opsForValue().set("key2", "value2", 5, TimeUnit.HOURS); } @Test public void testHash() { BoundHashOperations
Redis Spring Boot
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。