APP下载
报价宝  ›  科技  › 

Java架构笔记-springboot中使用自定义两级快取

报价宝 来源:baojiabao.com 发布时间:2019-07-06 03:51:00 11月20日更新
报价宝综合消息Java架构笔记-springboot中使用自定义两级快取

工作中用到了springboot的快取,使用起来挺方便的,直接引入redis或者ehcache这些快取依赖包和相关快取的starter依赖包,然后在启动类中加入@EnableCaching注解,然后在需要的地方就可以使用@Cacheable和@CacheEvict使用和删除快取了。这个使用很简单,相信用过springboot快取的都会玩,这里就不再多说了。美中不足的是,springboot使用了外挂式的整合方式,虽然用起来很方便,但是当你整合ehcache的时候就是用ehcache,整合redis的时候就是用redis。如果想两者一起用,ehcache作为本地一级快取,redis作为整合式的二级快取,使用预设的方式据我所知是没法实现的(如果有高人可以实现,麻烦指点下我)。毕竟很多服务需要多点部署,如果单独选择ehcache可以很好地实现本地快取,但是如果在多机之间共享快取又需要比较费时的折腾,如果选用集中式的redis快取,因为每次取资料都要走网络,总感觉效能不会太好。本话题主要就是讨论如何在springboot的基础上,无缝整合ehcache和redis作为一二级快取,并且实现快取同步。

为了不要侵入springboot原本使用快取的方式,这里自己定义了两个快取相关的注解,如下

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface Cacheable {

String value() default "";

String key() default "";

//泛型的Class型别

Class> type() default Exception.class;

}

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface CacheEvict {

String value() default "";

String key() default "";

}

如上两个注解和spring中快取的注解基本一致,只是去掉了一些不常用的属性。说到这里,不知道有没有朋友注意过,当你在springboot中单独使用redis快取的时候,Cacheable和CacheEvict注解的value属性,实际上在redis中变成了一个zset型别的值的key,而且这个zset里面还是空的,比如@Cacheable(value="cache1",key="key1"),正常情况下redis中应该是出现cache1 -> map(key1,value1)这种形式,其中cache1作为快取名称,map作为快取的值,key作为map里的键,可以有效的隔离不同的快取名称下的快取。但是实际上redis里确是cache1 -> 空(zset)和key1 -> value1,两个独立的键值对,试验得知不同的快取名称下的快取完全是共用的,如果有感兴趣的朋友可以去试验下,也就是说这个value属性实际上是个摆设,键的唯一性只由key属性保证。我只能认为这是spring的快取实现的bug,或者是特意这么设计的,(如果有知道啥原因的欢迎指点)。

回到正题,有了注解还需要有个注解处理类,这里我使用aop的切面来进行拦截处理,原生的实现其实也大同小异。切面处理类如下:

import com.xuanwu.apaas.core.multicache.annotation.CacheEvict;

import com.xuanwu.apaas.core.multicache.annotation.Cacheable;

import com.xuanwu.apaas.core.utils.JsonUtil;

import org.apache.commons.lang3.StringUtils;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.reflect.MethodSignature;

import org.json.JSONArray;

import org.json.JSONObject;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;

import org.springframework.expression.ExpressionParser;

import org.springframework.expression.spel.standard.SpelExpressionParser;

import org.springframework.expression.spel.support.StandardEvaluationContext;

import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**

* 多级快取切面

* @author rongdi

*/

@Aspect

@Component

