spring boot學習(十三)SpringBoot快取(EhCache 2.x 篇)
SpringBoot 快取(EhCache 2.x 篇)
SpringBoot 快取
在 Spring Boot中,通過@EnableCaching
註解自動化配置合適的快取管理器(CacheManager),Spring Boot根據下面的順序去偵測快取提供者:
* Generic
* JCache (JSR-107)
* EhCache 2.x
* Hazelcast
* Infinispan
* Redis
* Guava
* Simple
關於 Spring Boot 的快取機制:
快取記憶體抽象不提供實際儲存,並且依賴於由org.springframework.cache.Cache
org.springframework.cache.CacheManager
介面實現的抽象。 Spring Boot根據實現自動配置合適的CacheManager,只要快取支援通過@EnableCaching
註釋啟用即可。
Spring Boot 配置 EhCache 2.x
官方文件上對於註解快取的介紹資料非常之少,往往需要我們自己去了解相應的快取提供者。我這裡主要介紹的是 EhCache .
引入依賴
在pom.xml
檔案中引入以下依賴
<!--開啟 cache 快取-->
<dependency>
<groupId >org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache 快取 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId >
</dependency>
引入配置檔案 ehcache.xml
在resource
資料夾下建立檔案ehcache.xml
,並進行配置:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
<!-- 這裡的 users 快取空間是為了下面的 demo 做準備 -->
<cache
name="users"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
ehcache.xml 檔案配置詳解
部分資料來源於網路
- diskStore:為快取路徑,ehcache分為記憶體和磁碟兩級,此屬性定義磁碟的快取位置。
- defaultCache:預設快取策略,當ehcache找不到定義的快取時,則使用這個快取策略。只能定義一個。
- name:快取名稱。
- maxElementsInMemory:快取最大數目
- maxElementsOnDisk:硬碟最大快取個數。
- eternal:物件是否永久有效,一但設定了,timeout將不起作用。
- overflowToDisk:是否儲存到磁碟,當系統當機時
- timeToIdleSeconds:設定物件在失效前的允許閒置時間(單位:秒)。僅當eternal=false物件不是永久有效時使用,可選屬性,預設值是0,也就是可閒置時間無窮大。
- timeToLiveSeconds:設定物件在失效前允許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false物件不是永久有效時使用,預設是0.,也就是物件存活時間無窮大。
- diskPersistent:是否快取虛擬機器重啟期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.diskSpoolBufferSizeMB:這個引數設定DiskStore(磁碟快取)的快取區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區。
- diskExpiryThreadIntervalSeconds:磁碟失效執行緒執行時間間隔,預設是120秒。
- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。
- clearOnFlush:記憶體數量最大時是否清除。
- memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,預設策略)、FIFO(先進先出)、LFU(最少訪問次數)。
FIFO,first in first out,先進先出。
LFU, Less Frequently Used,一直以來最少被使用的。如上面所講,快取的元素有一個hit屬性,hit值最小的將會被清出快取。
LRU,Least Recently Used,最近最少使用的,快取的元素有一個時間戳,當快取容量滿了,而又需要騰出地方來快取新的元素的時候,那麼現有快取元素中時間戳離當前時間最遠的元素將被清出快取。
在主類加上啟動註解
在 Spring Boot 主類加上開啟快取的註解@EnableCaching
。
demo : SpringBoot + EhCache
搭建 Spring Boot 工程
我搭建了一個普通的 SpringBoot 工程,配置了 Druid+MySQL。
並在資料庫中建立了 users 表,各欄位如下:
欄位名 | 屬性 |
---|---|
id | bigint |
uuid | varchar |
name | varchar |
age | int |
使用者實體類
User.java
public class User {
private long id;
private String uuid;
private String name;
private Integer age;
//省略 get、set 及 toString 方法
}
使用者資料庫操作介面
UserDao.java
@Mapper
public interface UserDao{
void delete(String uuid);
User update(User user);
User findByUuid(String uuid);
int save(@Param("user") User user);
}
使用者操作Mapper檔案
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="qg.fangrui.boot.dao.UserDao">
<!--目的:為Dao介面方法提供SQL語句-->
<!--對映實體物件-->
<resultMap id="UserResultMap" type="qg.fangrui.boot.model.User">
<id property="id" column="id" />
<result property="uuid" column="uuid" />
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<insert id="save">
INSERT INTO users(name, age, uuid)
VALUES (#{user.name}, #{user.age}, #{user.uuid})
</insert>
<select id="findByUuid" resultType="User">
SELECT * FROM users WHERE uuid = #{uuid}
</select>
<delete id="delete">
DELETE FROM users WHERE uuid = #{uuid}
</delete>
</mapper>
使用者操作 service 層
一般情況下,我們在Sercive層進行對快取的操作。先介紹 Ehcache 在 Spring 中的註解:在支援 Spring Cache 的環境下,
* @Cacheable
: Spring在每次執行前都會檢查Cache中是否存在相同key的快取元素,如果存在就不再執行該方法,而是直接從快取中獲取結果進行返回,否則才會執行並將返回結果存入指定的快取中。
* @CacheEvict
: 清除快取。
* @CachePut
: @CachePut
也可以宣告一個方法支援快取功能。使用@CachePut
標註的方法在執行前不會去檢查快取中是否存在之前執行過的結果,而是每次都會執行該方法,並將執行結果以鍵值對的形式存入指定的快取中。
* 這三個方法中都有兩個主要的屬性:value 指的是 ehcache.xml 中的快取策略空間;key 指的是快取的標識,同時可以用 # 來引用引數。
UserService.java
@Service
public class UserService {
//這裡的單引號不能少,否則會報錯,被識別是一個物件
private static final String CACHE_KEY = "'user'";
private static final String DEMO_CACHE_NAME = "users";
@Autowired
private UserDao userDao;
//刪除使用者資料
@CacheEvict(value = DEMO_CACHE_NAME,key = "'user_'+#uuid")//這是清除快取
public void delete(String uuid){
userDao.delete(uuid);
}
//更新使用者資料
@CachePut(value = DEMO_CACHE_NAME,key = "'user_'+#user.getUuid()")
public User update(User user) throws CacheException{
User user1 = userDao.findByUuid(user.getUuid());
if (null == user1){
throw new CacheException("Not Find");
}
user1.setAge(user.getAge());
user1.setName(user.getName());
return user1;
}
//查詢使用者資料
@Cacheable(value=DEMO_CACHE_NAME,key="'user_'+#uuid")
public User findByUuid(String uuid){
//若找不到快取將打印出提示語句
System.err.println("沒有走快取!"+uuid);
return userDao.findByUuid(uuid);
}
//儲存使用者資料
@CacheEvict(value=DEMO_CACHE_NAME,key=CACHE_KEY)
public int save(User user){
return userDao.save(user);
}
}
Controller 類
最後我們建立一個 Controller 來訪問我們的快取。因為我的 SpringBoot 處於 Debug 模式,會將所有的資料庫操作打印出來,這樣子快取作用就可一目瞭然了。
EhcacheController.java
@RestController
public class EhcacheController {
private static final Logger logger = LoggerFactory.getLogger(EhcacheController.class);
@Autowired
private UserService userService;
@RequestMapping("/encache")
public String EhcacheTest(){
logger.debug("進行Encache快取測試");
System.out.println("====生成第一個使用者====");
User user1 = new User();
//生成第一個使用者的唯一識別符號 UUID
String u1_uuid = UUID.randomUUID().toString();
//去掉 UUID 的 - 符號
String uuid1 = u1_uuid.substring(0,8)+u1_uuid.substring(9,13)+u1_uuid.substring(14,18)+u1_uuid.substring(19,23)+u1_uuid.substring(24);
user1.setName("張三");
user1.setAge(18);
user1.setUuid(uuid1);
if (userService.save(user1) == 0){
throw new JdbcException("使用者物件插入資料庫失敗");
}
//第一次查詢
System.out.println(userService.findByUuid(user1.getUuid()));
//通過快取查詢
System.out.println(userService.findByUuid(user1.getUuid()));
System.out.println("====修改資料====");
User user2 = new User();
user2.setName("李四-update");
user2.setAge(22);
user2.setId(user1.getId());
user2.setUuid(user1.getUuid());
try {
System.out.println(userService.update(user2));
} catch (CacheException e){
e.printStackTrace();
}
System.out.println(userService.findByUuid(user2.getUuid()));
return "success";
}
}
測試
啟動 SpringBoot 工程,訪問 http://localhost:8080/encache ,並檢視控制檯列印資訊:
由控制檯,我們可以清楚到看到,第一次查詢使用者資訊時,工程將使用者資訊存入快取中;在第二次查詢時,無需訪問資料庫直接從快取中獲取使用者資訊。