阿里巴巴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

猜你喜欢

转载自blog.csdn.net/lwjaiyjk3/article/details/80257192