博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot cache 缓存
阅读量:7056 次
发布时间:2019-06-28

本文共 5795 字,大约阅读时间需要 19 分钟。

hot3.png

使用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 */@Servicepublic 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
getAll() { System.err.println("查询carList"); List
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)@SpringBootTestpublic 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"));    }}

 

转载于:https://my.oschina.net/silenceyawen/blog/1555996

你可能感兴趣的文章
做一个座右铭工具每天激励自己
查看>>
Jenkins安装配置
查看>>
vmware12下对虚拟机ubuntu14.10系统所在分区sda1进行磁盘扩容
查看>>
EJB到底是什么,真的那么神秘吗??
查看>>
UI开发工具
查看>>
广义表 (五)
查看>>
Swift中NSTimer定时器的使用
查看>>
Forms开发中触发器的执行顺序
查看>>
SEO博客三个月没更新排行骤步康复
查看>>
JQuery 插件开发的入门介绍
查看>>
马哥2016全新Linux+Python高端运维班第五周作业
查看>>
联想扬天A4680R台式电脑增加内存不识别的解决方案
查看>>
(5)Powershell别名(Alias)
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
linux配置NTP Server
查看>>
PBDOM操作XML文档轻松入门
查看>>
双机热备 纯软 镜像 实战 安装前准备
查看>>
我的友情链接
查看>>
C语言基本概念(7)
查看>>