springboot cache 缓存
2017-10-25 00:00
190 查看
使用springboot的cache功能:
1.在启动类上加注解@enableCaching,启用缓存
2.在需要缓存的方法上加入对应的注解,具体如下:
springboot实例如下:
实体类
缓存类
测试类
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")); } }
相关文章推荐
- spring boot guava cache 缓存学习
- springboot中关于缓存@Cacheable、@CacheEvict、@CachePut的用法理解浅析
- spring boot caffeine cache 缓存学习
- springboot缓存 之 从Cache中取出所有条目
- Spring Boot 数据缓存 Cache
- spring boot+spring cache实现两级缓存(redis+caffeine)
- SpringBoot--P-1:缓存 Another unnamed CacheManager already exists in the same VM.
- springboot cache redis 缓存
- spring boot guava cache 缓存学习
- spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题
- SpringBoot 整合redis实现缓存 记录@CachePut值为1
- spring boot guava cache 缓存学习
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- springBoot学习(四): springBoot + Cache 缓存
- Spring Boot 菜鸟教程 17 Cache-缓存
- Spring Boot缓存实战 默认Cache(ConcurrentMapCacheManager)
- Spring Boot Cache + redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
- spring boot redis cache 缓存学习
- Spring boot 中缓存的使用
- SpringBoot之Mybatis操作中使用Redis做缓存