日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boot AOP 实现Redis 存储

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

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