@beforeyoubid/redis-client
v0.0.9
Published
A Redis client for connecting to Redis caching server
Downloads
124
Keywords
Readme
Redis-client
An npm package for connecting to Redis caching server. Support both writer and reader clients.
Instance creation
Creating a writer
Simply call RedisClient.getWriter()
which will perform multiple steps:
- Pull a Redis connection string directly from process.env through a key
REDIS_CONN_STR_WRITER
. - You can pass optional redis params as the second
- The getWriter() will create a new instance everytime so it will try to return the connection within 2000ms to avoid some latency issue or network issue. It will ignore the conneciton if it takes too long. This is to avoid having a network issue
const [writerErr, writerClient] = await to(RedisClient.getWriter());
if (writerErr) {
logger.error(`Unable to get the Redis writer, Err: ${writerErr.message}`);
throw writerErr;
}
Writing cache
Redis 3.x doesn't support Promise (v4 does). Therefore, most common get
, set
are wrapped using a promisify
module.
To write, simply call
const [storingErr] = await to(
writerClient.set(cacheKey, JSON.stringify(propertySearchResponse), 'EX', expiryTimeInSeconds)
);
Creating a reader
Simply call RedisClient.getReader()
which will create a reader instance and connect to it.
const [readerErr, readerClient] = await to(RedisClient.getReader());
Reading cache
const [err, cache] = await to(readerClient.get(cacheKey));
Depending on the value stored on the Redis, if you store a serialised json object you may need to parse it first.