使用策略模式+装饰器模式实现接口防重复提交

一、目标场景

  • 提现 / 下单 / 行为记录接口

  • 需要:

    • 防重复点击

    • 不同接口规则不同

    • Redis / 内存都可能用

二、整体设计

@NoRepeatSubmit

AOP(装饰器)

RepeatSubmitStrategy(策略)

Redis / 内存 / Token

每一层职责非常单一

三、第一步:注解

java 复制代码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {

    /** 策略类型 */
    String strategy() default "REDIS";

    /** 间隔时间(毫秒) */
    long interval() default 1500;
}

四、第二步:策略模式

1 策略接口

java 复制代码
public interface RepeatSubmitStrategy {

    boolean isRepeat(String key, long interval);
}

2 Redis 实现

java 复制代码
@Component("REDIS")
public class RedisRepeatSubmitStrategy implements RepeatSubmitStrategy {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean isRepeat(String key, long interval) {
        Boolean success = redisTemplate.opsForValue()
                .setIfAbsent(key, "1", interval, TimeUnit.MILLISECONDS);
        return Boolean.FALSE.equals(success);
    }
}

3 内存实现(兜底 / 本地)

java 复制代码
@Component("LOCAL")
public class LocalRepeatSubmitStrategy implements RepeatSubmitStrategy {

    private final Map<String, Long> cache = new ConcurrentHashMap<>();

    @Override
    public boolean isRepeat(String key, long interval) {
        long now = System.currentTimeMillis();
        Long last = cache.put(key, now);
        return last != null && now - last < interval;
    }
}

五、第三步:AOP(装饰器模式)

java 复制代码
@Aspect
@Component
public class NoRepeatSubmitAspect {

    @Autowired
    private Map<String, RepeatSubmitStrategy> strategyMap;

    @Around("@annotation(noRepeatSubmit)")
    public Object around(ProceedingJoinPoint pjp,
                          NoRepeatSubmit noRepeatSubmit) throws Throwable {

        String key = buildKey(pjp);
        String strategy = noRepeatSubmit.strategy();
        long interval = noRepeatSubmit.interval();

        RepeatSubmitStrategy handler = strategyMap.get(strategy);

        if (handler.isRepeat(key, interval)) {
            return AjaxResult.error("操作太频繁,请稍后再试");
        }

        return pjp.proceed();
    }

    private String buildKey(ProceedingJoinPoint pjp) {
        // userId + method + 参数摘要
        return pjp.getSignature().toShortString();
    }
}

六、Controller 使用

java 复制代码
@PostMapping("/withdraw")
@NoRepeatSubmit(strategy = "REDIS", interval = 1500)
public AjaxResult withdraw() {
    // 业务逻辑非常干净
    return AjaxResult.success();
}

通过策略模式+装饰器模式的防重复提交实现:

  • Controller 0 if / 0 try-catch

  • 新加规则:

    • 新加一个 Strategy

    • 不动旧代码

  • Redis / 本地 / Token:

    • 随时切
相关推荐
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖2 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时2 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi2 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号2 天前
java函数式编程--入门基础
java·编程·函数式编程