@beforeyoubid/redis-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @beforeyoubid/redis-client

Weekly Downloads

217

Version

0.0.9

License

ISC

Unpacked Size

25.7 kB

Total Files

20

Last publish

Collaborators

  • cayob
  • alice-byb
  • ben-byb
  • akram-byb
  • shiwam-byb
  • dmitrii8723
  • chainat-byb
  • mitch_byb