Giter VIP home page Giter VIP logo

laravel-idempotent's Introduction

Idempotent

laravel 幂等中间件,防止客户端同一时间请求多次。

建议将 laravel 默认缓存设置为 redis,将得到更好的性能。

Requirement

  1. PHP >= 7.0 | PHP >= 8.0
  2. laravel >= 6

Installation

$ composer require chenpkg/laravel-idempotent

Usage

发布配置文件

$ php artisan vendor:publish --tag="laravel-idempotent"

中间件为 Chenpkg\Idempotent\IdempotentMiddleware,别名 idempotent

Route::group(['middleware' => 'idempotent'], function () {
    //...
});

注:请不要直接加在 App\Http\Kernelmiddleware 里面,由于中间件执行顺序问题,可能导致该组件获取不到当前用户身份标识符 ID

或者你可以将它加入到指定路由中间件组中

protected $middlewareGroups = [
    // ...
    'api' => [
        'idempotent',
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

重复的请求将会抛出 Chenpkg\Idempotent\Exceptions\RepeatRequestException Http 异常

Configure

// config/idempotent.php

return [
    // true 自动获取唯一key, false 前端提供
    'forcible' => true,

    // 需要过滤重复请求的请求类型
    'methods' => ['POST', 'PUT', 'PATCH'],

    // 缓存有效时间/秒,防止死锁
    'seconds' => 10,

    // 获取当前用户
    'resolve_user' => function (\Illuminate\Http\Request $request) {
        return auth()->user();
    },

    // 前端提供 key 请求头名称.
    'header_name' => 'Idempotent-Key',
];

License

MIT

laravel-idempotent's People

Contributors

chenpkg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.