更好用 更簡單的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/
有問題可關注公眾號聯繫