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

一、目标场景

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

  • 需要:

    • 防重复点击

    • 不同接口规则不同

    • 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:

    • 随时切
相关推荐
YsyaaabB5 小时前
ACM 模式通用代码模板
java·c++·python·算法
IT界的老黄牛5 小时前
从 MQ 积压追到事件总线:诊断 4K 线程吃光 7G 内存的实战
java·运维·rocketmq
小旭95275 小时前
商品详情实现与缓存问题(穿透、击穿、雪崩)解决方案
java·数据库·spring boot·后端·缓存
苦逼的猿宝6 小时前
基于springboot的课程作业管理系统(源码+论文)
java·毕业设计·springboot·计算机毕业设计
我本楚狂人www6 小时前
Spring 两大核心思想(一):IoC
java·数据库·spring
九皇叔叔6 小时前
高斯性能分析【第一天】单表执行计划分析
java·数据库·性能分析·执行计划·gauss
苦逼的猿宝6 小时前
基于springboot的社区团购系统设计(源码+论文)
java·毕业设计·springboot·计算机毕业设计
电魂泡哥6 小时前
RocketMQ Dledger 集群与 Raft 协议
java·rocketmq·java-rocketmq
行走的蜗牛6 小时前
【springai】 Model层设计与实现
java·ai编程
认真的薛薛6 小时前
Linux基础:GitOps发布流程
java·linux·运维