public class MultiCacheAspect {

private static final Logger logger = LoggerFactory.getLogger(MultiCacheAspect.class);

@Autowired

private CacheFactory cacheFactory;

//这里通过一个容器初始化监听器,根据外部配置的@EnableCaching注解控制快取开关

private boolean cacheEnable;

@Pointcut("@annotation(com.xuanwu.apaas.core.multicache.annotation.Cacheable)")

public void cacheableAspect() {

}

@Pointcut("@annotation(com.xuanwu.apaas.core.multicache.annotation.CacheEvict)")

public void cacheEvict() {

}

@Around("cacheableAspect()")

public Object cache(ProceedingJoinPoint joinPoint) {

//得到被切面修饰的方法的引数列表

Object[] args = joinPoint.getArgs();

// result是方法的最终返回结果

Object result = null;

//如果没有开启快取,直接呼叫处理方法返回

if(!cacheEnable){

try {

result = joinPoint.proceed(args);

} catch (Throwable e) {

logger.error("",e);

}

return result;

}

// 得到被代理方法的返回值型别

Class returnType = ((MethodSignature) joinPoint.getSignature()).getReturnType();

// 得到被代理的方法

Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();

// 得到被代理的方法上的注解

Cacheable ca = method.getAnnotation(Cacheable.class);

//获得经过el解析后的key值

String key = parseKey(ca.key(),method,args);

Class> elementClass = ca.type();

//从注解中获取快取名称

String name = ca.value();

try {

//先从ehcache中取资料

String cacheValue = cacheFactory.ehGet(name,key);

if(StringUtils.isEmpty(cacheValue)) {

//如果ehcache中没资料,从redis中取资料

cacheValue = cacheFactory.redisGet(name,key);

if(StringUtils.isEmpty(cacheValue)) {

//如果redis中没有资料

// 呼叫业务方法得到结果

result = joinPoint.proceed(args);

//将结果序列化后放入redis

cacheFactory.redisPut(name,key,serialize(result));

} else {

//如果redis中可以取到资料

//将快取中获取到的资料反序列化后返回

if(elementClass == Exception.class) {

result = deserialize(cacheValue, returnType);

} else {

result = deserialize(cacheValue, returnType,elementClass);

}

}

//将结果序列化后放入ehcache

cacheFactory.ehPut(name,key,serialize(result));

} else {

//将快取中获取到的资料反序列化后返回

if(elementClass == Exception.class) {

result = deserialize(cacheValue, returnType);

} else {

result = deserialize(cacheValue, returnType,elementClass);

}

}

} catch (Throwable throwable) {

logger.error("",throwable);

}

return result;

}

/**

* 在方法呼叫前清除快取,然后呼叫业务方法

* @param joinPoint

* @return

* @throws Throwable

*

*/

@Around("cacheEvict()")

public Object evictCache(ProceedingJoinPoint joinPoint) throws Throwable {

// 得到被代理的方法

Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();

//得到被切面修饰的方法的引数列表

Object[] args = joinPoint.getArgs();

// 得到被代理的方法上的注解

CacheEvict ce = method.getAnnotation(CacheEvict.class);

//获得经过el解析后的key值

String key = parseKey(ce.key(),method,args);

//从注解中获取快取名称

String name = ce.value();

// 清除对应快取

cacheFactory.cacheDel(name,key);

return joinPoint.proceed(args);

}

/**

* 获取快取的key

* key 定义在注解上,支援SPEL表示式

* @return

*/

private String parseKey(String key,Method method,Object [] args){

if(StringUtils.isEmpty(key)) return null;

//获取被拦截方法引数名列表(使用Spring支援类库)

LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();

String[] paraNameArr = u.getParameterNames(method);

//使用SPEL进行key的解析

ExpressionParser parser = new SpelExpressionParser();

//SPEL上下文

StandardEvaluationContext context = new StandardEvaluationContext();

//把方法引数放入SPEL上下文中

for(int i=0;i context.setVariable(paraNameArr[i], args[i]);

}

return parser.parseExpression(key).getValue(context,String.class);

}

//序列化

private String serialize(Object obj) {

String result = null;

try {

result = JsonUtil.serialize(obj);

} catch(Exception e) {

result = obj.toString();

}

return result;

}

//反序列化

private Object deserialize(String str,Class clazz) {

Object result = null;

try {

if(clazz == JSONObject.class) {

result = new JSONObject(str);

} else if(clazz == JSONArray.class) {

result = new JSONArray(str);

} else {

result = JsonUtil.deserialize(str,clazz);

}

} catch(Exception e) {

}

return result;

}

//反序列化,支援List

private Object deserialize(String str,Class clazz,Class elementClass) {

Object result = null;

try {

if(clazz == JSONObject.class) {

result = new JSONObject(str);

} else if(clazz == JSONArray.class) {

result = new JSONArray(str);

} else {

result = JsonUtil.deserialize(str,clazz,elementClass);

}

} catch(Exception e) {

}

return result;

}

public void setCacheEnable(boolean cacheEnable) {

this.cacheEnable = cacheEnable;

}

}

