用慣了 Redis ,很多人已經忘記了還有另一個緩存方案 Ehcache ,是的,在 Redis 一統江湖的時代,Ehcache 漸漸有點沒落了,不過,我們還是有必要了解下 Ehcache ,在有的場景下,我們還是會用到 Ehcache。
今天松哥就來和大家聊聊 Spring Boot 中使用 Ehcache 的情況。相信看完本文,大家對於[Spring Boot 操作 Redis,三種方案全解析!]一文中的第二種方案會有更加深刻的理解。
Ehcache 也是 Java 領域比較優秀的緩存方案之一,Ehcache 這個緩存的名字很有意思,正著念反著念,都是 Ehcache,Spring Boot 中對此也提供了很好的支持,這個支持主要是通過 Spring Cache 來實現的。
Spring Cache 可以整合 Redis,當然也可以整合 Ehcache,兩種緩存方案的整合還是比較相似,主要是配置的差異,具體的用法是一模一樣的,就類似於 JDBC 和 資料庫驅動的關係一樣。前面配置完成後,後面具體使用的 API 都是一樣的。
和 Spring Cache + Redis 相比,Spring Cache + Ehcache 主要是配置有所差異,具體的用法是一模一樣的。我們來看下使用步驟。
首先,來創建一個 Spring Boot 項目,引入 Cache 依賴:
工程創建完成後,引入 Ehcache 的依賴,Ehcache 目前有兩個版本:
這裡採用第二個,在 pom.xml 文件中,引入 Ehcache 依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency> </dependencies>
在 resources 目錄下,添加 ehcache 的配置文件 ehcache.xml ,文件內容如下:
<ehcache> <diskStore path="java.io.tmpdir/shiro-spring-sample"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <cache name="user" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/> </ehcache>
配置含義:
注意
默認情況下,這個文件名是固定的,必須叫 ehcache.xml ,如果一定要換一個名字,那麼需要在 application.properties 中明確指定配置文件名,配置方式如下:
spring.cache.ehcache.config=classpath:aaa.xml
開啟緩存的方式,也和 Redis 中一樣,如下添加 @EnableCaching 依賴即可:
@EnableCaching
@SpringBootApplication @EnableCaching public class EhcacheApplication { public static void main(String[] args) { SpringApplication.run(EhcacheApplication.class, args); } }
其實到這一步,Ehcache 就算配置完成了,接下來的用法,和松哥之前講 Redis 的文章一模一樣。不過這裡松哥還是帶大家使用下。
這裡主要向小夥伴們介紹緩存中幾個核心的註解使用。
這個註解在類上使用,用來描述該類中所有方法使用的緩存名稱,當然也可以不使用該註解,直接在具體的緩存註解上配置名稱,示例代碼如下:
@Service @CacheConfig(cacheNames = "user") public class UserService { }
這個註解一般加在查詢方法上,表示將一個方法的返回值緩存起來,默認情況下,緩存的 key 就是方法的參數,緩存的 value 就是方法的返回值。示例代碼如下:
@Cacheable(key = "#id") public User getUserById(Integer id,String username) { System.out.println("getUserById"); return getUserFromDBById(id); }
當有多個參數時,默認就使用多個參數來做 key ,如果只需要其中某一個參數做 key ,則可以在 @Cacheable 註解中,通過 key 屬性來指定 key ,如上代碼就表示只使用 id 作為緩存的 key ,如果對 key 有複雜的要求,可以自定義 keyGenerator 。當然,Spring Cache 中提供了root對象,可以在不定義 keyGenerator 的情況下實現一些複雜的效果,root 對象有如下屬性:
也可以通過 keyGenerator 自定義 key ,方式如下:
@Component public class MyKeyGenerator implements KeyGenerator { @Override public Object generate(Object target, Method method, Object... params) { return method.getName()+Arrays.toString(params); } }
然後在方法上使用該 keyGenerator :
@Cacheable(keyGenerator = "myKeyGenerator") public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("lisi"); System.out.println(user); return user; }
這個註解一般加在更新方法上,當資料庫中的數據更新後,緩存中的數據也要跟著更新,使用該註解,可以將方法的返回值自動更新到已經存在的 key 上,示例代碼如下:
@CachePut(key = "#user.id") public User updateUserById(User user) { return user; }
這個註解一般加在刪除方法上,當資料庫中的數據刪除後,相關的緩存數據也要自動清除,該註解在使用的時候也可以配置按照某種條件刪除( condition 屬性)或者或者配置清除所有緩存( allEntries 屬性),示例代碼如下:
@CacheEvict() public void deleteUserById(Integer id) { //在這裡執行刪除操作, 刪除是去資料庫中刪除 }
本文主要向大家了 Spring Boot 整合 Ehcache 的用法,其實說白了還是 Spring Cache 的用法。相信讀完本文,大家對於 Redis + Spring Cache 的用法會有更深的認識。
本文案例我已上傳到 GitHub ,歡迎大家 star:https://github.com/lenve/javaboy-code-samples
關於本文,有問題歡迎留言討論。
關注公眾號【江南一點雨】,專註於 Spring Boot+微服務以及前後端分離等全棧技術,定期視頻教程分享,關注後回復 Java ,領取松哥為你精心準備的 Java 乾貨!