中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

寧陵做網(wǎng)站雅虎搜索引擎中文版

寧陵做網(wǎng)站,雅虎搜索引擎中文版,電腦網(wǎng)站適應(yīng)手機(jī)如何做,長沙網(wǎng)站優(yōu)化對策AOP使用案例 如何進(jìn)行數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步?/ 你在項(xiàng)目的那些地方使用了aop?答:可以通過Aop操作來實(shí)現(xiàn)數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步。/ 通過Aop操作來實(shí)現(xiàn)數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步??梢远x一個(gè)切面類,通過對控制器下的所有…

AOP使用案例

如何進(jìn)行數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步?/ 你在項(xiàng)目的那些地方使用了aop?
答:可以通過Aop操作來實(shí)現(xiàn)數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步。/ 通過Aop操作來實(shí)現(xiàn)數(shù)據(jù)庫和Redis中的數(shù)據(jù)同步。
可以定義一個(gè)切面類,通過對控制器下的所有方法進(jìn)行環(huán)繞通知。
數(shù)據(jù)同步有兩種情況

  1. 一種是服務(wù)器接收get請求,首先從Redis中取,沒有對應(yīng)的key再執(zhí)行方法從數(shù)據(jù)庫中獲取數(shù)據(jù)并添加到Redis中;
  2. 第二種情況是服務(wù)器接收寫請求,包括增刪改,這時(shí)就需要先對Redis中的數(shù)據(jù)進(jìn)行掃描,對特定key對應(yīng)的的數(shù)據(jù)進(jìn)行刪除清空,再執(zhí)行方法修改數(shù)據(jù)庫中的內(nèi)容(沒有考慮再次將數(shù)據(jù)庫中的數(shù)據(jù)同步到Redis是因?yàn)?#xff1a;如果服務(wù)器接收到任一get請求,都會(huì)自動(dòng)進(jìn)行同步)
import cn.cnmd.redis.RedisService;
import com.alibaba.fastjson2.JSON;
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.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;import java.lang.reflect.Method;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;@Aspect
@Component
public class RedisCacheAspect {private static Random random = new Random();@Autowiredprivate RedisService redisService;@Pointcut("execution(* cn.ctmd.electric.*.controller.*(..))")private void pointcut() {}@Around("pointcut()")public Object around(ProceedingJoinPoint pjp) throws Throwable {Signature signature = pjp.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();String className = method.getDeclaringClass().getSimpleName();String methodName = method.getName();if (method.isAnnotationPresent(GetMapping.class)) {// get請求Object[] args = pjp.getArgs();String cacheKey = className + "::" + methodName + JSON.toJSONString(args);if (Boolean.TRUE.equals(redisService.hasKey(cacheKey))) {return redisService.get(cacheKey);} else {synchronized (this) {if (Boolean.FALSE.equals(redisService.hasKey(cacheKey))) {Object value = pjp.proceed();long expireTime = Duration.ofMinutes(5).toMillis() + random.nextInt(1000);redisService.set(cacheKey, value, expireTime, TimeUnit.MILLISECONDS);return value;} else {return redisService.get(cacheKey);}}}} else {if (method.isAnnotationPresent(PostMapping.class) || method.isAnnotationPresent(PutMapping.class) || method.isAnnotationPresent(DeleteMapping.class)) {List<String> list = redisService.scan(className, 50);if (list != null) {redisService.delete(list.toString());}}}return pjp.proceed();}
}

AOP

概念:面向切面編程

術(shù)語

  • 連接點(diǎn):被攔截到的程序的執(zhí)行點(diǎn)(在spring中就是被攔截到的方法)
  • 切入點(diǎn):對需要進(jìn)行攔截的條件的定義(某個(gè)位置)
  • 通知、增強(qiáng):為切入點(diǎn)添加二維的功能
  • 目標(biāo)對象:要被增強(qiáng)的對象
  • 織入:將切面和業(yè)務(wù)邏輯對象連接起來,并創(chuàng)建通知代理的過程
  • 代理:被織入后產(chǎn)生的結(jié)果類
  • 切面:一個(gè)橫切關(guān)注點(diǎn)的模塊化(一個(gè)切面類的代稱)

類型

