當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring boot AOP 实现Redis 存储
生活随笔
收集整理的這篇文章主要介紹了
Spring boot AOP 实现Redis 存储
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.carloan.common.web.annotation;import java.lang.annotation.*;/*** 自定義redis緩存注解、只要在service上添加該注解。數據第一次訪問都會加載到redis里**** @author 周志偉***/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisCache {/*** SYS 系統級別* INFO 業務級別* @return*/String type() default "SYS";
} package com.carloan.common.web.aspect;import com.carloan.common.redisTemplate.service.RedisUtils;
import com.carloan.common.utils.SpringUtil;
import com.carloan.common.web.annotation.RedisCache;
import org.apache.commons.lang3.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;/*** @author 周志偉* @projectname 項目名稱: ${project_name}* @classname: RedisAspect* @description:** 定義redis緩存AOP** @date 2018/7/18:16:44*/
@Component
@Aspect
public class RedisAspect {Logger logger = LoggerFactory.getLogger(RedisAspect.class);@Around("@annotation(redisCache)")public Object doValid(ProceedingJoinPoint joinPoint, RedisCache redisCache) throws Throwable {RedisUtils redisUtils=(RedisUtils) SpringUtil.getObject("com.carloan.common.redisTemplate.service.RedisUtils");Object object=null;String key=this.getKey(redisCache.type(),joinPoint);try {if (!redisUtils.exists(key)) {object = joinPoint.proceed();redisUtils.set(key, object);logger.info("插入redis緩存OK---方法名稱{},redis--key{}",joinPoint.getSignature().getName(),key);} else {object = redisUtils.get(key);logger.info("獲取redis緩存OK---方法名稱{},redis--key{}",joinPoint.getSignature().getName(),key);}}catch (Exception e){object = joinPoint.proceed();logger.info("執行異常-RedisAspect---方法名稱{0},redis--key{}",joinPoint.getSignature().getName(),key);e.printStackTrace();}return object ;}public String getKey(String type,ProceedingJoinPoint point) throws NoSuchMethodException {StringBuffer sb = new StringBuffer();Object[] arguments = point.getArgs();Signature sig = point.getSignature();MethodSignature msig = null;msig = (MethodSignature) sig;Object target = point.getTarget();Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());String methodName = currentMethod.getName();String className = point.getTarget().getClass().getName();sb.append(type).append(":").append(className).append(":").append(methodName);if (arguments != null && arguments.length != 0) {for (Object a : arguments) {sb.append(":").append(StringUtils.substringBefore(a.toString(),"@"));}}return sb.toString();}}
?
?調用
?
轉載于:https://www.cnblogs.com/yy123/p/9358501.html
總結
以上是生活随笔為你收集整理的Spring boot AOP 实现Redis 存储的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 域简述
- 下一篇: 用python生成词云wordcloud