九、Spring Boot 缓存(2)

本章概要

  • Redis 单机缓存

9.2 Redis 单机缓存

和 Ehcache 一样,如果在 classpath 下存在 Redis 并且 Redis 已经配置好了,此时默认就会使用 RedisCacheManager 作为缓存提供者,Redis 单机缓存使用步骤如下:

1. 创建项目,添加缓存依赖

创建 Spring Boot 项目,添加 spring-boot-starter-cache 和 Redis 依赖

<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>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>io.lettuce</groupId>
  <artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

2. 缓存配置

Redis 单机缓存只需要开发者在 application.properties 中进行 Redis 配置及缓存配置即可,代码如下

# 缓存配置
# 配置缓存名称,Redis中的key都有一个前缀,默认前缀是“缓存名::”
spring.cache.cache-names=c1,c2
# 配置缓存有效期,即Redis中的key过期时间
spring.cache.redis.time-to-live=1800s
# Redis 配置
spring.redis.database=0
spring.redis.host=localhost
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

3. 开启缓存

在项目入口类中开启缓存,如下

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

第 4、5 步与本专栏下《九、Spring Boot 缓存(1)》一样,此处不再做过多的解释

4. 创建 BookDao

Book

public class Book implements Serializable {
    
    
    private Integer id;
    private String name;
    private String author;

    @Override
    public String toString() {
    
    
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getAuthor() {
    
    
        return author;
    }

    public void setAuthor(String author) {
    
    
        this.author = author;
    }
}

BookDao

@Repository
@CacheConfig(cacheNames = "book_cache")
public class BookDao {
    
    
    @Cacheable
    public Book getBookById(Integer id) {
    
    
        System.out.println("getBookById");
        Book book = new Book();
        book.setId(id);
        book.setName("三国演义");
        book.setAuthor("罗贯中");
        return book;
    }
    @CachePut(key = "#book.id")
    public Book updateBookById(Book book) {
    
    
        System.out.println("updateBookById");
        book.setName("三国演义2");
        return book;
    }
    @CacheEvict(key = "#id")
    public void deleteBookById(Integer id) {
    
    
        System.out.println("deleteBookById");
    }
}

5. 创建测试类

创建测试类,对 Service 中的方法进行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheApplicationTests {
    
    
    @Autowired
    BookDao bookDao;
    @Test
    public void contextLoads() {
    
    
        bookDao.deleteBookById(1);
        bookDao.getBookById(1);
        bookDao.getBookById(1);
        bookDao.deleteBookById(1);
        Book b3 = bookDao.getBookById(1);
        System.out.println("b3:"+b3);
        Book b = new Book();
        b.setName("三国演义");
        b.setAuthor("罗贯中");
        b.setId(1);
        bookDao.updateBookById(b);
        Book b4 = bookDao.getBookById(1);
        System.out.println("b4:"+b4);
    }
}

执行该方法,控制台打印日志如下:

deleteBookById
getBookById
deleteBookById
getBookById
b3:Book{id=1, name='三国演义', author='罗贯中'}
updateBookById
b4:Book{id=1, name='三国演义2', author='罗贯中'}

为了防止来回测试缓存的影响,这里先执行删除操作(同时也会删除缓存)。然后执行了一次查询,正常打印,接着又执行了一次查询没打印(直接读取的缓存),然后执行删除,接着再执行查询正常打印(删除操作也删除了缓存),再接着执行更新操作(同时更新了缓存),最后再次查询,打印更新后的数据。

猜你喜欢

转载自blog.csdn.net/GXL_1012/article/details/126232842