springboot cache 缓存

时间:2022-05-04
本文章向大家介绍springboot cache 缓存,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

使用springboot的cache功能:

1.在启动类上加注解@enableCaching,启用缓存

2.在需要缓存的方法上加入对应的注解,具体如下:

/*
 * 1.@Cacheable(cacheNames = "car", key = "#name")
 * 将方法的返回值 保存 在缓存“car”中,键由key指定,值是方法的返回值
 * 2.@CachePut(cacheNames = "car", key = "#car.name")
 * 使用方法的返回值 更新 缓存“car”中,键为key的值
 * 3.@CacheEvict(cacheNames = "car", allEntries = true)
 * 根据key和condition删除缓存,如果指定allEntries为true,则删除缓存中所有的对象
 */

springboot实例如下:

实体类

package com.example.demo7cache.entity;

import java.time.LocalTime;

/**
 * @author Created by yawn on 2017-10-24 16:42
 */
public class Car {
    private String name;
    private LocalTime time;

    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }

    public Car(String name, LocalTime time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }

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

    public LocalTime getTime() {
        return time;
    }

    public void setTime(LocalTime time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + ''' +
                ", time=" + time +
                '}';
    }
}

缓存类

package com.example.demo7cache.cache;

import com.example.demo7cache.entity.Car;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by yawn on 2017-10-24 16:44
 */
@Service
public class CarService {

    // 以下三个方法为查询方法
    /*
     * 1.@Cacheable(cacheNames = "car", key = "#name")
     * 将方法的返回值 保存 在缓存“car”中,键由key指定,值是方法的返回值
     * 2.@CachePut(cacheNames = "car", key = "#car.name")
     * 使用方法的返回值 更新 缓存“car”中,键为key的值
     * 3.@CacheEvict(cacheNames = "car", allEntries = true)
     * 根据key和condition删除缓存,如果指定allEntries为true,则删除缓存中所有的对象
     */

    @Cacheable(cacheNames = "car", key = "#name")
    public Car getByName(String name) {
        System.err.println("查询car");
        return new Car(name, LocalTime.now());
    }
    @Cacheable(cacheNames = "car", key = "#car.name")
    public Car getByCar(Car car) {
        System.err.println("查询car");
        return new Car("getByCar", LocalTime.now());
    }
    @Cacheable(cacheNames = "carList")
    public List<Car> getAll() {
        System.err.println("查询carList");
        List<Car> carList = new ArrayList<>();
        carList.add(new Car("aaa", LocalTime.now()));
        carList.add(new Car("bbb", LocalTime.now()));
        carList.add(new Car("ccc", LocalTime.now()));
        return carList;
    }

    /**
     * save方法:
     * 1.将返回值写入car缓存
     * 2.清空carList缓存中所有对象
     */
    @Cacheable(cacheNames = "car", key = "#car.name")
    @CacheEvict(cacheNames = "carList", allEntries = true)
    public Car save(Car car) {
        System.err.println("保存car");
        return car;
    }

    /**
     * update方法
     * 1.更新缓存car
     * 2.清空carList缓存中所有对象
     */
    @CachePut(cacheNames = "car", key = "#car.name")
    @CacheEvict(cacheNames = "carList", allEntries = true)
    public Car update(Car car) {
        System.err.println("更新car");
        return new Car("yawn", LocalTime.now());
    }

    /**
     * delete方法
     * 1.删除car缓存和carList缓存中的所有对象
     */
    @CacheEvict(cacheNames = {"car", "carList"}, allEntries = true)
    public void delete(String name) {
        System.err.println("删除car");
    }
}

测试类

package com.example.demo7cache;

import com.example.demo7cache.cache.CarService;
import com.example.demo7cache.entity.Car;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.time.LocalTime;

/**
 * @author Created by yawn on 2017-10-24 16:58
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class CarServiceTest {

    @Resource
    private CarService carService;

    /**
     * 测试第二次从缓存中查询
     */
    @Test
    public void test1() {
        System.out.println(carService.getByName("yawn"));
        System.out.println(carService.getByName("yawn"));
        System.out.println(carService.getAll());
        System.out.println(carService.getAll());
    }

    /**
     * 测试不同方法查询从同一缓存中查询
     */
    @Test
    public void test2() {
        System.out.println(carService.getByName("yawn"));
        System.out.println(carService.getByCar(new Car("yawn")));
    }

    /**
     * 测试保存后的写入缓存的查询
     */
    @Test
    public void test3() throws InterruptedException {
        System.out.println(carService.save(new Car("yawn", LocalTime.now())));
        Thread.sleep(998);
        System.out.println(carService.getByName("yawn"));
    }

    /**
     * 测试更新后对car缓存的更新
     */
    @Test
    public void test4() throws InterruptedException {
        System.out.println(carService.getByName("yawn"));
        Thread.sleep(998);
        System.out.println(carService.update(new Car("yawn", LocalTime.now())));
        Thread.sleep(998);
        System.out.println(carService.getByName("yawn"));
    }

    /**
     * 更新后对carList缓存的删除
     */
    @Test
    public void test5() throws InterruptedException {
        System.out.println(carService.getAll());
        System.out.println(carService.update(new Car("yawn")));
        Thread.sleep(998);
        System.out.println(carService.getAll());
    }

    /**
     * 删除后对两个缓存中 所有实体的删除
     */
    @Test
    public void test7() throws InterruptedException {
        System.out.println(carService.getAll());
        System.out.println(carService.getByName("yawn"));
        carService.delete("yyy");
        Thread.sleep(988);
        System.out.println(carService.getAll());
        System.out.println(carService.getByName("yawn"));
    }

}