承接 codenaline/laravel-idempotency 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codenaline/laravel-idempotency

Composer 安装命令:

composer require codenaline/laravel-idempotency

包简介

Laravel middleware and drivers for handling Idempotency Keys (Cache, Database, Redis).

README 文档

README

A professional Laravel package for handling Idempotency Keys.
It ensures that duplicate requests (like hitting the payment button twice) are processed only once, safely returning the same response.

🚀 Features

  • Support for Idempotency-Key header in requests (customizable)
  • Three storage drivers:
    • Redis → blazing fast, ideal for high‑traffic environments
    • Database → persistent and easy to monitor
    • Cache → simple and flexible using Laravel’s cache system
  • TTL (time‑to‑live) support for automatic expiration
  • Artisan command to purge expired keys (Database driver only)
  • Full Pest test coverage (Feature, Unit, Command tests)

📥 Installation

composer require codenaline/laravel-idempotency

⚙️ Configuration

Publish the config file:

php artisan vendor:publish --tag=idempotency-config

Example config/idempotency.php

return [

    'header' => env('IDEMPOTENCY_HEADER', 'Idempotency-Key'),

    'default' => env('IDEMPOTENCY_DRIVER', 'redis'),

    'drivers' => [

        'redis' => [
            'connection' => env('IDEMPOTENCY_REDIS_CONNECTION', 'default'),
            'ttl' => env('IDEMPOTENCY_TTL', 60),
        ],

        'database' => [
            'connection' => env('IDEMPOTENCY_DB_CONNECTION', config('database.default')),
            'table' => env('IDEMPOTENCY_DB_TABLE', 'idempotency_keys'),
            'ttl' => env('IDEMPOTENCY_TTL', 60),
        ],

        'cache' => [
            'store' => env('IDEMPOTENCY_CACHE_STORE'),
            'ttl' => env('IDEMPOTENCY_TTL', 60),
        ],
    ],
];

🔑 Custom Header

By default, the package looks for the header:

Idempotency-Key: abc123

You can change the header name in config/idempotency.php:

'header' => env('IDEMPOTENCY_HEADER', 'Idempotency-Key'),

For example, if you want to use X-Request-Id instead:

IDEMPOTENCY_HEADER=X-Request-Id

Then your requests should include:

POST /payments
X-Request-Id: abc123

🛠️ Usage

Add the middleware to your routes:

Attach the middleware to your routes to enable idempotency:

Route::post('/payments', [PaymentController::class, 'store'])
    ->middleware('idempotency');

By default, the middleware will use the TTL value defined in your configuration file (config/idempotency.php).

You can override the TTL for a specific route by passing it as a parameter to the middleware:

Route::post('/payments', [PaymentController::class, 'store'])
    ->middleware('idempotency:120'); // TTL = 120 seconds

This allows you to fine‑tune expiration times depending on the sensitivity of each endpoint.
For example:

  • Short TTL (e.g. 30 seconds) for lightweight requests.
  • Longer TTL (e.g. 600 seconds) for heavy operations like payment processing.

Each request must include the idempotency header.
If the same key is reused, the previous response will be returned instead of re‑processing.

🧹 Purging Expired Keys

  • Redis / Cache drivers → no purge needed, TTL is handled automatically by the storage engine.
  • Database driver → purge is required to clean up expired rows.

Run the Artisan command manually:

php artisan idempotency:purge

Schedule it in app/Console/Kernel.php:

protected function schedule(Schedule $schedule): void
{
    // Run every hour
    $schedule->command('idempotency:purge')->hourly();

    // Or daily
    // $schedule->command('idempotency:purge')->daily();
}

🧪 Testing

This package ships with full Pest tests:

  • Feature tests for middleware flow
  • Unit tests for each driver (Cache, Database, Redis)
  • Command tests for purging expired keys (Database driver only)

📌 Summary

  • Prevents duplicate request execution
  • Supports three drivers: Redis, Database, Cache
  • TTL expiration included
  • Header name customizable (Idempotency-Key by default)
  • Purge command only needed for Database driver, can be scheduled in the Laravel Kernel
  • Ready for production with full test coverage

🤝 Contributing

Pull requests are welcome! Please see CONTRIBUTING for details.

👨🏻‍💻 Credits

📄 License

The MIT License (MIT). Please see License File for more information.

codenaline/laravel-idempotency 适用场景与选型建议

codenaline/laravel-idempotency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「database」 「cache」 「middleware」 「laravel」 「idempotency」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codenaline/laravel-idempotency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codenaline/laravel-idempotency 我们能提供哪些服务?
定制开发 / 二次开发

基于 codenaline/laravel-idempotency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-26