一、背景
公司想对一些不经常变动的数据做一些本地缓存,我们使用AOP+Caffeine来实现。
但是只使用本地缓存,如果缓存失效,高并发下在重新加载缓存时,用户会出现时不时的“卡顿”现象。
如果使用非过期的缓存,还需要额外维护缓存一致性的问题。
本案例中采用二级缓存。L1缓存失效时间短,L2缓存失效时间长。请求优先从L1缓存获取数据,如果未命中,则加锁,保证只有一个线程去数据库中读取数据然后再更新到L1和L2中。然后其他线程依然在L2缓存获取数据。
这样可以保证,用户请求的数据都是来自于缓存,不会出现“卡顿”的现象。
多级缓存通常用于不怎么经常修改的数据、经常访问的数据比如说网站首页、商品详情页等。
我们一级缓存使用Caffeine,二级缓存使用Redis。
二、实现本地缓存
1、定义注解
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、切面
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 if (type.getName().startsWith("java.")) {
} 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 (Method method : type.getMethods()) {
if (method.getName().startsWith("get") && method.getParameterCount() == 0) {
try {
Object value = method.invoke(o);
if (value != null) {
fieldMap.put(method.getName().substring(3), value);
}
} catch (Exception e) {}
}
}
}
}
3、缓存工具类
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());
}
}
4、测试
@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";
}
三、实现多级缓存
import com.github.benmanes.caffeine.cache.Cache;
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;
/**
* 本地缓存
*
* @author cuixiangfei
* @since 2024-03-18
*/
@Component
public class CaffeineCache {
private static final Map<Long, Cache<String, String>> cacheMap = new ConcurrentHashMap<>();
/**
* 创建本地缓存
* @param seconds 过期时间:秒
*/
private static Cache<String, String> createCache(long seconds) {
return Caffeine.newBuilder()
.expireAfterWrite(seconds, TimeUnit.SECONDS)
.build();
}
/**
* 获取一个缓存组
* @param seconds 缓存过期时间
*/
private static Cache<String, String> getAndLoad(long seconds) {
if (cacheMap.containsKey(seconds)) {
return cacheMap.get(seconds);
}
Cache<String, String> cache = createCache(seconds);
cacheMap.put(seconds, cache);
return cache;
}
/**
* 缓存数据,过期时间默认10分钟
* @param key key
* @param supplier 数据来源的方法
*/
public String cacheData(String key, Supplier<String> supplier) {
return cacheData(key, supplier, 600);
}
/**
* 缓存数据
* @param key key
* @param supplier 数据来源的方法
* @param seconds 过期时间:秒
*/
public String cacheData(String key, Supplier<String> supplier, long seconds) {
Assert.state(seconds > 0, "过期时间必须大于0秒");
Cache<String, String> cache = getAndLoad(seconds);
// 自动解决缓存击穿问题
return cache.get(key, k -> supplier.get());
}
}
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
* 缓存管理
*/
@Component
public class CacheManager {
private final StringRedisTemplate redisTemplate;
private final CaffeineCache caffeineCache;
// 用于刷新缓存的线程池
private final ThreadPoolTaskExecutor refreshCacheExecutor;
@Autowired
public CacheManager(StringRedisTemplate redisTemplate, CaffeineCache caffeineCache) {
this.redisTemplate = redisTemplate;
this.caffeineCache = caffeineCache;
// 用于刷新缓存的线程池
this.refreshCacheExecutor = new ThreadPoolTaskExecutor();
this.refreshCacheExecutor.setCorePoolSize(10);
this.refreshCacheExecutor.setMaxPoolSize(10);
this.refreshCacheExecutor.setKeepAliveSeconds(60);
this.refreshCacheExecutor.setThreadNamePrefix("refreshCache-");
this.refreshCacheExecutor.setWaitForTasksToCompleteOnShutdown(true);
this.refreshCacheExecutor.initialize();
}
/**
* 多级缓存
* @param key key
* @param supplier 查询数据库的结果
* @param localSeconds 本地缓存秒数
* @param redisSeconds redis过期的秒数
*/
public String multiCache(String key, Supplier supplier, long localSeconds, long redisSeconds) {
return caffeineCache.cacheData(key, () -> redisCache(key, supplier, redisSeconds), localSeconds);
}
/**
* 获取redis的缓存
* @param key key
* @param supplier 查询数据库的结果
* @param seconds redis过期的秒数
*/
private String redisCache(String key, Supplier supplier, long seconds) {
String value = redisTemplate.opsForValue().get(key);
if (StringUtils.isNotBlank(value)) {
System.out.println("获取到redis缓存" + key);
// 异步查库,更新redis
refreshCacheExecutor.execute(() -> {
Object finalValue = supplier.get();
if (finalValue != null) {
String jsonFinalValue = JSON.toJson(finalValue);
redisTemplate.opsForValue().set(key, jsonFinalValue, seconds, TimeUnit.SECONDS);
} else {
// 解决缓存穿透问题
redisTemplate.opsForValue().set(key, "", seconds, TimeUnit.SECONDS);
}
});
return value;
}
// 缓存不存在,需要查库(兜底方案)
Object finalValue = supplier.get();
System.out.println("redis不存在,查库");
if (finalValue != null) {
String jsonFinalValue = JSON.toJson(finalValue);
redisTemplate.opsForValue().set(key, jsonFinalValue, seconds, TimeUnit.SECONDS);
return jsonFinalValue;
} else {
// 解决缓存穿透问题
redisTemplate.opsForValue().set(key, "", seconds, TimeUnit.SECONDS);
return "";
}
}
}
@GetMapping("test1")
public String test1() {
return cacheManager.multiCache("key1", () -> data.get("key1"), 5, 10);
}