更好用 更簡單的Java快取框架 jscache
比Spring Cache 更好用 更簡單的快取工具 jscache 取名意義為 java simple cache,基於AOP實現,支援註解到介面 自定義單個快取過期時間配置 ttl,輕鬆擴展快取實現,默認實現了jedis,spring-data-redis,還有一個基於本地記憶體的map。
源碼倉庫 //github.com/peachyy/jscache.git
註解API
@Cacheable
設置/獲取快取 如果當前KEY對應的快取存在就直接返回,不存在則調用服務後 再對結果進行快取。
配置項
- prefix 快取前綴
- key key 是
el表達式 默認生成後的快取key為prefix+key - ttl 快取存活時間(過期時間) 需要具體的快取實現支援 如常用的
redis是支援的 - argCondition 前置條件過濾 針對參數過濾 滿足則執行表達式邏輯
- returnCondition 後置條件過濾 只有前置條件為
true的情況下才能到達後置過濾 為true才會把結果放入快取中 -
allowNullValue 返回值為空的情況下是否快取 防止快取穿透可以支援為
true
@Cacheable(prefix = "user:",key = "#p0",ttl = 60,returnCondition = "#result!=null")
public User getUserById(Integer userId) {
User user=new User();
user.setId(userId);
user.setName("xxx");
log.info("GET getUserById");
return user;
}
@CachePut
只是設置(put)快取 無論快取是否存在 這裡支援設置(put)多個快取
配置項 與 cacheable類似
- prefix 快取前綴
- key key 是
el表達式 默認生成後的快取key為prefix+key - ttl 快取存活時間(過期時間) 需要具體的快取實現支援 如常用的
redis是支援的 - argCondition 前置條件過濾 針對參數過濾 滿足則執行表達式邏輯
- returnCondition 後置條件過濾 只有前置條件為
true的情況下才能到達後置過濾 為true才會把結果放入快取中 -
allowNullValue 返回值為空的情況下是否快取 防止快取穿透可以支援為
true
@CachePut(prefix = "user:",key = "#p0")
@CachePut(prefix = "members:",key = "#p0")
public User getUserById(Integer userId) {
User user=new User();
user.setId(userId);
user.setName("xxx");
log.info("GET getUserById");
return user;
}
@CacheEvict
刪除快取 支援刪除多個快取
配置項
- prefix 快取前綴
- key key 是
el表達式 默認生成後的快取key為prefix+key - argCondition 前置條件過濾 針對參數過濾 滿足則執行表達式邏輯
@CacheEvict(prefix = "members:",key = "#p0")
@CacheEvict(prefix = "user:",key = "#p0",argCondition = "#p0==100")
public User getUserById(Integer userId) {
User user=new User();
user.setId(userId);
user.setName("xxx");
log.info("GET getUserById");
return user;
}
開始使用快取
如springboot中 標註@EnableCache註解 表示快取功能啟用 只要標註了註解的就會生效。
引入jar
<dependency>
<groupId>com.peachyy</groupId>
<artifactId>jscache-core</artifactId>
<version>${last.jscache.version}</version>
</dependency>
啟用快取 並配置一個快取實現。
@SpringBootApplication
@EnableCache()
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Bean
public JedisCache cache(){
Properties properties=new Properties();
properties.put("hosts","192.168.0.2:6379");
properties.put("password","bac123");
properties.put("database","0");
return new JedisCache(properties,null);
}
這裡有一個基於springboot的例子 //github.com/peachyy/jscache/tree/master/jscache-springmvc-example
更多適配
主要是用於針對部分rpc 如dubbo 當使用@Reference註解 實例沒有被spring ioc管理到 就不能到框架AOP 所以提供一些簡單的支援 目前僅實現了dubbo的這種情況
模組
- jscache-annotation 只是註解包 比如標註在介面上 而這個介面需要給其它地方引用 就只需要引用這個jar就好,免得過多產生過多的依賴
- jscache-core 核心功能實現
- jscache-dubbo 針對沒有被spring管理
dubbo service的適配 基於filter實現 - jscache-springmvc-example 一個springboot 簡單例子
序列化以及其它擴展
序列化
序列化只針對值 key默認為String字元,方便監控查看。自定義序列化需要實現 com.peachyy.jscache.core.serialize.Serializer介面。默認的實現有fastJson,jackson,java 自定義的直接傳自定義的全類名就行。
如 擴展了一個com.xxx.myJacksonSerializer序列化方式 設置的方式大概就是這樣
@EnableCache(serializer="com.xxx.myJacksonSerializer")
擴展快取實現
擴展快取需要實現com.peachyy.jscache.core.Cache介面,加入spring容器就完事了。不需要複雜的實現
@Bean
public JedisCache cache(){
Properties properties=new Properties();
properties.put("hosts","192.168.0.2:6379");
properties.put("password","bac123");
properties.put("database","0");
return new JedisCache(properties,null);
}
和spring cache比起來使用上的功能大部分有,一些設計也參考了它,使用上明顯的差別就是支援了ttl過期時間,去掉了cacheManager設計,但是僅不止如此 開發者更易駕馭,一個系統中一般保持一套快取規範就夠了。總之適合就是最好的。
原文://blog.seoui.com/2020/08/21/jscache/

有問題可關注公眾號聯繫

