Spring Boot - Redis缓存

Spring Boo缓存

Redis缓存

  1. 创建项目,添加缓存依赖,在Spring Boot 2.1.5 中远程连接Redis需要添加Spring Security依赖
<dependencies>
    <!--缓存依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!--Redis依赖,去除lettuce,引入jedis-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.lettuce</groupId>
                <artifactId>io.lettuce</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>
    <!--Spring Security依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>2.1.13.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

  1. 缓存配置
#缓存配置 
spring.cache.cache-names=book
spring.cache.redis.time-to-live=1800s
#Redis配置
spring.redis.database=0
spring.redis.host=121.199.12.11
spring.redis.password=1011
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0

其中缓存的名称为book

  1. 开启缓存
@SpringBootApplication
//开启缓存
@EnableCaching
public class CacheApplication { 
    public static void main(String[] args) {
        SpringApplication.run(CacheApplication.class, args);
    }
}

4.创建BookDao

@Repository
@CacheConfig(cacheNames = "book")
public class BookDao {

    @Cacheable
    public Book getBookById(Integer name){
        System.out.println("getBookById>>>1");
        Book book = new Book();
        book.setId(1);
        book.setAuthor("zs");
        book.setName("java");
        return book;
    }

    @CachePut(key = "#book.id")
    public Book updateBookById(Book book){
        System.out.println("updateBookById>>>2");
        book.setName("java");
        return book;
    }

    @CacheEvict
    public void deleteBookById(Integer id){
        System.out.println("deleteBookById>>>3");
    }
}
  • @CacheConfig(cacheNames = “book”)缓存的名称为book
  • @Cacheable表示对该方法进行缓存,默认情况下,缓存的key为方法的参数,缓存的value是方法的返回值。
  • @CachePut(key = “#book.id”) 用于更新方法,添加了@CachePut注解在执行方法时不会去检查有没有缓存,而是直接去执行方法,然后将执行得到得结果缓存起来。下次调用该缓存时,返回的是更新的数据。
  • @CacheEvict用于删除缓存,移除一个key对应的value。
  1. 测试
@SpringBootTest
class CacheApplicationTests {

    @Autowired
    BookDao bookDao;
    @Test
    void contextLoads() {
        bookDao.getBookById(1);
        bookDao.getBookById(1);
        bookDao.deleteBookById(1);
        Book b3 = bookDao.getBookById(1);
        System.out.println("b3="+b3);
        Book book = new Book();
        book.setName("java");
        book.setAuthor("zs");
        book.setId(1);
        bookDao.updateBookById(book);
        Book b4 = bookDao.getBookById(1);
        System.out.println("b4="+b4);
    }
}

在这里插入图片描述

先将Redis的缓存清空,然后运行程序。getBookById()方法只执行了一次,说明缓存起作用了,然后执行deleteBookById()方法,缓存被清除,所以调用getBookById()方法时又一次的执行了getBookById()方法,
然后执行updateBookById(),方法被更新,最后调用getBookById()的时候返回新的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值