上面这个界面使用了一个cacheEnable变数控制是否使用快取,为了实现无缝的接入springboot,必然需要受到原生@EnableCaching注解的控制,这里我使用一个spring容器载入完成的监听器,然后在监听器里找到是否有被@EnableCaching注解修饰的类,如果有就从spring容器拿到MultiCacheAspect物件,然后将cacheEnable设定成true。这样就可以实现无缝接入springboot,不知道朋友们还有没有更加优雅的方法呢?欢迎交流!监听器类如下

import com.xuanwu.apaas.core.multicache.CacheFactory;

import com.xuanwu.apaas.core.multicache.MultiCacheAspect;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.ApplicationListener;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Component;

import java.util.Map;

/**

* 用于spring载入完成后,找到专案中是否有开启快取的注解@EnableCaching

* @author rongdi

*/

@Component

public class ContextRefreshedListener implements ApplicationListener {

@Override

public void onApplicationEvent(ContextRefreshedEvent event) {

// 判断根容器为Spring容器,防止出现呼叫两次的情况(mvc载入也会触发一次)

if(event.getApplicationContext().getParent()==null){

//得到所有被@EnableCaching注解修饰的类

Map beans = event.getApplicationContext().getBeansWithAnnotation(EnableCaching.class);

if(beans != null && !beans.isEmpty()) {

MultiCacheAspect multiCache = (MultiCacheAspect)event.getApplicationContext().getBean("multiCacheAspect");

multiCache.setCacheEnable(true);

}

}

}

}

实现了无缝接入,还需要考虑多点部署的时候,多点的ehcache怎么和redis快取保持一致的问题。在正常应用中,一般redis适合长时间的集中式快取,ehcache适合短时间的本地快取,假设现在有A,B和C服务器,A和B部署了业务服务,C部署了redis服务。当请求进来,前端入口不管是用LVS或者nginx等负载软件,请求都会转发到某一个具体服务器,假设转发到了A服务器,修改了某个内容,而这个内容在redis和ehcache中都有,这时候,A服务器的ehcache快取,和C服务器的redis不管控制快取失效也好,删除也好,都比较容易,但是这时候B服务器的ehcache怎么控制失效或者删除呢?一般比较常用的方式就是使用释出订阅模式,当需要删除快取的时候在一个固定的通道释出一个讯息,然后每个业务服务器订阅这个通道,收到讯息后删除或者过期本地的ehcache快取(最好是使用过期,但是redis目前只支援对key的过期操作,没办法操作key下的map里的成员的过期,如果非要强求用过期,可以自己加时间戳自己实现,不过用删除出问题的概率也很小,毕竟加快取的都是读多写少的应用,这里为了方便都是直接删除快取)。总结起来流程就是更新某条资料,先删除redis中对应的快取,然后释出一个快取失效的讯息在redis的某个通道中,本地的业务服务去订阅这个通道的讯息,当业务服务收到这个讯息后去删除本地对应的ehcache快取,redis的各种配置如下

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.xuanwu.apaas.core.multicache.subscriber.MessageSubscriber;

import org.springframework.cache.CacheManager;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.cache.RedisCacheManager;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.data.redis.listener.PatternTopic;

import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;

import java.util.Map;

@Configuration

