溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot中怎么使用集中式緩存Redis

發(fā)布時(shí)間:2022-02-25 10:27:57 來(lái)源:億速云 閱讀:185 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“Spring Boot中怎么使用集中式緩存Redis”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

動(dòng)手試試

User實(shí)體的定義

@Entity @Data @NoArgsConstructor public class User implements Serializable {

@Id
    @GeneratedValue
    private Long id;


    private String name;
    private Integer age;


    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

User實(shí)體的數(shù)據(jù)訪問(wèn)實(shí)現(xiàn)(涵蓋了緩存注解)

@CacheConfig(cacheNames = "users") public interface UserRepository extends JpaRepository<User, Long> {

@Cacheable
    User findByName(String name);


}

(推薦課程:Spring教程)

下面開(kāi)始改造這個(gè)項(xiàng)目:

第一步pom.xml中增加相關(guān)依賴:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

Spring Boot 1.x的早期版本中,該依賴的名稱為spring-boot-starter-redis,所以在Spring Boot 1.x基礎(chǔ)教程中與這里不同。

第二步:配置文件中增加配置信息,以本地運(yùn)行為例,比如:

spring.redis.host=localhost spring.redis.port=6379 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.shutdown-timeout=100ms

關(guān)于連接池的配置,需要注意:

Redis的連接池配置在 1.x 版本中前綴為spring.redis.poolSpring Boot 2.x有所不同。在 1.x 版本中采用jedis作為連接池,而在 2.x 版本中采用了lettuce作為連接池以上配置均為默認(rèn)值,實(shí)際上生產(chǎn)需進(jìn)一步根據(jù)部署情況與業(yè)務(wù)要求做適當(dāng)修改.

再來(lái)試試單元測(cè)試:

@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class Chapter54ApplicationTests {

@Autowired
    private UserRepository userRepository;


    @Autowired
    private CacheManager cacheManager;


    @Test
    public void test() throws Exception {
        System.out.println("CacheManager type : " + cacheManager.getClass());


        // 創(chuàng)建1條記錄
        userRepository.save(new User("AAA", 10));


        User u1 = userRepository.findByName("AAA");
        System.out.println("第一次查詢:" + u1.getAge());


        User u2 = userRepository.findByName("AAA");
        System.out.println("第二次查詢:" + u2.getAge());
    }


}

執(zhí)行測(cè)試輸出可以得到:

CacheManager type : class org.springframework.data.redis.cache.RedisCacheManager Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where nextval=? Hibernate: insert into user (age, name, id) values (?, ?, ?) 2020-08-12 16:25:26.954  INFO 68282 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library 2020-08-12 16:25:26.955  INFO 68282 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library Hibernate: select user0.id as id10, user0_.age as age20, user0_.name as name30 from user user0 where user0.name=? 第一次查詢:10 第二次查詢:10

(推薦微課:Spring微課)

可以看到:

  1. 第一行輸出的CacheManager typeorg.springframework.data.redis.cache.RedisCacheManager,而不是上一篇中的EhCacheCacheManager

  2. 第二次查詢的時(shí)候,沒(méi)有輸出SQL語(yǔ)句,所以是走的緩存獲取

“Spring Boot中怎么使用集中式緩存Redis”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI