SpringBoot缓存 --Redis单机缓存

Posted CrazyDream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot缓存 --Redis单机缓存相关的知识,希望对你有一定的参考价值。

 

pom.xml

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

application.properties

#缓存配置
spring.cache.cache-names=c1,c2
spring.cache.redis.time-to-live=1800s
#Redis配置
spring.redis.database=0
spring.redis.host=192.168.205.100
spring.redis.port=6379
spring.redis.password=123456 
spring.redis.jedis.pool.max-active=8 
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms 
spring.redis.jedis.pool.min-idle=0

dao

@Repository
public class BookDao {
    @Cacheable("c1")
    public Book getBookById(Integer id) {
        System.out.println("getBookById");
        Book book = new Book();
        book.setId(id);
        book.setName("三国演义");
        book.setAuthor("罗贯中");
        return book;
    }
}

项目入口类开启缓存:

@SpringBootApplication
@EnableCaching
public class RediscacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(RediscacheApplication.class, args);
    }
}

 

以上是关于SpringBoot缓存 --Redis单机缓存的主要内容,如果未能解决你的问题,请参考以下文章

重学SpringBoot系列之EhCache缓存,缓存问题

SpringBoot 开启Redis缓存

spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

SpringBoot 整合 Redis缓存

完整SpringBoot Cache整合redis缓存

Shiro整合Springboot缓存之Redis实现