public class MultiCacheConfig {

@Bean

public CacheManager cacheManager(RedisTemplate redisTemplate) {

RedisCacheManager rcm = new RedisCacheManager(redisTemplate);

//设定快取过期时间(秒)

Map expires = new HashMap();

expires.put("ExpOpState",0L);

expires.put("ImpOpState",0L);

rcm.setExpires(expires);

rcm.setDefaultExpiration(600);

return rcm;

}

@Bean

public RedisTemplate redisTemplate(RedisConnectionFactory factory) {

StringRedisTemplate template = new StringRedisTemplate(factory);

StringRedisSerializer redisSerializer = new StringRedisSerializer();

template.setValueSerializer(redisSerializer);

template.afterPropertiesSet();

return template;

}

/**

* redis讯息监听器容器

* 可以新增多个监听不同话题的redis监听器,只需要把讯息监听器和相应的讯息订阅处理器系结,该讯息监听器

* 通过反射技术呼叫讯息订阅处理器的相关方法进行一些业务处理

* @param connectionFactory

* @param listenerAdapter

* @return

*/

@Bean

public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,

MessageListenerAdapter listenerAdapter) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

//订阅了一个叫redis.uncache的通道

container.addMessageListener(listenerAdapter, new PatternTopic("redis.uncache"));

//这个container 可以新增多个 messageListener

return container;

}

/**

* 讯息监听器界面卡,系结讯息处理器,利用反射技术呼叫讯息处理器的业务方法

* @param receiver

* @return

*/

@Bean

MessageListenerAdapter listenerAdapter(MessageSubscriber receiver) {

//这个地方 是给messageListenerAdapter 传入一个讯息接受的处理器,利用反射的方法呼叫“handle”

return new MessageListenerAdapter(receiver, "handle");

}

}

讯息释出类如下:

import com.xuanwu.apaas.core.multicache.CacheFactory;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class MessageSubscriber {

private static final Logger logger = LoggerFactory.getLogger(MessageSubscriber.class);

@Autowired

private CacheFactory cacheFactory;

/**

* 接收到redis订阅的讯息后,将ehcache的快取失效

* @param message 格式为name_key

*/

public void handle(String message){

logger.debug("redis.ehcache:"+message);

if(StringUtils.isEmpty(message)) {

return;

}

String[] strs = message.split("#");

String name = strs[0];

String key = null;

if(strs.length == 2) {

key = strs[1];

}

cacheFactory.ehDel(name,key);

}

}

具体操作快取的类如下:

import com.xuanwu.apaas.core.multicache.publisher.MessagePublisher;

import net.sf.ehcache.Cache;

import net.sf.ehcache.CacheManager;

import net.sf.ehcache.Element;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.RedisConnectionFailureException;

import org.springframework.data.redis.core.HashOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import java.io.InputStream;

/**

* 多级快取切面

* @author rongdi

*/

@Component

