spring boot+vue全栈开发实战 pdf 下载_spring+vue全栈开发实战-第七章spring boot缓存-0310-2020...

本文详细介绍如何在Spring Boot项目中实现Redis单机缓存,包括添加依赖、配置缓存参数、开启缓存功能,并通过具体示例展示了缓存的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

v2-efc9925396a8a0d5ed2a3a54cdb6f237_1440w.jpg?source=172ae18b
1、redis单机缓存

1、redis单机缓存

Redis 单机缓存使用步骤如下:

创建项目,添加缓存依赖?

创建 Spring Boot 项目,添加 spring-boot-starter-cache 和 Redis 依赖,代码如下:

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


    <!--redis单机缓存end-->

缓存配置?

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

#缓存配置
spring.cache.cache-names=c1,c2
spring.cache.redis.time-to-live=1800s
#Redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
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

开启缓存?

接下来在项 目入口类中开启缓存,代码如下:

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

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

创建 BookDao ?

创建 Book 实体类和 BookService

package Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

import javax.crypto.KeyGenerator;
import Bean.Book;
@Service
@CacheConfig(cacheNames = "book_cache")
public class BookDao {
    @Autowired
//    MyKeyGenerator myKeyGenerator;
//    @Cacheable(keyGenerator = "myKeyGenerator")
    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");
    }
}



package Bean;

import java.io.Serializable;

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;
    }
}

创建测试类?

创建测试类, 对 Service 中的方法进行测试,代码如下:

package test;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import Dao.BookDao;
import Bean.Book;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheApplicationTests {
    @Autowired
    BookDao bookDao;
    @Test
    public void contextLoads() {
        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);
    }
}

v2-2893b10f8d4d85d42df150a76ec3efb5_b.jpg

一开始执行了两个查询,但是查询方法只打印了一次,因为第二次使用了缓存。接下来执行 了删除方法,删除方法执行完之后再次执行查询, 查询方法又被执行了,因为在删除方法中缓存己 经被删除了 。再接下来执行更新方法,更新方法中不仅更新数据,也更新了缓存,所以在最后的查 询方法中, 查询方法日志没打印,说明该方法没执行,而是使用了缓存中的数据,而缓存中的数据 已经被更新了 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值