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();
        //生成缓存key
        Object 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
    @Override
    public Long getParentIdByName(String name) {
        //根据name查询父级ID
        System.out.println("com.example.test.ThreadLocalTestServiceImpl.getParentIdByName 执行了");
        return 666L;
    }
}
  1. 方法调用
@RestController
@RequestMapping("/ThreadLocalTest")
public class ThreadLocalTest {

    @Autowired
    private ThreadLocalTestService threadLocalTestService;

    @Autowired
    private 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导致的。

相关推荐

  1. ThreadLocal切面实现线级别方法缓存

    2024-04-07 08:32:05       42 阅读
  2. 理解并使用ThreadLocal实现线级别数据隔离

    2024-04-07 08:32:05       52 阅读
  3. ThreadLocal+TaskDecorator实现父子线 参数传递

    2024-04-07 08:32:05       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 08:32:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 08:32:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 08:32:05       87 阅读
  4. Python语言-面向对象

    2024-04-07 08:32:05       96 阅读

热门阅读

  1. 2024/4/6 HarmonyOS学习笔记-图片组件

    2024-04-07 08:32:05       37 阅读
  2. 力扣---***********LRU 缓存***********

    2024-04-07 08:32:05       186 阅读
  3. C语言如何限定外部变量的使⽤范围?

    2024-04-07 08:32:05       34 阅读
  4. 【go从入门到精通】常量和枚举详解

    2024-04-07 08:32:05       31 阅读
  5. 《机器学习在量化投资中的应用研究》目录

    2024-04-07 08:32:05       34 阅读
  6. LeetCode 1049. 最后一块石头的重量 II

    2024-04-07 08:32:05       32 阅读
  7. 网易雷火 暑期实习提前批一面(48min)

    2024-04-07 08:32:05       32 阅读