阿里巴巴JetCache整理

JetCache 框架介紹

整體框架

JetCache整體框架對應的接口圖
Jetcache傳遞上下文對應的類圖
基於註解的整體流程

  • 註解中使用表達式的解析類:ExpressionEvaluator
  • 註解@EnableCache的作用是即使對應的方法上的註解@Cached的enabled屬性設置爲false,只要@EnableCache開啓,則對應的緩存就會被開啓
  • 註解使用的例子如下源碼中例子拷貝:
@Component("testBean")
public class TestBean {

    private static int count = 0;
    Map<String, Integer> m = new HashMap<>();


    public TestBean() {
    }

    public int noCacheCount(){
        return count++;
    }

    @Cached
    public static int staticCount() {
        return count;
    }

    @Cached
    public int count() {
        return count++;
    }

    @Cached(expire = 50, cacheType = CacheType.LOCAL, timeUnit = TimeUnit.MILLISECONDS)
    public int countWithExpire50() {
        return count++;
    }

    @Cached
    public int count1() {
        return count++;
    }

    @Cached(cacheType = CacheType.LOCAL)
    public int countWithLocalCache(){
        return count++;
    }

    @Cached(cacheType = CacheType.BOTH)
    public int countWithBoth(){
        return count++;
    }


    @Cached(enabled = false)
    public int countWithDisabledCache(){
        return count++;
    }

    @Cached(area = "A1" , cacheType = CacheType.LOCAL)
    public int countLocalWithDynamicQuery(DynamicQuery q) {
        return count++;
    }

    @Cached(area = "A1" , cacheType = CacheType.LOCAL, keyConvertor = "fastjson")
    public int countLocalWithDynamicQueryAndKeyConvertor(DynamicQuery q) {
        return count++;
    }

    @Cached(area = "A1")
    public int countRemoteWithDynamicQuery(DynamicQuery q) {
        return count++;
    }

    @Cached(area = "A1")
    public int countLocalWithDynamicQueryWithEquals(DynamicQueryWithEquals q) {
        return count++;
    }

    @Cached(condition = "mvel{bean('configBean').trueProp}")
    public int countEnabledWithConfigBean(){
        return count++;
    }

    @Cached(condition = "mvel{bean('configBean').falseProp}")
    public int countDisabledWithConfigBean(){
        return count++;
    }

    @Cached(condition = "mvel{xxx('configBean').trueProp}")
    public int countWithWrongCondition(){
        return count++;
    }

    @Cached(condition = "mvel{args[0]}")
    public int count(boolean useCache){
        return count++;
    }

    @Cached(name="n1")
    public int namedCount1_WithNameN1(){
        return count++;
    }

    @Cached(name="n1")
    public int namedCount2_WithNameN1(){
        return count++;
    }

    @Cached(name="n2")
    public int namedCount_WithNameN2(){
        return count++;
    }


    @Cached(name = "c1", key = "args[0]")
    public int count(String id) {
        Integer v = m.get(id);
        if (v == null) {
            v = count++;
        }
        v++;
        m.put(id, v);
        return v;
    }

    @CacheUpdate(name = "c1", key = "#id", value = "args[1]")
    public void update(String id, int value) {
        m.put(id, value);
    }
    @CacheInvalidate(name = "c1", key = "args[0]")
    public void delete(String id) {
        m.remove(id);
    }

    @CacheUpdate(name = "c2", key = "args[0]", value = "args[1]")
    public void update2(String id, int value) {
        m.put(id, value);
    }

    @CacheInvalidate(name = "c2", key = "args[0]")
    public void delete2(String id) {
        m.remove(id);
    }
}

CompletionStage 簡介

Future: Java 8 之前的 Java 版本功能較弱,僅支持兩種用法:要麼檢查 future 是否已經完成,要麼等待 future 完成;Java 8 增加了 CompletableFuture 類,它實現了新的 CompletionStage 接口,並對 Future進行了擴展。(都包含在 java.util.concurrent 包中。)CompletionStage 代表異步計算中的一個階段或步驟。該接口定義了多種不同的方式,將CompletionStage 實例與其他實例或代碼鏈接在一起,比如完成時調用的方法;CompletionStage 的接口一般都返回新的CompletionStage,表示執行完一些邏輯後,生成新的CompletionStage,構成鏈式的階段型的操作。CompletionStage是一個接口,從命名上看得知是一個完成的階段,它裏面的方法也標明是在某個運行階段得到了結果之後要做的事情。用法參見https://blog.csdn.net/mrxiky/article/details/78962614

redis lettuce簡介

Lettuce是一個可伸縮的線程安全的Redis客戶端,用於同步,異步和反應使用。 多個線程可以共享同一個RedisConnection。它利用優秀netty NIO框架來高效地管理多個連接。 支持先進的Redis功能,如Sentinel,集羣,流水線,自動重新連接和Redis數據模型。用法如下:

RedisClient redisClient = RedisClient.create("redis://[email protected]:6379/0");// 新建客戶端  
StatefulRedisConnection<String, String> connection = redisClient.connect();// 連接  

RedisAsyncCommands<String, String> asynCommands = connection.async();// 異步操作  

RedisFuture<String> future = asynCommands.get("key");// 使用future  
future.thenAccept((e) -> System.out.println(e)); // 接收到數據時 執行  
// 執行其他操作  

// 關閉連接  
connection.close();  
redisClient.shutdown();  

java.util.function-Function接口 簡介

Function 接口:This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. 文檔參見:https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

//將Function對象應用到輸入的參數上,然後返回計算結果。
R apply(T t);
//返回一個先執行當前函數對象apply方法再執行after函數對象apply方法的函數對象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
//返回一個先執行before函數對象apply方法再執行當前函數對象apply方法的函數對象
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

函數式接口 - Functional Interface

所謂的函數式接口,當然首先是一個接口,然後就是在這個接口裏面只能有一個抽象方法。這種類型的接口也稱爲SAM接口,即Single Abstract Method interfaces。它們主要用在Lambda表達式和方法引用(實際上也可認爲是Lambda表達式)上。如定義了一個函數式接口如下:

 @FunctionalInterface
    interface MyFirstFunctionInterface
    {
        void say(String message);
    }

那麼就可以使用Lambda表達式來表示該接口的一個實現(注:JAVA 8 之前一般是用匿名類實現的):

MyFirstFunctionInterface firstInterface= message -> System.out.println("Hello " + message);

詳細信息參見:https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

項目源碼和文檔

項目源碼和文檔:https://github.com/alibaba/jetcache.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章