Counters

Increment Values

It's time to increment our counter for team1 to ensure they get points when they are doing well: :tada:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const Redis = require('ioredis');

const redis = new Redis();

async function main() {
    const key = 'team1';
    try {
        const points = await redis.incr(key);
        console.log(points);
    } catch (error) {
        console.error(error);
    }
    redis.disconnect();
}

main();

When we invoke the Redis INCR command in line 8, it returns the value of the team1 counter after the increment operation has been completed.

Decrement Values

At times, it is necessary to decrement the points for team1 and this is how we accomplish that goal:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const Redis = require('ioredis');

const redis = new Redis();

async function main() {
    const key = 'team1';
    try {
        const points = await redis.decr(key);
        console.log(points);
    } catch (error) {
        console.error(error);
    }
    redis.disconnect();
}

main();

This code is strikingly similar to the code for incrementing counters above, and there are no real surprises here.

results matching ""

    No results matching ""