DRF接口幂等操作

CACHES = {

'default': {

'BACKEND': 'django_redis.cache.RedisCache',

'LOCATION': 'redis://127.0.0.1:6379/1',

'OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient'}

}

}

幂等键保留时间(秒),建议比你业务最长处理时间久一点,比如7天

IDEMPOTENCY_TIMEOUT = 60 * 60 * 24 * 7

decorators.py

from django.core.cache import cachefrom django.http import JsonResponsefrom rest_framework.response import Responsefrom functools import wrapsimport hashlibimport jsondef idempotent(timeout=86400): """ DRF视图幂等装饰器 使用方式: @idempotent(timeout=3600) 放在 post/put 方法上 """ def decorator(view_func): @wraps(view_func) def wrapper(self, request, *args, **kwargs): # 1. 获取幂等键(优先Header,其次请求体) idem_key = request.headers.get('Idempotency-Key') or request.data.get('idempotency_key') if not idem_key: # 如果没有幂等键,直接执行(不阻塞,但非幂等) return view_func(self, request, *args, **kwargs) # 2. 生成唯一缓存Key(结合用户ID和路径,防止不同用户冲突) user_id = request.user.id if request.user.is_authenticated else 'anonymous' path = request.path cache_key = f"idem:{user_id}:{path}:{idem_key}" # 3. 尝试原子性占位(利用cache.add的原子性) # 设置占位值 "PROCESSING",防止并发请求同时执行业务 if not cache.add(cache_key, "PROCESSING", timeout=timeout): # 键已存在,说明请求正在处理或已处理完成 cached_result = cache.get(cache_key) if cached_result == "PROCESSING": # 并发冲突:另一个相同请求正在执行中 # 方案A:轮询等待(不推荐,占用连接) # 方案B:直接返回409,让客户端稍后重试(推荐) return Response( {"error": "Duplicate request is being processed, please retry later"}, status=409 # Conflict ) else: # 已处理完成,直接返回之前缓存的响应 return Response(cached_result.get('data'), status=cached_result.get('status')) # 4. 成功占用锁,执行业务逻辑 try: response = view_func(self, request, *args, **kwargs) # 判断响应类型,提取数据和状态码 if isinstance(response, Response): resp_data = response.data resp_status = response.status_code else: # 兼容普通的HttpResponse/JsonResponse resp_data = json.loads(response.content) if hasattr(response, 'content') else {} resp_status = response.status_code # 缓存成功结果(覆盖 "PROCESSING") cache.set(cache_key, {"data": resp_data, "status": resp_status}, timeout=timeout) return response except Exception as e: # 业务执行失败,删除缓存键,释放锁(允许客户端重试) cache.delete(cache_key) raise e # 继续抛出异常让DRF处理 return wrapper return decorator

views.py

from rest_framework.views import APIViewfrom .decorators import idempotentclass OrderAPIView(APIView): @idempotent(timeout=3600) # 该订单创建接口1小时内幂等 def post(self, request): # 假设这里会扣库存、创建订单 order = create_order(request.data) return Response({"order_id": order.id, "status": "created"}, status=201)

相关推荐
宿州派大星2 小时前
Python 音视频处理流水线:视频分离音频 + 讯飞转写 + 星火大模型解析(附完整代码与踩坑指南)
开发语言·python·音视频
I Am a robert girl2 小时前
DiffRAC:当视频“呼吸”有了节拍器——重复动作计数的新范式
python·深度学习·计算机视觉·视频分析·视频扩散模型·重复动作计数
郝学胜-神的一滴3 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
有个傻瓜3 小时前
容器里时间凭空差 8 小时:naive 与 aware 时区混用排错复盘与四种方案实测
python·docker·springboot·时区排错·naive/aware
心流剑3 小时前
从零打造带降级路由的 Agentic RAG 校园智能体(附前端终端UI实战)
python·ai·rag
天远数科4 小时前
零信任架构实战:基于天远车辆过户详版查询构建自动化抵押车权属合规审查网关
人工智能·python·架构·自动化
badhope4 小时前
3 个 Agent 并行发文章:我用 SQLite 任务表止住重复
后端·python·ai编程
战6175 小时前
Python 基础算法:三种排序、二分查找与递归
开发语言·python·算法
databook5 小时前
马尔可夫链入门:状态、转移概率与无记忆性
python·机器学习·数据分析
郝学胜-神的一滴5 小时前
量化交易从入门到精通 05:策略从哪来
开发语言·python·numpy·pandas·scikit-learn·matplotlib·scipy