@nsfilho/redis-connection
v0.1.14
Published
A very simple and small services to simplify process to connect a redis host or cluster and provide some sugars and environments flavors.
Downloads
101
Readme
Redis Connection Library
A very simple and small services to simplify process to connect a redis host or cluster and provide some sugars and environments flavors.
Environment Variables
This services use some environment variables to pre-adjust some things, like:
Environment Variables
This services use some environment variables to pre-adjust some things, like:
REDIS_${INSTANCENAME}_CLUSTER
: a redis cluster or host, example and default:[{ port: 6379, host: '127.0.0.1' }]
;REDIS_${INSTANCENAME}_PASSWORD
: a password for access redis, example and default:password
;REDIS_CONNECTION_POOLING
: timing between multiples connections to check for instance;
- If you do not specify instance name in
getConnection
ordisconnect
it will use as defaultmain
. - The environment variables replaces the hard-coded values for
nodes
andpassword
.
For debug purposes, we are using debug library. You can use:
DEBUG
: with a value likeredis-connection:*
to see all logs.
Running
For use this library you will need to run a RabbitMQ. A sample docker-compose:
version: '2.0'
services:
redis:
image: redis:6
environment:
ALLOW_EMPTY_PASSWORD: 'no'
REDIS_EXTRA_FLAGS: '--maxmemory 250mb'
command: >
--requirepass password
ports:
- 6379:6379
If you would like to use a docker swarm (stack) version, you can see in your sample folder.
Example
You will found other samples in: Samples Folder.
/* eslint-disable no-console */
import { getConnection, disconnect } from '@nsfilho/redis-connection';
const createSampleKey = async () => {
const redis = await getConnection();
redis.set(
'mysample',
JSON.stringify({
when: new Date().toISOString(),
status: true,
}),
);
};
const readSampleKey = async () => {
const redis = await getConnection();
const valueStr = await redis.get('mysample');
if (valueStr) {
const valueObj = JSON.parse(valueStr);
console.log('Recovered values:', valueObj);
}
};
const execute = async () => {
await createSampleKey();
await readSampleKey();
await disconnect();
};
execute();