优化 缓存切面

v1.4.1
Parker 4 years ago
parent 1693c5ed56
commit 06c82ee572

@ -1,15 +1,19 @@
package org.opsli.core.aspect; package org.opsli.core.aspect;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.opsli.common.annotation.EnableHotData;
import org.opsli.common.annotation.HotDataDel; import org.opsli.common.annotation.HotDataDel;
import org.opsli.common.annotation.HotDataPut; import org.opsli.common.annotation.HotDataPut;
import org.opsli.common.base.entity.BaseEntity; import org.opsli.core.base.entity.BaseEntity;
import org.opsli.common.constants.CacheConstants; import org.opsli.common.constants.CacheConstants;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.entity.CacheDataEntity;
import org.opsli.core.cache.pushsub.enums.CacheType; import org.opsli.core.cache.pushsub.enums.CacheType;
import org.opsli.core.cache.pushsub.enums.PushSubType; import org.opsli.core.cache.pushsub.enums.PushSubType;
import org.opsli.core.cache.pushsub.msgs.CacheDataMsgFactory; import org.opsli.core.cache.pushsub.msgs.CacheDataMsgFactory;
@ -18,7 +22,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import static org.opsli.common.constants.OrderConstants.HOT_DATA_ORDER; import static org.opsli.common.constants.OrderConstants.HOT_DATA_ORDER;
@ -48,12 +55,101 @@ public class CacheDataAop {
public void hotDataDel() { public void hotDataDel() {
} }
/**
*
* @param point
* @return
* @throws Throwable
*/
@Around("hotDataPut()") @Around("hotDataPut()")
public Object hotDataPutProcess(ProceedingJoinPoint point) throws Throwable { public Object hotDataPutProcess(ProceedingJoinPoint point) throws Throwable {
Object[] args= point.getArgs(); Object[] args= point.getArgs();
Object returnValue = point.proceed(args); Object returnValue = point.proceed(args);
// 将 // 判断 方法上是否使用 HotData注解 如果没有表示开启热数据 则直接跳过
if(returnValue != null){ Annotation annotation = point.getTarget().getClass().getAnnotation(EnableHotData.class);
if(annotation == null){
return returnValue;
}
// ====== 如果 使用了 EnableHotData ,表示开启热数据加载 则执行下段代码
CacheDataEntity cacheDataEntity = this.putHandlerData(point, returnValue);
if(cacheDataEntity != null){
// 更新缓存数据
// 热点数据
if(CacheConstants.HOT_DATA.equals(cacheDataEntity.getCacheName())){
CacheUtil.putByKeyOriginal(cacheDataEntity.getKey(), returnValue);
}
// 永久数据
else if(CacheConstants.EDEN_DATA.equals(cacheDataEntity.getCacheName())) {
CacheUtil.putEdenByKeyOriginal(cacheDataEntity.getKey(), returnValue);
}
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
CacheDataMsgFactory.createMsg(cacheDataEntity.getType(),
cacheDataEntity.getKey(), returnValue, CacheType.UPDATE)
);
}
return returnValue;
}
/**
*
* @param point
* @return
* @throws Throwable
*/
@Around("hotDataDel()")
public Object hotDataDelProcess(ProceedingJoinPoint point) throws Throwable {
Object[] args= point.getArgs();
Object returnValue = point.proceed(args);
// 判断 方法上是否使用 HotData注解 如果没有表示开启热数据 则直接跳过
Annotation annotation = point.getTarget().getClass().getAnnotation(EnableHotData.class);
if(annotation == null){
return returnValue;
}
// ====== 如果 使用了 EnableHotData ,表示开启热数据加载 则执行下段代码
List<CacheDataEntity> cacheDataEntityList = this.delHandlerData(point, args);
if(cacheDataEntityList != null && cacheDataEntityList.size() > 0){
for (CacheDataEntity cacheDataEntity : cacheDataEntityList) {
// 更新缓存数据 - 删除缓存
CacheUtil.del(cacheDataEntity.getKey());
// 广播缓存数据 - 通知其他服务器同步数据
redisPlugin.sendMessage(
CacheDataMsgFactory.createMsg(cacheDataEntity.getType(),
cacheDataEntity.getKey(), returnValue, CacheType.DELETE)
);
}
}
return returnValue;
}
// ===========================================================================
/***
* PUT
* @param point
*/
private CacheDataEntity putHandlerData(ProceedingJoinPoint point, Object returnValue){
CacheDataEntity ret;
// 返回值为空直接
if(returnValue == null){
return null;
}
// 这里 只对 继承了 BaseEntity 的类做处理
if(!(returnValue instanceof BaseEntity)){
return null;
}
// 报错不处理
try {
String methodName= point.getSignature().getName(); String methodName= point.getSignature().getName();
Class<?> classTarget= point.getTarget().getClass(); Class<?> classTarget= point.getTarget().getClass();
Class<?>[] par=((MethodSignature) point.getSignature()).getParameterTypes(); Class<?>[] par=((MethodSignature) point.getSignature()).getParameterTypes();
@ -76,7 +172,7 @@ public class CacheDataAop {
type = PushSubType.EDEN_DATA; type = PushSubType.EDEN_DATA;
} else { } else {
// 如果都不是 则直接退出 不走缓存 // 如果都不是 则直接退出 不走缓存
return returnValue; return null;
} }
try { try {
@ -86,26 +182,37 @@ public class CacheDataAop {
// key 存储ID // key 存储ID
String key = keyBuf.append(baseEntity.getId()).toString(); String key = keyBuf.append(baseEntity.getId()).toString();
// 广播数据 ret = new CacheDataEntity();
redisPlugin.sendMessage( ret.setKey(key);
CacheDataMsgFactory.createMsg(type, key, returnValue, CacheType.UPDATE) ret.setType(type);
); ret.setCacheName(aCache.name());
return ret;
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
} }
}catch (Exception e){
log.error(e.getMessage(),e);
} }
return returnValue; return null;
} }
/***
* DEL
* @param point
*/
private List<CacheDataEntity> delHandlerData(ProceedingJoinPoint point, Object[] args){
if(args == null || args.length == 0){
return null;
}
// DEL 消息集合
List<CacheDataEntity> cacheDataEntities = Lists.newArrayListWithCapacity(args.length);
@Around("hotDataDel()") // 报错不处理
public Object hotDataDelProcess(ProceedingJoinPoint point) throws Throwable { try {
Object[] args= point.getArgs();
Object returnValue = point.proceed(args);
// 将
if(returnValue != null){
String methodName= point.getSignature().getName(); String methodName= point.getSignature().getName();
Class<?> classTarget= point.getTarget().getClass(); Class<?> classTarget= point.getTarget().getClass();
Class<?>[] par=((MethodSignature) point.getSignature()).getParameterTypes(); Class<?>[] par=((MethodSignature) point.getSignature()).getParameterTypes();
@ -128,26 +235,67 @@ public class CacheDataAop {
type = PushSubType.EDEN_DATA; type = PushSubType.EDEN_DATA;
} else { } else {
// 如果都不是 则直接退出 不走缓存 // 如果都不是 则直接退出 不走缓存
return returnValue; return null;
} }
try { try {
// 这里 只对 继承了 BaseEntity 的类做处理
BaseEntity baseEntity = (BaseEntity) returnValue;
// key 存储ID // 处理数据
String key = keyBuf.append(baseEntity.getId()).toString(); for (Object arg : args) {
if (arg instanceof String) {
// 广播数据 // key 存储ID
redisPlugin.sendMessage( String key = keyBuf.toString() + arg;
CacheDataMsgFactory.createMsg(type, key, returnValue, CacheType.DELETE) CacheDataEntity ret = new CacheDataEntity();
); ret.setKey(key);
ret.setType(type);
ret.setCacheName(aCache.name());
cacheDataEntities.add(ret);
} else if (arg instanceof String[]) {
String[] ids = (String[]) arg;
for (String id : ids) {
// key 存储ID
String key = keyBuf.toString() + id;
CacheDataEntity ret = new CacheDataEntity();
ret.setKey(key);
ret.setType(type);
ret.setCacheName(aCache.name());
cacheDataEntities.add(ret);
}
} else if (arg instanceof BaseEntity) {
// key 存储ID
BaseEntity baseEntity = (BaseEntity) arg;
String key = keyBuf.toString() + baseEntity.getId();
CacheDataEntity ret = new CacheDataEntity();
ret.setKey(key);
ret.setType(type);
ret.setCacheName(aCache.name());
cacheDataEntities.add(ret);
} else if (arg instanceof Collection) {
try {
Collection<BaseEntity> baseEntityList = (Collection<BaseEntity>) arg;
for (BaseEntity baseEntity : baseEntityList) {
// key 存储ID
String key = keyBuf.toString() + baseEntity.getId();
CacheDataEntity ret = new CacheDataEntity();
ret.setKey(key);
ret.setType(type);
ret.setCacheName(aCache.name());
cacheDataEntities.add(ret);
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
}
return cacheDataEntities;
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
} }
}catch (Exception e){
log.error(e.getMessage(),e);
} }
return returnValue; return null;
} }
} }

Loading…
Cancel
Save