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

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏hbbliyong

WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlug...

4214
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8538
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2850
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.4K7
来自专栏跟着阿笨一起玩NET

c#实现打印功能

3642
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

5228
来自专栏码匠的流水账

聊聊NettyConnector的start及shutdown

reactor-netty-0.7.6.RELEASE-sources.jar!/reactor/ipc/netty/NettyConnector.java

1031
来自专栏我和未来有约会

Silverlight第三方控件专题

这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏请告知我一下。 名称 简介 截图 telerik 商 RadC...

4385
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

4219
来自专栏魂祭心

原 canvas绘制clock

5074

扫码关注云+社区