springboot cache 快取
阿新 • • 發佈:2022-05-04
使用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"));
}
}