基础版

  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. /**
  6. * 普通 Map 版本
  7. */
  8. @RequestMapping("/user")
  9. @RestController
  10. public class UserController3 {
  11. // 缓存 ID 集合
  12. private Map<String, Integer> reqCache = new HashMap<>();
  13. @RequestMapping("/add")
  14. public String addUser(String id) {
  15. // 非空判断(忽略)...
  16. synchronized (this.getClass()) {
  17. // 重复请求判断
  18. if (reqCache.containsKey(id)) {
  19. // 重复请求
  20. System.out.println("请勿重复提交!!!" + id);
  21. return "执行失败";
  22. }
  23. // 存储请求 ID
  24. reqCache.put(id, 1);
  25. }
  26. // 业务代码...
  27. System.out.println("添加用户ID:" + id);
  28. return "执行成功!";
  29. }
  30. }

存在的问题:此实现方式有一个致命的问题,因为 HashMap 是无限增长的,因此它会占用越来越多的内存,并且随着 HashMap 数量的增加查找的速度也会降低,所以我们需要实现一个可以自动“清除”过期数据的实现方案。

优化版——固定大小的数组

此版本解决了 HashMap 无限增长的问题,它使用数组加下标计数器(reqCacheCounter)的方式,实现了固定数组的循环存储。
当数组存储到最后一位时,将数组的存储下标设置 0,再从头开始存储数据,实现代码如下:

  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. import java.util.Arrays;
  4. @RequestMapping("/user")
  5. @RestController
  6. public class UserController {
  7. private static String[] reqCache = new String[100]; // 请求 ID 存储集合
  8. private static Integer reqCacheCounter = 0; // 请求计数器(指示 ID 存储的位置)
  9. @RequestMapping("/add")
  10. public String addUser(String id) {
  11. // 非空判断(忽略)...
  12. synchronized (this.getClass()) {
  13. // 重复请求判断
  14. if (Arrays.asList(reqCache).contains(id)) {
  15. // 重复请求
  16. System.out.println("请勿重复提交!!!" + id);
  17. return "执行失败";
  18. }
  19. // 记录请求 ID
  20. if (reqCacheCounter >= reqCache.length) reqCacheCounter = 0; // 重置计数器
  21. reqCache[reqCacheCounter] = id; // 将 ID 保存到缓存
  22. reqCacheCounter++; // 下标往后移一位
  23. }
  24. // 业务代码...
  25. System.out.println("添加用户ID:" + id);
  26. return "执行成功!";
  27. }
  28. }

扩展版——双重检测锁(DCL)

上一种实现方法将判断和添加业务,都放入 synchronized 中进行加锁操作,这样显然性能不是很高,于是我们可以使用单例中著名的 DCL(Double Checked Locking,双重检测锁)来优化代码的执行效率,实现代码如下:

  1. import org.springframework.web.bind.annotation.RequestMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. import java.util.Arrays;
  4. @RequestMapping("/user")
  5. @RestController
  6. public class UserController {
  7. private static String[] reqCache = new String[100]; // 请求 ID 存储集合
  8. private static Integer reqCacheCounter = 0; // 请求计数器(指示 ID 存储的位置)
  9. @RequestMapping("/add")
  10. public String addUser(String id) {
  11. // 非空判断(忽略)...
  12. // 重复请求判断
  13. if (Arrays.asList(reqCache).contains(id)) {
  14. // 重复请求
  15. System.out.println("请勿重复提交!!!" + id);
  16. return "执行失败";
  17. }
  18. synchronized (this.getClass()) {
  19. // 双重检查锁(DCL,double checked locking)提高程序的执行效率
  20. if (Arrays.asList(reqCache).contains(id)) {
  21. // 重复请求
  22. System.out.println("请勿重复提交!!!" + id);
  23. return "执行失败";
  24. }
  25. // 记录请求 ID
  26. if (reqCacheCounter >= reqCache.length) reqCacheCounter = 0; // 重置计数器
  27. reqCache[reqCacheCounter] = id; // 将 ID 保存到缓存
  28. reqCacheCounter++; // 下标往后移一位
  29. }
  30. // 业务代码...
  31. System.out.println("添加用户ID:" + id);
  32. return "执行成功!";
  33. }
  34. }