public class CacheFactory {

private static final Logger logger = LoggerFactory.getLogger(CacheFactory.class);

@Autowired

private RedisTemplate redisTemplate;

@Autowired

private MessagePublisher messagePublisher;

private CacheManager cacheManager;

public CacheFactory() {

InputStream is = this.getClass().getResourceAsStream("/ehcache.xml");

if(is != null) {

cacheManager = CacheManager.create(is);

}

}

public void cacheDel(String name,String key) {

//删除redis对应的快取

redisDel(name,key);

//删除本地的ehcache快取,可以不需要,订阅器那里会删除

// ehDel(name,key);

if(cacheManager != null) {

//释出一个讯息,告诉订阅的服务该快取失效

messagePublisher.publish(name, key);

}

}

public String ehGet(String name,String key) {

if(cacheManager == null) return null;

Cache cache=cacheManager.getCache(name);

if(cache == null) return null;

cache.acquireReadLockOnKey(key);

try {

Element ele = cache.get(key);

if(ele == null) return null;

return (String)ele.getObjectValue();

} finally {

cache.releaseReadLockOnKey(key);

}

}

public String redisGet(String name,String key) {

HashOperations oper = redisTemplate.opsForHash();

try {

return oper.get(name, key);

} catch(RedisConnectionFailureException e) {

//连线失败,不抛错,直接不用redis快取了

logger.error("connect redis error ",e);

return null;

}

}

public void ehPut(String name,String key,String value) {

if(cacheManager == null) return;

if(!cacheManager.cacheExists(name)) {

cacheManager.addCache(name);

}

Cache cache=cacheManager.getCache(name);

//获得key上的写锁,不同key互相不影响,类似于synchronized(key.intern()){}

cache.acquireWriteLockOnKey(key);

try {

cache.put(new Element(key, value));

} finally {

//释放写锁

cache.releaseWriteLockOnKey(key);

}

}

public void redisPut(String name,String key,String value) {

HashOperations oper = redisTemplate.opsForHash();

try {

oper.put(name, key, value);

} catch (RedisConnectionFailureException e) {

//连线失败,不抛错,直接不用redis快取了

logger.error("connect redis error ",e);

}

}

public void ehDel(String name,String key) {

if(cacheManager == null) return;

Cache cache = cacheManager.getCache(name);

if(cache != null) {

//如果key为空,直接根据快取名删除

if(StringUtils.isEmpty(key)) {

cacheManager.removeCache(name);

} else {

cache.remove(key);

}

}

}

public void redisDel(String name,String key) {

HashOperations oper = redisTemplate.opsForHash();

try {

//如果key为空,直接根据快取名删除

if(StringUtils.isEmpty(key)) {

redisTemplate.delete(name);

} else {

oper.delete(name,key);

}

} catch (RedisConnectionFailureException e) {

//连线失败,不抛错,直接不用redis快取了

logger.error("connect redis error ",e);

}

}

}

工具类如下

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.DeserializationFeature;

import com.fasterxml.jackson.databind.JavaType;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;

import org.json.JSONArray;

import org.json.JSONObject;

import java.util.*;

public class JsonUtil {

private static ObjectMapper mapper;

static {

mapper = new ObjectMapper();

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,

false);

}

/**

* 将物件序列化成json

*

* @param obj 待序列化的物件

* @return

* @throws Exception

*/

public static String serialize(Object obj) throws Exception {

if (obj == null) {

throw new IllegalArgumentException("obj should not be null");

}

return mapper.writeValueAsString(obj);

}

/**

带泛型的反序列化,比如一个JSONArray反序列化成List

*/

public static T deserialize(String jsonStr, Class> collectionClass,

Class>... elementClasses) throws Exception {

JavaType javaType = mapper.getTypeFactory().constructParametrizedType(

collectionClass, collectionClass, elementClasses);

return mapper.readValue(jsonStr, javaType);

}

/**

* 将json字串反序列化成物件

* @param src 待反序列化的json字串

* @param t 反序列化成为的物件的class型别

* @return

* @throws Exception

*/

public static T deserialize(String src, Class t) throws Exception {

if (src == null) {

throw new IllegalArgumentException("src should not be null");

}

if("{}".equals(src.trim())) {

return null;

}

return mapper.readValue(src, t);

}

}

具体使用快取,和之前一样只需要关注@Cacheable和@CacheEvict注解,同样也支援spring的el表示式。而且这里的value属性表示的快取名称也没有上面说的那个问题,完全可以用value隔离不同的快取,例子如下

@Cacheable(value = "bo",key="#session.productVersionCode+''+#session.tenantCode+''+#objectcode")

@CacheEvict(value = "bo",key="#session.productVersionCode+''+#session.tenantCode+''+#objectcode")

附上主要的依赖包

"org.springframework.boot:spring-boot-starter-redis:1.4.2.RELEASE",

'net.sf.ehcache:ehcache:2.10.4',

"org.json:json:20160810"

文章标签: 报价宝 降噪耳机价格 耳机价格 红米手机价格 华为手机价格 小米手机价格 电视机价格 笔记本电脑价格 笔记本价格 汽车价格 数码相机价格 汽车价格 笔记本电脑价格 小米手机价格 降噪耳机价格