使用SpringAOP+Caffeine实现本地缓存

@[TOC]

一、背景

公司想对一些不经常变动的数据做一些本地缓存,我们使用AOP+Caffeine来实现

二、实现

1、定义注解

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 本地缓存
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalCacheable {

    // 过期时间 默认10分钟
    long expired() default 600;

    // key创建器
    String keyGenerator() default "org.springframework.cache.interceptor.KeyGenerator";
}

2、切面

java 复制代码
import com.google.gson.internal.LinkedTreeMap;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 本地缓存
 */
@Aspect
@Component
public class LocalCacheAspect {

    private static final String separator = ":";

    @Around("@annotation(com.framework.localcache.LocalCacheable)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        if (AopUtils.isAopProxy(point.getTarget())) {
            return point.proceed();
        }

        Method method = getMethodSignature(point).getMethod();
        if (method == null) {
            return point.proceed();
        }

        LocalCacheable annotation = method.getAnnotation(LocalCacheable.class);
        if (annotation == null) {
            return point.proceed();
        }

        // 生成key
        String key = generateKey(point);
//         System.out.println("生成的key:" + key);

        long expired = annotation.expired();

        Throwable[] throwable = new Throwable[1];
        Object proceed = LocalCache.cacheData(key, () -> {
            try {
                return point.proceed();
            } catch (Throwable e) {
                throwable[0] = e;
            }
            return null;
        }, expired);

        if (throwable[0] != null) {
            throw throwable[0];
        }

        return proceed;
    }

    /**
     * 获取方法
     */
    private MethodSignature getMethodSignature(ProceedingJoinPoint point) {
        Signature signature = point.getSignature();
        if (signature instanceof MethodSignature) {
            return ((MethodSignature) signature);
        }
        return null;
    }

    /**
     * 获取key
     */
    private String generateKey(ProceedingJoinPoint point) {

        // 目标类、方法、参数等
        Class<?> targetClass = AopProxyUtils.ultimateTargetClass(point.getTarget());
        Method method = getMethodSignature(point).getMethod();
        String[] parameterNames = getMethodSignature(point).getParameterNames();
        Object[] args = point.getArgs();

        // 解析参数,生成key
        LinkedTreeMap<String, Object> paramResolveResult = new LinkedTreeMap<>();
        if (ArrayUtils.isNotEmpty(args)) {
            for (int i = 0; i < args.length; i++) {
                resolveParam(args[i], paramResolveResult, parameterNames[i]);
            }
        }

        StringBuilder key = new StringBuilder(targetClass.getName() + separator + method.getName() + separator);
        paramResolveResult.forEach((k, v) -> {
            if (v != null) {
                key.append(k + "," + v + separator);
            }
        });

        // 根据方法名和参数生成唯一标识
        return key.toString();
    }


    private void resolveParam(Object param, Map<String, Object> paramResolveResult, String prefix) {
        if (param == null) {
            return;
        }
        Class<?> type = param.getClass();
        if (type == List.class) {
            List<Object> param0 = (List) param;
            for (int i = 0; i < param0.size(); i++) {
                resolveParam(param0.get(i), paramResolveResult, prefix + "[" + i + "]");
            }
        } else if (type == Map.class) {
            Map<Object, Object> param0 = (Map) param;
            param0.forEach((k, v) -> {
                resolveParam(v, paramResolveResult, prefix + "." + k);
            });
        } else if (type.isArray()) {
            Object[] param0 = (Object[]) param;
            for (int i = 0; i < param0.length; i++) {
                resolveParam(param0[i], paramResolveResult, prefix + "[" + i + "]");
            }
        } else if (type == Byte.class
                || type == Short.class
                || type == Integer.class
                || type == Long.class
                || type == Float.class
                || type == Double.class
                || type == Boolean.class
                || type == Character.class
                || type == String.class) {
            paramResolveResult.put(prefix, param);
        } else {
            // 复杂类型
            Map<String, Object> fieldMap = new HashMap<>();
            // CGLIB代理
            if (Enhancer.isEnhanced(type)) {
                getAllFieldsAndValue(param, type.getSuperclass(), fieldMap);
            } else {
                getAllFieldsAndValue(param, type, fieldMap);
            }

            fieldMap.forEach((k, v) -> {
                if (v == null) {
                    return;
                }
                resolveParam(v, paramResolveResult, prefix + "." + k);
            });
        }
    }

    /**
     * 获取所有字段和值
     */
    private void getAllFieldsAndValue(Object o, Class type, Map<String, Object> fieldMap) {

        for (Field field : type.getDeclaredFields()) {
            if (field.trySetAccessible()) {
                try {
                    fieldMap.put(field.getName(), field.get(o));
                } catch (IllegalAccessException e) {}
            }
        }

        if (type.getSuperclass() != Object.class) {
            getAllFieldsAndValue(o, type.getSuperclass(), fieldMap);
        }
    }


}

