redis-sliding-window-counter

1.0.2 • Public • Published

Redis Sliding Window Counter

Count twice, get count.

import { increment, count } from 'redis-sliding-window-counter';
 
const id = `test-id-${Math.random()}`;
const iv = 10 * 1000;
const redis = createRedisClient();
 
await increment({ id, iv, redis });
await increment({ id, iv, redis });
    
const count = await count({ id, iv, redis });
 
assert.equal(count, 2);

Count twice, wait until expired, get count.

import { increment, count } from 'redis-sliding-window-counter';
 
const id = `test-id-${Math.random()}`;
const iv = 10 * 1000;
const redis = createRedisClient();
 
await increment({ id, iv, redis });
await increment({ id, iv, redis });
 
// wait 11 seconds
await new Promise(r => setTimeout(r, iv + 1000))
    
const count = await count({ id, iv, redis });
 
assert.equal(count, 0);

Package Sidebar

Install

npm i redis-sliding-window-counter

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

5.11 kB

Total Files

4

Last publish

Collaborators

  • carlhopf