写了个 Laravel 幂等组件
Laravel-Idempotent
由来
上周业务部门反应有重复创建订单的情况……
然后找了几个包看了看发现写的都不太完整,加上还没写过 Composer 包,醉醉醉重要的是买了超哥的教程一直在吃灰心里充满罪恶感;于是便有了这个包~
感兴趣的兄弟可以看看~~
安装
$ composer require lidongyooo/laravel-idempotent -vvv
配置
- 在
config/app.php
注册 ServiceProvider (Laravel 5.5 + 无需手动注册)
'providers' => [
// ...
Lidongyooo\Idempotent\IdempotentServiceProvider::class,
],
- 创建配置文件
$ php artisan vendor:publish --tag="laravel-idempotent"
- 查看应用根目录下的
config/idempotent.php
使用
中间件 Lidongyooo\Idempotent\IdempotentMiddleware
,别名 idempotent
// ...
Route::post('/test', function () {
return 'test';
})->middleware('idempotent');
//如果你想强制缓存响应
Route::post('/test', function () {
return 'test';
})->middleware('idempotent:true');
或者你可以将它加入到指定路由中间件组中
protected $middlewareGroups = [
// ...
'api' => [
'idempotent',
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
返回值
- 重复请求将会返回异常
abort(425, 'Your request is still being processed.');
- 已缓存的请求将添加响应头
$response->header($this->config['back_header_name'], $idempotentKey);
更多详细介绍请查看 配置文件
:+1:
同买了超哥的教程,我当时就想把dingo-api的转换层单独拿出来,就自己整了一个转换层 github.com/bukeliyuchenmou/respons...
原理是什么,redis吗,redis除了栈其他不是原子性的吧