3、缓存工具类

java 复制代码
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * 本地缓存
 */
public class LocalCache {

    private static final Map<Long, Cache<Object, Object>> cacheMap = new ConcurrentHashMap<>();

    /**
     * 创建本地缓存
     * @param seconds 过期时间:秒
     */
    private static Cache<Object, Object> createCache(long seconds) {
        return Caffeine.newBuilder()
                .expireAfterWrite(seconds, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 创建本地缓存
     * @param seconds 过期时间:秒
     * @param loader 缓存方法
     */
    private Cache<Object, Object> createLoadingCache(long seconds, CacheLoader<Object, Object> loader) {
        return Caffeine.newBuilder()
                .expireAfterWrite(seconds, TimeUnit.SECONDS)
                .build(loader);
    }


    /**
     * 获取一个缓存组
     * @param seconds 缓存过期时间
     */
    private static Cache<Object, Object> getAndLoad(long seconds) {
        if (cacheMap.containsKey(seconds)) {
            return cacheMap.get(seconds);
        }

        Cache<Object, Object> cache = createCache(seconds);
        cacheMap.put(seconds, cache);

        return cache;
    }

    /**
     * 缓存数据,过期时间默认10分钟
     * @param key key
     * @param supplier 数据来源的方法
     */
    public static Object cacheData(Object key, Supplier<Object> supplier) {
        return cacheData(key, supplier, 600);
    }


    /**
     * 缓存数据
     * @param key key
     * @param supplier 数据来源的方法
     * @param seconds 过期时间:秒
     */
    public static Object cacheData(Object key, Supplier<Object> supplier, long seconds) {
        Assert.state(seconds > 0, "过期时间必须大于0秒");
        Cache<Object, Object> cache = getAndLoad(seconds);
        return cache.get(key, k -> supplier.get());
    }
}

三、测试

java 复制代码
    @LocalCacheable
    @GetMapping("test1")
    public String test1() {
        System.out.println("执行了");
        return "success";
    }

    @LocalCacheable
    @GetMapping("test2")
    public String test2(String a) {
        System.out.println("执行了" + a);
        return "success";
    }

    @LocalCacheable
    @GetMapping("test3")
    public String test3(String a, int b, String c) {
        System.out.println("执行了" + a + b + c);
        return "success";
    }

    @LocalCacheable
    @GetMapping("test4")
    public String test4(UserInfo user) {
        System.out.println("执行了" + user);
        return "success";
    }


    @LocalCacheable
    @GetMapping("test5")
    public String test5(UserInfo[] users) {
        System.out.println("执行了" + users);
        return "success";
    }


    @LocalCacheable
    @GetMapping("test6")
    public String test6(List<UserInfo> users) {
        System.out.println("执行了" + users);
        return "success";
    }


    @LocalCacheable
    @GetMapping("test7")
    public String test7(UserInfo user) {
        System.out.println("执行了" + user.getMap());
        return "success";
    }
相关推荐
customer0818 分钟前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_8575893628 分钟前
SpringBoot框架:作业管理技术新解
java·spring boot·后端
一只爱打拳的程序猿1 小时前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
假装我不帅2 小时前
asp.net framework从webform开始创建mvc项目
后端·asp.net·mvc
神仙别闹2 小时前
基于ASP.NET+SQL Server实现简单小说网站(包括PC版本和移动版本)
后端·asp.net
计算机-秋大田3 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
货拉拉技术3 小时前
货拉拉-实时对账系统(算盘平台)
后端
掘金酱4 小时前
✍【瓜分额外奖金】11月金石计划附加挑战赛-活动命题发布
人工智能·后端
代码之光_19804 小时前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端