1、CacheManager
Spring Boot預設內建CacheManager,如下包所示:
可以看出springboot自動配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。
預設的CacheManager為ConcurrenMapCacheManager,Spring從Spring3.1開始基于java.util.concurrent.ConcurrentHashMap實作的緩存管理器。Spring Boot 預設使用 ConcurrentMapCacheManager作為緩存技術。建立一個springboot項目,直接使用cache相關注解來測試,效果如下:
2、整合encache
pom依賴引入
<!--開啟 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>
application檔案配置
spring:
cache:
#ehcache配置檔案路徑
ehcache:
config: classpath:/ehcache/ehcache.xml
#指定緩存類型,可加可不加
#type: ehcache
encache.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="myEncache">
<!--
diskStore:為緩存路徑,ehcache分為記憶體和磁盤 2級,此屬性定義磁盤的緩存位置
user.home - 使用者主目錄
user.dir - 使用者目前工作目錄
java.io.tmpdir - 預設臨時檔案路徑
-->
<diskStore path="D:/home/Tmp_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,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那麼現有緩存元素中時間戳離目前時間最遠的元素将被清出緩存。
-->
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="users_test"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
啟動類加上啟用緩存注解
@EnableCaching
public class SpringbootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
}
}
代碼中使用cache注解
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
//使用ehcache配置的緩存名users_test
private final String USER_CACHE_NAME = "users_test";
@Override
public List<User> listUser() {
return userMapper.selectUserList();
}
@Override
// @Cacheable(value = USER_CACHE_NAME, key = "#id")
@Cacheable(value = USER_CACHE_NAME, key = "'user' + #id")
public User selectUserById(Integer id) {
return userMapper.selectUserById(id);
}
@Override
// @CacheEvict(value = USER_CACHE_NAME, key = "#id")
@CacheEvict(value = USER_CACHE_NAME, key = "'user_' + #id")
public void delete(Integer id) {
userMapper.delete(id);
}
@Override
// @CacheEvict(value = USER_CACHE_NAME, key = "#user.userId")
@CacheEvict(value = USER_CACHE_NAME, key = "'user' + #user.userId")
// @CachePut(value = USER_CACHE_NAME, key = "'user' + #user.userId") //測試發現隻将結果清除,key未清除,導緻查詢繼續使用緩存但結果為空????
public void update(User user) {
userMapper.update(user);
}
}
項目啟動後檢視CacheManager,如下圖所示則表示整合ehcache成功