A light weight library to perform content flagging backed by redis, limited or not to a time span window.
Ideal to be included in all kind of data streams or cases like:
- To preventing process something twice.
- To filter data streams to let only unique elements.
- To be used as a distributed semaphore.
- ... and all the cases that a redis flag could be useful.
npm install redis-flagger --save
¿How it works?
Before you can use it, you need to specify if library creates a new redis connection or it should use an already created one. After that, you can override default options letting you to fit any behavior.
// instance // Pass an already instantiated redis// host // new Redis instance Host (if no instance was setted)// port // new Redis instance Port (if no instance was setted)// db // new Redis instance db (if no instance was setted)// password // new Redis instance Password (if no instance was setted)// ttl // Default ttl timespan// key_builder // Key builder calculation function// key_prefix // Key to be prepended to builded key
Redis flagger treats the element between flag, unflag and flagged as the same way. The main idea is to allways reach the same calculated redis key.
The calculating process, with default behavioral arguments, is the following...
To determine redis key flag, element is treated by key_builder. key_builder JSON encode it and then apply a sha1 hashing algorithm to, no matter what kind of stuff element is, always have the same size key. After that, key_preffix is prepended to that hash to improve redis key tracing. Both key_builder and key_preffix could be override it in setup options to fit any case you want.
Creating a redis connection:
const flagger = ;flagger;const element = foo:"bar";flagger;flagger; // trueflagger; // false
Or passing an existing one:
const instance = Redis;flagger;const element2 = foo:"bar"bar:"foo";// Flags an element for 90 seconds overriding defaultflagger;flagger; // trueflagger;flagger; // false
- Flagged element could be anything (array, string, object, etc).
- Note that differences in object atribute order or array elements order, could lead in different hashing results. So if you cannot enshure that this object are EXACTLY the same, you can create an string element representation for example:
Considering.. original and another are the same but order.
const original = foo:'bar'bar:'foo';const another = bar:'foo'foo:'bar';
Leads to different hash keys, so:
flagger;flagger; // false
One Solution could be:
flagger;flagger; // true
Or in some 'agnostic' way:
const default_handler = flaggerDEFAULTKEY_BUILDER;flagger;flagger;flagger; // true
Any doubt please, let me know.