SpringBoot redis缓存

2022/6/14 2:20:05

本文主要是介绍SpringBoot redis缓存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

添加缓存依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

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

 

缓存配置:

spring:
  redis:
    host: localhost # Redis服务器地址
    database: 0 # Redis数据库索引(默认为0)
    port: 6379 # Redis服务器连接端口
    password: # Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接
    timeout: 3000ms # 连接超时时间(毫秒)
  cache:
    cache-names: book_cache,c1,c2
    redis:
      time-to-live: 1800s

 

开启缓存:

@SpringBootApplication
@EnableCaching
public class XcSpringbootApplication {

  

 

创建BookService和测试的步骤与SpringBoot Ehcache 2.x缓存一致

 

文章来源: Spring Boot+Vue全栈开发实战 - 9.2 Redis单机缓存

 



这篇关于SpringBoot redis缓存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程