当前位置: 首页 > news >正文

做网站ai用多大比例海外推广营销系统

做网站ai用多大比例,海外推广营销系统,网站建设数据库模板,网站模板 餐饮1、实现效果 当一个请求线程多次请求A方法时,只会触发一次A方法的实际调用,会将方法结果缓存起来,避免多次调用。 2、实现过程 1. 需要一个注解ThreadLocalCache,在需要缓存的方法上加上该注解 2. 需要一个切面,借助ThreadLocal,将结果缓存起来,利用环绕通知来实现方法拦截从…

1、实现效果

当一个请求线程多次请求A方法时,只会触发一次A方法的实际调用,会将方法结果缓存起来,避免多次调用。

2、实现过程

1. 需要一个注解ThreadLocalCache,在需要缓存的方法上加上该注解
2. 需要一个切面,借助ThreadLocal,将结果缓存起来,利用环绕通知来实现方法拦截从缓存中返回方法执行结果

3、代码实现

3.1、ThreadLocalCache注解创建

作用于方法级别

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ThreadLocalCache {
}

3.2、ThreadLocalTestAspect切面创建

@Aspect
@Component
public class ThreadLocalTestAspect {private ThreadLocal<Map<Object, Object>> threadLocal = new ThreadLocal<>();@Around("@annotation(com.example.test.ThreadLocalCache)")private Object myPointcut(ProceedingJoinPoint proceedingJoinPoint) {//获取方法的入参Object[] args = proceedingJoinPoint.getArgs();Signature signature = proceedingJoinPoint.getSignature();//获取目标方法名String name = signature.getName();//获取目标方法的类的完全限定名String declaringTypeName = signature.getDeclaringTypeName();//生成缓存keyObject key = SimpleKeyGenerator.generateKey(args, declaringTypeName, name);if (Objects.isNull(threadLocal.get())) {threadLocal.set(new HashMap<>(8));}try {if (!threadLocal.get().containsKey(key)) {threadLocal.get().put(key, proceedingJoinPoint.proceed());}} catch (Throwable e) {//日志记录e.printStackTrace();}return threadLocal.get().get(key);}public void removeThreadLocal(){threadLocal.remove();}}

4、测试过程

  1. 创建一个接口及实现
public interface ThreadLocalTestService {Long getParentIdByName(String name);
}
@Service
public class ThreadLocalTestServiceImpl implements ThreadLocalTestService{@ThreadLocalCache@Overridepublic Long getParentIdByName(String name) {//根据name查询父级IDSystem.out.println("com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了");return 666L;}
}
  1. 方法调用
@RestController
@RequestMapping("/ThreadLocalTest")
public class ThreadLocalTest {@Autowiredprivate ThreadLocalTestService threadLocalTestService;@Autowiredprivate ThreadLocalTestAspect threadLocalTestAspect;@GetMapping("getParentIdByName")public Long getParentIdByName(String name){System.out.println(Thread.currentThread().getName());threadLocalTestService.getParentIdByName(name);threadLocalTestService.getParentIdByName(name);Long parentId = threadLocalTestService.getParentIdByName(name);threadLocalTestAspect.removeThreadLocal();return parentId;}}

3.执行结果

http-nio-8087-exec-1
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-2
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-4
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-5
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-6
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-7
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-8
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-10
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-9
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-3
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-1
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-2
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了

http-nio-8087-exec-是线程名字,可以看到http-nio-8087-exec-1执行了两次,每次都调用四次getParentIdByName 方法,但getParentIdByName 方法实际至执行了一次,剩下的三次是从缓存中获取的。
这里需要注意的是:线程每次结束的时候都需要调用threadLocalTestAspect.removeThreadLocal();为的是把当前线程threadLocal里的缓存抹掉,因为同一个线程可能会被重复使用,所以不抹掉,可能会导致多次请求使用同一个线程,目标方法只会执行一次,和我们的最初的实现效果是违背的。
下面是不调用threadLocalTestAspect.removeThreadLocal();的执行结果

http-nio-8087-exec-1
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-3
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-8
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-5
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-6
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-7
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-4
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-9
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-10
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-2
com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了
http-nio-8087-exec-1
http-nio-8087-exec-3
http-nio-8087-exec-8
http-nio-8087-exec-5

可以很清楚的看到http-nio-8087-exec-1、3、5、8再次请求的时候getParentIdByName 方法并没有执行了,因为之前的threadlocal缓存没有被remove导致的。

http://www.tj-hxxt.cn/news/68803.html

相关文章:

  • 做网站技术选择如何做好企业推广
  • 免费网站建设 百度一下广告优化师的工作内容
  • 云南网站建设选天软推广赚钱平台
  • 做网站后付款百度推广客户端登录
  • 电商网站源码百度新闻首页
  • 怎么做可以支付的网站最新实时大数据
  • 网站开发最适合的浏览器seo推广培训资料
  • 服务器做jsp网站教程视频播放百度下载软件
  • php 校园网站设计公众号营销
  • 网站服务器收费商丘搜索引擎优化
  • 申请400客服电话seo搜索引擎优化推广
  • minecraft做图网站哪里有免费的网站推广
  • 设计创意网站推荐店铺推广方法
  • 免费 网站 cms南宁百度推广seo
  • 境外网站可以备案吗成都seo的方法
  • 网站后台建设招聘深圳网站seo
  • 一线城市做网站工资有多少企业百度推广怎么收费
  • 福州市住房和城乡建设网站最新国际新闻事件今天
  • 婚纱摄影网站开发沈阳cms模板建站
  • 外贸商城 wordpress长春seo优化企业网络跃升
  • 湖北响应式网站建设微博营销推广策划方案
  • 网站建设江西网络推广员有前途吗
  • 自主网站建设太原百度关键词优化
  • 文昌网站建设企业网站怎么优化
  • 专业网站建设特点分析怎样在百度上发布信息
  • 阿拉伯语网站怎么做seo技术助理
  • 做网站投资多少钱如何百度收录自己的网站
  • 专门做金融培训的网站有哪些网站建设企业建站
  • discuz做资讯网站软文发稿网
  • 肇庆软件建网站公司济南seo