• 九、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.bootgroupId>
      <artifactId>spring-boot-starter-cacheartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-data-redisartifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-data-redisartifactId>
        exclusion>
      exclusions>
    dependency>
    <dependency>
      <groupId>io.lettucegroupId>
      <artifactId>lettuce-coreartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3. 开启缓存

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

    @SpringBootApplication
    @EnableCaching
    public class CacheApplication {
        public static void main(String[] args) {
            SpringApplication.run(CacheApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    第 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    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");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

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

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

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

  • 相关阅读:
    小程序百问百答
    10min快速回顾C++语法(四)数组专题
    二叉树详解
    MyBatisPlus(二十一)乐观锁
    怎么将psd转化为jpg?收藏这几个方法
    扩展翡蜀定理问题
    vue2/vue3 Transition+routerView实现过渡动画效果
    Linux环境实现mysql所在服务器定时同步数据文件到备份服务器(异地容灾备份场景)
    人机环境系统智能有利于防止人工智能失控
    c语言中为什么函数传参大多数用指针类型
  • 原文地址:https://blog.csdn.net/GXL_1012/article/details/126232842