Storage Pod
Store data in Redis, get back the key it was stored at. The purpose of the library is to transport chunks of data between multiple processes. It will store your data in Redis and provide you with the key it used to store it. You can then send the key to the other process (for example via pub/sub or a queue) and it can then receive the package. That's all.
Installation
Install it in your project via:
npm install storage-pod --save
Usage
First, you create a storage function. For that you need a namespace you want to use for the generated keys (in our example that is mynamespace
) and a Redis client (which needs to be API compatible with the redis
NPM package).
var client = ;var createStorage = ;var store = ;
Now you have a store
function, which you can use to store data. You provide it with a callback which will receive an error if something went wrong and the key that your data is accessible with:
;
You can now send the key to the other process. It will just need to GET
the key from Redis.
Serialize
As you want to serialize the value most of the time, you can provide a serialize function to the createStorage
function as an option. The serialize function is applied to your data before it is stored in Redis. In the following example we use JSON as our serialization format, but you can choose whatever you want (MsgPack, ProtoBuf, transit... Just provide a function that takes data and returns it serialized).
var store = ; // Now you can just provide an object:;
Set a TTL
This library is meant to be used to send a piece of data to another process. What happens after the other library has read the data? It is now useless and can be deleted. You can either delete it when you have read it or you could set a TTL on it. If you want to go with the second option, you can do it like this (we set the time in milliseconds):
var store = ;
Contributing
First install all development dependencies with npm install
. Then run npm run ci
to see if the code:
- Passes the test suite (aka.
npm test
) - Passes the linter (aka.
npm run jshint
) - Passes the code style checker (aka.
npm run jscs
)
This will also automatically happen before every commit and be checked by Travis CI. Tests are written using tape.
#lesscode
This library is inspired by the #lesscode movement: It has a very specific purpose, only one runtime dependency (a Redis client, which you need to provide to it and is therefore not a runtime dependency of the package) and just a few lines of code.
Thanks to soveran for the inspiration (especially OST).
License
This code is published under the MIT license.