sobhanatar/idempotent
Composer 安装命令:
composer require sobhanatar/idempotent
包简介
Idempotent Package for Laravel
README 文档
README
Idempotent package provides idempotency for your laravel package. However, in its current state, maybe not extendable to different requirements and can be used as a sample of doing so. Moreover, this package will be updated, in attempt to giving more control over various aspects of it.
Installation
To get started, install the Idempotent package via the Composer package manager:
$ composer require sobhanatar/idempotent
Idempotent service provider registers its own config, language, and database migration file, so you need to export them
$ php artisan vendor:publish --provider="Sobhanatar\Idempotent\IdempotentServiceProvider"
Note: If you don't want to use mysql database as shared memory, you can publish config, and language file using following command:
$ php artisan vendor:publish --tag=idempotent-config --tag=idempotent-language
Deploying Idempotent
To use idempotent package, you need set the options as per the need of your service in configuration and language files. The configuration file is self-documented so that you can find your way through.
The next step is deciding on how you want to control the idempotency of your service. Idempotent package provides two
middlewares that can help you achieve the idempotency; IdempotentHeader, and VerifyIdempotent. Don't forget to
register the middleware in Kernel.php.
IdempotentHeader
IdempotentHeader makes an idempotent key/hash based on the entity's configurations and put it in the header of
request. The assumption in this middleware is that the developer will remain responsible for the logic of using the
idempotent header.
VerifyIdempotent
VerifyIdempotent handles all the required steps for making an endpoint idempotent. The steps are as follows:
- Get the
entityconfiguration - Create an idempotent key/hash based on the entity's configurations.
- Check if the idempotent key/hash exists in the selected
storage. - If it doesn't exist:
- A new record with the status of
progressbe created with the entity'stimeout,and it continues to the logic of the service - When code execution has finished, the response to the client updates the
statusandresponseof the cache.
- A new record with the status of
- If it exists:
- If the
statusisdoneorfail, then theresponsewill be read from storage and replied to the user. - If the
statusisprogress, the message inidempotentlanguage file for thatentitywill be return as response to the user.
- If the
Note: Make sure to use any of two middlewares to only those routes that you want to be idempotent, and not all the routes.
Purging Idempotent Keys/Hashes
If you use mysql as the storage, it's important to purge the expired keys/hashes. Idempotent included
idempotent:purge Artisan command can do this for you.
# Purge expired keys/hashes
$ php artisan idempotent:purge --entity=my-idempotent-endpoint
You may also configure a scheduled job in your application's App\Console\Kernel class to automatically prune your
tokens on a schedule:
/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('idempotent:purge')->hourly(); }
Under the hood
This package is relied on php-lock/lock for locking mechanism and also spatie/async for testing the locking mechanism.
Testing
$ composer test
Contributing
Thank you for considering contributing to Idempotent! You can read the contribution guide here.
Security
If you discover any security-related issues, please email sobhanattar@gmail.com instead of using the issue tracker.
Credits
License
Idempotent is open-sourced software licensed under the MIT license.
sobhanatar/idempotent 适用场景与选型建议
sobhanatar/idempotent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 495 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「idempotency」 「idempotent」 「Idempotent-request」 「Idempotent-Laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sobhanatar/idempotent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sobhanatar/idempotent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sobhanatar/idempotent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Idempotency Middleware
Prevent duplicate jobs from running in Laravel php framework
A PHP helper utility class for fluent interaction and manipulation with collection data using immutable bags
HTTP Idempotency Middleware for Laravel applications
laravel idempotent
A simple package for idempotent requests in Laravel.
统计信息
- 总下载量: 495
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-08