sobanvuex/li3_ratelimit

Lithium plugin for rate limiting requests

Installs: 7

Dependents: 0

Stars: 1

Watchers: 1

Language: PHP

0.1.0 2014-04-11 02:17 UTC

README

Quality / Metrics Releases Downloads Licence Build Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version Composer Downloads License

Requirements

  • PHP >=5.4 - The RateLimit is available via PHP's traits

Installation

Using Composer's command line interface:

php composer.phar require sobanvuex/li3_ratelimit:~0.1

Manually adding the requirements to composer.json:

"require": {
    "sobanvuex/li3_ratelimit": "~0.1"
}

Usage

Add a cache configuration for the rate limit counters

<?php

// app/bootstrap/cache.php

use lithium\storage\Cache;

Cache::config(array(
    // other cache configurations
    'rate_limit' => array(
        'adapter' => 'File' // Or any other cache adapter.
        // There is no need for strategy as this cache will store only integers
    )
));

To add it to your lithium controller add it as a trait

<?php

// app/controllers/MyController.php

namespace app\controllers;

class MyController extends \lithium\action\Controller
{

    use \li3_ratelimit\extensions\RateLimit;

}

To rate limit an action

<?php

// app/controllers/MyController.php

namespace app\controllers;

class MyController extends \lithium\action\Controller
{

    use \li3_ratelimit\extensions\RateLimit;

    public function index()
    {
        if ($this->rateLimit()) {
            // Code which will handle requests exceeding the rate limit
        }

        // continue as usual
    }
}

Parameters

  • $tries - Number of tries. Default is 5.
  • $expiry - How long it takes to expire tries. Default is +5 seconds.
  • $cache - The cache configuration to use. Default is rate_limit.
  • $log - The logger priority to use. Default is notice. Use false to disable