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

Use PSR-6 caching interfce & remove desarolla2 cache #10

Merged
merged 6 commits into from
Nov 9, 2017
Merged
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
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
language: php

services:
- redis-server

php:
- 5.5
- 5.6
- 7.0
- 7.1

before_script:
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer self-update
- composer install --prefer-source --dev

script:
- phpunit --colors --verbose --coverage-clover build/logs/clover.xml
- php vendor/bin/phpunit --colors --verbose --coverage-clover build/logs/clover.xml -c phpunit.xml

after_script: if [ $(phpenv version-name) = "5.6" ]; then php vendor/bin/ocular code-coverage:upload --format=php-clover build/logs/clover.xml; fi
after_script: if [ $(phpenv version-name) = "7.1" ]; then php vendor/bin/ocular code-coverage:upload --format=php-clover build/logs/clover.xml; fi
55 changes: 19 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ by including `sunspikes/php-ratelimiter` in your project composer.json require:

``` json
"require": {
"sunspikes/php-ratelimiter": "dev-master"
"sunspikes/php-ratelimiter": "^2.0"
}
```

### Without Composer

You can also download it from [Github] (https://github.com/sunspikes/php-ratelimiter),
You can also download it from [Github](https://github.com/sunspikes/php-ratelimiter),
but no autoloader is provided so you'll need to register it with your own PSR-4
compatible autoloader.

Expand All @@ -36,9 +36,9 @@ compatible autoloader.

```php
// 1. Make a rate limiter with limit 3 attempts in 10 minutes
$cacheAdapter = new DesarrollaCacheAdapter((new DesarrollaCacheFactory())->make());
$throttlerCache = new ThrottlerCache($anyPsr6CacheAdapter);
$settings = new ElasticWindowSettings(3, 600);
$ratelimiter = new RateLimiter(new ThrottlerFactory($cacheAdapter), new HydratorFactory(), $settings);
$ratelimiter = new RateLimiter(new ThrottlerFactory($throttlerCache), new HydratorFactory(), $settings);

// 2. Get a throttler for path /login
$loginThrottler = $ratelimiter->get('/login');
Expand All @@ -64,41 +64,22 @@ if ($loginThrottler->access()) {
print $loginThrottler->count(); // or count($throttler)
```

### Configuration
### Using and Extending

By default PHP Ratelimiter uses the [desarolla2 cache adapter](https://github.com/desarrolla2/Cache), the sample configuration provided in ```config/config.php```
The PHP Ratelimiter is highly extensible, you can use any PSR6 cache adapters as caching backend

You can configure the drivers in ```config.php```, for example to use memcache change the driver to ```'memcache'```
For example,

```php
return [
'default_ttl' => 3600,
'driver' => 'memcache',
'memcache' => [
//....
],
];
```

### Extending
- PHP Cache (http://www.php-cache.com/en/latest/)
- Symfony (https://symfony.com/doc/current/components/cache.html)
- Stash (http://www.stashphp.com)

The PHP Ratelimiter is highly extensible, you can have custom adapters by implementing ```Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface```

For example to use Doctrine cache adapter
For example to use Memcache adapter from php cache

```php
class DoctrineCacheAdapter implements CacheAdapterInterface
{
public function __construct($cache)
{
$this->cache = $cache;
}

// Implement the methods
}

// Build adapter using APC cache driver
$adapter = new DoctrineCacheAdapter(new \Doctrine\Common\Cache\ApcCache());
$adapter = new \Cache\Adapter\Memcache\MemcacheCachePool();
$throttlerCache = new ThrottlerCache($adapter);
...
```

Also you can have custom hydrators by implementing ```Sunspikes\Ratelimit\Throttle\Hydrator\DataHydratorInterface```
Expand Down Expand Up @@ -157,10 +138,10 @@ See [Overview example](#overview) for instantiation.
All the following throttlers use time functions, thus needing a different factory for construction:

```php
$cacheAdapter = new DesarrollaCacheAdapter((new DesarrollaCacheFactory())->make());
$throttlerCache = new ThrottlerCache($adapter);
$timeAdapter = new PhpTimeAdapter();

$throttlerFactory = new TimeAwareThrottlerFactory($cacheAdapter, $timeAdapter);
$throttlerFactory = new TimeAwareThrottlerFactory($throttlerCache, $timeAdapter);
$hydratorFactory = new HydratorFactory();

//$settings = ...
Expand Down Expand Up @@ -207,10 +188,12 @@ the request is delayed until the internal throttler has capacity again.
$settings = new RetrialQueueSettings(new LeakyBucketSettings(120, 60000, 120));
```

## Author
## Authors

Krishnaprasad MG [@sunspikes]

Mike Feijs [@Feijs]

## Contributing

Please feel free to send pull requests.
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
}
],
"require": {
"php": ">=5.5",
"desarrolla2/cache": "~2.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The composer.lock file should be updated to reflect these changes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it is not a good commit the composer.lock file to VCS, for libraries

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, you're right, I mistakenly thought it was already in there

"php": ">=7.0"
},
"require-dev": {
"mockery/mockery": "^0.9.4",
"phpunit/phpunit": "^4.7.6",
"phpunit/phpunit": "^6.4",
"cache/cache": "^0.4",
"scrutinizer/ocular": "^1.1"
},
"suggest": {
"cache/cache": "Rate limiter needs one of the PSR-6 compatible cache adapters"
},
"autoload": {
"psr-4": {
"Sunspikes\\Ratelimit\\": "src/"
Expand Down
39 changes: 0 additions & 39 deletions config/config.php

This file was deleted.

3 changes: 0 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"/>
</listeners>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,39 @@
* SOFTWARE.
*/

namespace Sunspikes\Ratelimit\Cache\Factory;
namespace Sunspikes\Ratelimit\Cache;

interface FactoryInterface
abstract class AbstractCacheItem implements ThrottlerItemInterface
{
/**
* Create a cache driver adapter
* @param string $serialized
* @return ThrottlerItemInterface
*/
public function unserialize($serialized)
{
$array = json_decode($serialized, true);
$this->fromArray($array);
}

/**
* @return array
*/
public function serialize()
{
return json_encode($this->toArray());
}

/**
* Wake up call to build the cache item object from array representation
*
* @param array $array
*/
abstract protected function fromArray(array $array);

/**
* Get the array representation of the object
*
* @return mixed
* @return array
*/
public function make();
}
abstract protected function toArray(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
/**
* Cache driver not found
*/
class DriverNotFoundException extends CacheException
class CacheAdapterException extends CacheException
{
}
4 changes: 3 additions & 1 deletion src/Cache/Exception/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@

namespace Sunspikes\Ratelimit\Cache\Exception;

use Psr\Cache\CacheException as PsrCacheException;

/**
* The base cache exception
*/
class CacheException extends \Exception
class CacheException extends \RuntimeException implements PsrCacheException
{
}
Loading