日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring boot AOP 实现Redis 存储

發布時間:2025/5/22 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 存储的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。