Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted for memcached #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
php-ratelimiter
===============

A small class that uses Memcache to allow only a certain number of requests per a certain amount of minutes.
> NOTE: this was previously working with memcache, but I adjusted it to work with memcached (with a d).

A small class that uses Memcached to allow only a certain number of requests per a certain amount of minutes.

The class works around the problem that the timeframe is constantly moving, i.e. every new minute the timeframe is different. See my [blogpost](http://alexander.kirk.at/2013/04/19/add-a-rate-limit-to-your-website/).

Expand All @@ -11,7 +13,7 @@ Usage
-----

```php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"]);
$rateLimiter = new RateLimiter($_SERVER["REMOTE_ADDR"]);
try {
// allow a maximum of 100 requests for the IP in 5 minutes
$rateLimiter->limitRequestsInMinutes(100, 5);
Expand All @@ -30,10 +32,10 @@ If you want to protect multiple resources with different limits, use the third p

```php
// script1.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script1");
$rateLimiter = new RateLimiter($_SERVER["REMOTE_ADDR"], "script1");
try { ... }
// script2.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script2");
$rateLimiter = new RateLimiter($_SERVER["REMOTE_ADDR"], "script2");
try { ... }
```

Expand Down
16 changes: 9 additions & 7 deletions ratelimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,32 @@
class RateExceededException extends Exception {}

class RateLimiter {
private $prefix, $memcache;
public function __construct(Memcache $memcache, $ip, $prefix = "rate") {
$this->memcache = $memcache;
private $prefix, $memcached;
public function __construct($ip, $prefix = "rate", $port=11211) {
$this->memcached = new Memcached();
$this->memcached->addServer('localhost', $port);
$this->prefix = $prefix . $ip;
}

public function limitRequestsInMinutes($allowedRequests, $minutes) {
$requests = 0;

foreach ($this->getKeys($minutes) as $key) {
$requestsInCurrentMinute = $this->memcache->get($key);
$requestsInCurrentMinute = $this->memcached->get($key);
if (false !== $requestsInCurrentMinute) $requests += $requestsInCurrentMinute;
}

if (false === $requestsInCurrentMinute) {
$this->memcache->set($key, 1, 0, $minutes * 60 + 1);
$this->memcached->set($key, 1, ($minutes + 1) * 60);
} else {
$this->memcache->increment($key, 1);
$this->memcached->increment($key);
}

if ($requests > $allowedRequests) throw new RateExceededException;
}

private function getKeys($minutes) {
$keys = array();
$keys = [];
$now = time();
for ($time = $now - $minutes * 60; $time <= $now; $time += 60) {
$keys[] = $this->prefix . date("dHi", $time);
Expand All @@ -60,3 +61,4 @@ private function getKeys($minutes) {
return $keys;
}
}