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

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

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

ocharai/laravel-idempotency

Composer 安装命令:

composer require ocharai/laravel-idempotency

包简介

Laravel idempotency package with multiple storage support

README 文档

README

A robust Laravel package that provides idempotency support for your API endpoints with Redis storage. This package helps prevent duplicate operations when retrying API requests.

Features

  • Redis-based storage for idempotency keys
  • Configurable TTL for idempotency keys
  • Support for multiple HTTP verbs (POST, PUT, PATCH, DELETE)
  • User-specific idempotency keys
  • Automatic response caching for successful JSON responses
  • ULID-based idempotency keys for security and uniqueness

Requirements

  • PHP ^7.4|^8.0|^8.3
  • Laravel ^8.0|^9.0|^10.0|^11.0
  • Redis server

Installation

You can install the package via composer:

composer require ocharai/laravel-idempotency

The package will automatically register its service provider.

Configuration

Publish the configuration files:

php artisan vendor:publish --provider="OCharai\Idempotency\Providers\IdempotencyServiceProvider"

This will create two configuration files:

  • config/idempotency.php: Main package configuration
  • config/database.php: Redis connection configuration

Main Configuration Options

return [
    // The header key used for idempotency
    'header_key' => env('IDEMPOTENCY_HEADER_KEY', 'X-Idempotency-Key'),

    // Time-to-live for idempotency keys (in seconds)
    'ttl' => env('IDEMPOTENCY_TTL', 86400), // 1 day

    // HTTP verbs that require idempotency
    'enforced_verbs' => [
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
    ],

    // Redis configuration
    'redis' => [
        'connection' => env('IDEMPOTENCY_REDIS_CONNECTION', 'idempotency'),
        'prefix'     => 'idempotency:',
    ],
];

Redis Configuration

return [
    'redis' => [
        'idempotency' => [
            'client'   => env('REDIS_CLIENT', 'redis'),
            'url'      => env('REDIS_URL'),
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port'     => env('REDIS_PORT', '6379'),
            'prefix'   => 'idempotency:',
        ],
    ],
];

Usage

  1. Add the middleware to your routes or controller:
use OCharai\Idempotency\Middleware\IdempotencyMiddleware;

// In a route
Route::post('/api/orders', [OrderController::class, 'store'])
    ->middleware(IdempotencyMiddleware::class);

// Or in a controller
public function __construct()
{
    $this->middleware(IdempotencyMiddleware::class);
}
  1. Make API requests with an idempotency key:
curl -X POST https://your-api.com/api/orders \
    -H "X-Idempotency-Key: 01HNG4V2JXSC0XD6YE4CTWF5QM" \
    -H "Content-Type: application/json" \
    -d '{"product_id": 123}'

Note: The idempotency key must be a valid ULID (Universally Unique Lexicographically Sortable Identifier).

How It Works

  1. When a request is made with an idempotency key, the middleware checks if the key exists in Redis
  2. If the key exists and the request method matches, the cached response is returned
  3. If the key doesn't exist, the request is processed normally
  4. On successful JSON responses, the response is cached with the idempotency key
  5. Subsequent requests with the same key within the TTL period will receive the cached response

Important Notes

  • Only successful JSON responses (2xx status codes) are cached
  • Idempotency keys are scoped to the authenticated user (if any) and HTTP method
  • Keys automatically expire after the configured TTL
  • Invalid or missing ULID keys will result in a 400 Bad Request response

Testing

composer test

Security

If you discover any security-related issues, please email othman.fiver@gmail.com instead of using the issue tracker.

Credits

License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-11