比Spring Cache 更好用 更簡單的快取工具 jscache 取名意義為 java simple cache,基于AOP實作,支持注解到介面 自定義單個快取過期時間配置 ttl,輕松擴展快取實作,默認實作了jedis,spring-data-redis,還有一個基于本地記憶體的map,
原始碼倉庫 https://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的例子 https://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設計,但是僅不止如此 開發者更易駕馭,一個系統中一般保持一套快取規范就夠了,總之適合就是最好的,
原文:https://blog.seoui.com/2020/08/21/jscache/

有問題可關注公眾號聯系
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59817.html
標籤:Java