完善版——LRUMap

上面的代码基本已经实现了重复数据的拦截,但显然不够简洁和优雅,比如下标计数器的声明和业务处理等,但值得庆幸的是 Apache 为我们提供了一个 commons-collections 的框架,里面有一个非常好用的数据结构 LRUMap 可以保存指定数量的固定的数据,并且它会按照 LRU 算法,帮你清除最不常用的数据。

小贴士:LRU 是 Least Recently Used 的缩写,即最近最少使用,是一种常用的数据淘汰算法,选择最近最久未使用的数据予以淘汰。

首先,我们先来添加 Apache commons collections 的引用:

  1. <!-- 集合工具类 apache commons collections -->
  2. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
  3. <dependency>
  4. <groupId>org.apache.commons</groupId>
  5. <artifactId>commons-collections4</artifactId>
  6. <version>4.4</version>
  7. </dependency>
  1. import org.apache.commons.collections4.map.LRUMap;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RequestMapping("/user")
  5. @RestController
  6. public class UserController {
  7. // 最大容量 100 个,根据 LRU 算法淘汰数据的 Map 集合
  8. private LRUMap<String, Integer> reqCache = new LRUMap<>(100);
  9. @RequestMapping("/add")
  10. public String addUser(String id) {
  11. // 非空判断(忽略)...
  12. synchronized (this.getClass()) {
  13. // 重复请求判断
  14. if (reqCache.containsKey(id)) {
  15. // 重复请求
  16. System.out.println("请勿重复提交!!!" + id);
  17. return "执行失败";
  18. }
  19. // 存储请求 ID
  20. reqCache.put(id, 1);
  21. }
  22. // 业务代码...
  23. System.out.println("添加用户ID:" + id);
  24. return "执行成功!";
  25. }
  26. }

最终版——封装

以上都是方法级别的实现方案,然而在实际的业务中,我们可能有很多的方法都需要防重,那么接下来我们就来封装一个公共的方法,以供所有类使用:

  1. import org.apache.commons.collections4.map.LRUMap;
  2. /**
  3. * 幂等性判断
  4. */
  5. public class IdempotentUtils {
  6. // 根据 LRU(Least Recently Used,最近最少使用)算法淘汰数据的 Map 集合,最大容量 100 个
  7. private static LRUMap<String, Integer> reqCache = new LRUMap<>(100);
  8. /**
  9. * 幂等性判断
  10. * @return
  11. */
  12. public static boolean judge(String id, Object lockClass) {
  13. synchronized (lockClass) {
  14. // 重复请求判断
  15. if (reqCache.containsKey(id)) {
  16. // 重复请求
  17. System.out.println("请勿重复提交!!!" + id);
  18. return false;
  19. }
  20. // 非重复请求,存储请求 ID
  21. reqCache.put(id, 1);
  22. }
  23. return true;
  24. }
  25. }
  1. import com.example.idempote.util.IdempotentUtils;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RequestMapping("/user")
  5. @RestController
  6. public class UserController4 {
  7. @RequestMapping("/add")
  8. public String addUser(String id) {
  9. // 非空判断(忽略)...
  10. // -------------- 幂等性调用(开始) --------------
  11. if (!IdempotentUtils.judge(id, this.getClass())) {
  12. return "执行失败";
  13. }
  14. // -------------- 幂等性调用(结束) --------------
  15. // 业务代码...
  16. System.out.println("添加用户ID:" + id);
  17. return "执行成功!";
  18. }
  19. }

参考文章
https://juejin.im/post/6850418120985837575#heading-3