dabergut/laravel-idempotency
Composer 安装命令:
composer require dabergut/laravel-idempotency
包简介
Extracting duplicated request handling so you don't have to.
README 文档
README
Drop-in middleware that prevents duplicate POST/PATCH processing in your Laravel API.
Client sends an Idempotency-Key header, server stores the response. Same key comes in again — cached response goes back, controller never fires twice. No double charges, no duplicate orders, no angry customers.
Why
Every API that mutates state has the same problem: the client sends a request, something hiccups (timeout, flaky connection, eager retry logic), and the same request arrives twice. Without idempotency handling your API happily processes it again.
You can write this yourself. I've done it about four times across different projects before extracting it into this package. The tricky bits are locking (concurrent duplicate requests), body fingerprinting (same key, different payload), and scoping keys per user.
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
composer require dabergut/laravel-idempotency
The service provider registers automatically. Publish the config if you need to tweak anything:
php artisan vendor:publish --tag=idempotency-config
Usage
Add the middleware to routes that shouldn't be processed twice:
Route::post('/orders', CreateOrderController::class) ->middleware('idempotent');
Or apply it to a group:
Route::middleware('idempotent')->group(function () { Route::post('/orders', CreateOrderController::class); Route::post('/payments', ProcessPaymentController::class); Route::patch('/orders/{order}', UpdateOrderController::class); });
That's it. Your clients need to send an Idempotency-Key header with their requests:
POST /api/orders HTTP/1.1
Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000
Content-Type: application/json
{"product_id": 42, "quantity": 1}
First request processes normally. Second request with the same key returns the stored response without hitting your controller.
Response headers
Every response from an idempotent endpoint includes:
| Header | Value | Meaning |
|---|---|---|
Idempotent-Replayed |
false |
Fresh response, controller executed |
Idempotent-Replayed |
true |
Cached response, controller skipped |
What happens when
| Scenario | Result |
|---|---|
No Idempotency-Key header |
Request processed normally, no caching |
| Key present, first time seen | Request processed, response cached |
| Key present, seen before, same body | Cached response returned (201, not 200) |
| Key present, seen before, different body | 422 error — key reuse with different payload |
| Key too short (< 8 chars by default) | 422 error |
| Concurrent duplicate requests | Second request waits for lock, then returns cached response |
| GET or DELETE request | Middleware does nothing (idempotent by HTTP spec) |
Configuration
// config/idempotency.php return [ // Header name. Stripe uses the same one. 'header' => 'Idempotency-Key', // How long to keep stored responses (minutes). Default: 24 hours. 'ttl' => 1440, // Cache store. null = your default driver. Redis recommended. 'store' => null, // Which HTTP methods to enforce on. 'methods' => ['POST', 'PATCH'], // Minimum key length. UUIDs are 36 chars. 'min_key_length' => 8, // Reject reused keys with different request bodies. 'enforce_body_match' => true, ];
User scoping
Keys are automatically scoped to the authenticated user. User A and User B can both send Idempotency-Key: abc without collision. Unauthenticated requests share a global scope — keep that in mind if your public endpoints use this middleware.
Cache backend
This works with any Laravel cache driver, but use Redis in production. File cache works for development but won't survive deployments and doesn't support atomic locks properly.
IDEMPOTENCY_STORE=redis
Testing
composer test
License
MIT
dabergut/laravel-idempotency 适用场景与选型建议
dabergut/laravel-idempotency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「middleware」 「requests」 「laravel」 「duplicate」 「idempotency」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dabergut/laravel-idempotency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dabergut/laravel-idempotency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dabergut/laravel-idempotency 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Rest client to make GET, POST, PUT, DELETE, PATCH, etc calls.
This package is free and can be used for API Brazil website functions
A simple package to better handle application requests.
A PSR-7 compatible library for making CRUD API endpoints
Slim Framework 3 CSRF protection middleware utilities
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-18