  1. 前置通知
  2. 后置通知
  3. 環(huán)繞通知
  4. 異常拋出通知
  5. 最終通知(少見)

一個(gè)切面類

@Aspect
public class AspectJAdvice {@Before(value = "execution(* com.qf.spring.aop.service..*(..))")public void before(JoinPoint jp){Object[] args = jp.getArgs(); //獲取方法參數(shù)Signature signature = jp.getSignature(); //獲取簽名if(signature instanceof MethodSignature){ //如果簽名是方法簽名Method method = ((MethodSignature) signature).getMethod(); //獲取方法String methodName = method.getName();String className = method.getDeclaringClass().getName();System.out.println("準(zhǔn)備執(zhí)行方法:" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args));}}@AfterReturning(value = "execution(* com.qf.spring.aop.service..*(..))", returning = "returnValue")public void after(JoinPoint jp, Object returnValue){Object[] args = jp.getArgs(); //獲取方法參數(shù)Signature signature = jp.getSignature(); //獲取簽名if(signature instanceof MethodSignature){ //如果簽名是方法簽名Method method = ((MethodSignature) signature).getMethod(); //獲取方法String methodName = method.getName();String className = method.getDeclaringClass().getName();System.out.println("執(zhí)行完方法:" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args) + ",得到返回值:" + returnValue);}}@AfterThrowing(value = "execution(* com.qf.spring.aop.service..*(..))", throwing = "t")public void exception(JoinPoint jp, Throwable t){Object[] args = jp.getArgs(); //獲取方法參數(shù)Signature signature = jp.getSignature(); //獲取簽名if(signature instanceof MethodSignature){ //如果簽名是方法簽名Method method = ((MethodSignature) signature).getMethod(); //獲取方法String methodName = method.getName();String className = method.getDeclaringClass().getName();System.out.println("執(zhí)行方法時(shí):" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args) + ",發(fā)生了異常:" + t.getMessage());}}@Around("execution(* com.qf.spring.aop.service..*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable {Object[] args = pjp.getArgs();//獲取方法的參數(shù)Object target = pjp.getTarget(); //獲取代理對象Signature signature = pjp.getSignature(); //獲取簽名if(signature instanceof MethodSignature) { //如果簽名是方法簽名Method method = ((MethodSignature) signature).getMethod(); //獲取被攔截的方法對象String methodName = method.getName();String className = method.getDeclaringClass().getName();try {System.out.println("準(zhǔn)備執(zhí)行方法:" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args));Object returnValue = method.invoke(target, args);System.out.println("執(zhí)行完方法:" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args) + ",得到返回值:" + returnValue);return returnValue;} catch (Throwable t){System.out.println("執(zhí)行方法時(shí):" + className + "." + methodName + ",參數(shù):" + Arrays.toString(args) + ",發(fā)生了異常:" + t.getMessage());throw t;}}return null;}
}
http://m.risenshineclean.com/news/61323.html

相關(guān)文章:

  • 想學(xué)網(wǎng)站制作網(wǎng)絡(luò)軟件開發(fā)
  • php網(wǎng)站開發(fā)需要學(xué)什么怎么宣傳自己新開的店鋪
  • 網(wǎng)站開發(fā)績效考核站長工具友鏈檢測
  • 泰安的網(wǎng)站建設(shè)公司哪家好正規(guī)推廣平臺
  • 購物網(wǎng)站開發(fā)的需求分析常見的網(wǎng)站推廣方式
  • 做網(wǎng)站建設(shè)公司crm在線的提升服務(wù)外貿(mào)網(wǎng)站推廣服務(wù)
  • 上海建設(shè)電動(dòng)車網(wǎng)絡(luò)營銷鄭州優(yōu)化推廣公司
  • 課程網(wǎng)站建設(shè) 碧輝騰樂谷歌seo
  • 網(wǎng)絡(luò)營銷策劃是什么意思seo排名優(yōu)化方法
  • 蘇州怎么制作網(wǎng)頁網(wǎng)站杭州網(wǎng)站seo價(jià)格
  • 廣東哪里網(wǎng)站建設(shè)安卓優(yōu)化神器
  • seopc流量排名官網(wǎng)超級seo外鏈
  • 免費(fèi)個(gè)人crmapp網(wǎng)站優(yōu)化排名易下拉穩(wěn)定
  • 外貿(mào)狼成都seo的方法
  • 做線下活動(dòng)的網(wǎng)站武漢seo網(wǎng)絡(luò)優(yōu)化公司
  • 云南酒店網(wǎng)站建設(shè)名優(yōu)網(wǎng)站關(guān)鍵詞優(yōu)化
  • 小游戲網(wǎng)址鏈接seo網(wǎng)站推廣優(yōu)化論文
  • java做直播網(wǎng)站有哪些軟件有哪些微信加人推碼35一單
  • 商務(wù)網(wǎng)站建設(shè)管理思路青島seo經(jīng)理
  • 做的網(wǎng)站怎么發(fā)布到網(wǎng)上網(wǎng)絡(luò)推廣方案怎么寫
  • 模仿網(wǎng)站怎么防止侵權(quán)電商關(guān)鍵詞一般用哪些工具
  • icp備案網(wǎng)站要先建好嗎微信社群營銷
  • 北京網(wǎng)站建設(shè)價(jià)格天湛江今日頭條新聞
  • 鄭州網(wǎng)站的優(yōu)化廣州公關(guān)公司
  • 網(wǎng)站建設(shè)外包排名游戲推廣在哪里接活
  • app網(wǎng)站制作要多少錢排名nba
  • 做電影網(wǎng)站的工具抖音關(guān)鍵詞搜索排名
  • 做網(wǎng)站最主要搜索引擎推廣的費(fèi)用
  • 政府網(wǎng)站建設(shè)辦法中國工商業(yè)聯(lián)合會(huì)
  • 如何通過建設(shè)網(wǎng)站賺錢天津疫情最新情況