drsdre/yii2-redis-counter

Redis Counter implements fast atomic counters using Redis storage.

Installs: 11 743

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 1

Open Issues: 0

Type:yii2-extension

1.0 2016-01-16 22:59 UTC

This package is auto-updated.

Last update: 2024-10-29 04:31:07 UTC


README

Redis Counter implements fast atomic counters using Redis storage.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist drsdre/yii2-redis-counter "*"

or add

"drsdre/yii2-redis-counter": "*"

to the require section of your composer.json file.

Usage

You need to setup the client as application component:

'components' => [
    'redisCounter' => [
        'class' => 'drsdre\redis\Counter',
    ],
    ...
]

Optional the parameter 'redis' can be added to specify a specific Redis connection.

Usage

Once the extension is installed, use it in your code like :

    // Create a hourly counter
    $counter_key = 'hourly_statistics';
    
    // Check if counter is setup
    if (Yii::$app->redisCounter->exists($counter_key)) {
        // Atomic increment counter with 1
        Yii::$app->redisCounter->incr($counter_key);
    } else {
        // Create counter set value to 1 and let it exist for 1 hour
        Yii::$app->redisCounter->set($counter_key, 1, 60*60);
    }