前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot cache 缓存

springboot cache 缓存

作者头像
yawn
发布2018-03-14 11:49:02
8210
发布2018-03-14 11:49:02
举报
文章被收录于专栏:osc同步分享-java技术分享站

使用springboot的cache功能:

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

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

代码语言:javascript
复制
/*
 * 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实例如下:

实体类

代码语言:javascript
复制
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 +
                '}';
    }
}

缓存类

代码语言:javascript
复制
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");
    }
}

测试类

代码语言:javascript
复制
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"));
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档