@gasbuddy/configured-redis-client

A configuration driven redis client

Stats

StarsIssuesVersionUpdatedCreatedSize
@gasbuddy/configured-redis-client
2.0.03 years ago5 years agoMinified + gzip package size for @gasbuddy/configured-redis-client in KB

Readme

configured-redis-client

A small wrapper around redis to allow configuration from confit.

{
  "connections": {
    "redis": {
      "module": "require:@gasbuddy/configured-redis-client",
      "hosts":
    }
  }
}

API

The redis client attached to req.gb.db is an ioredis Redis object.

Additional Utilities

Ttl

Common redis ttl values can be found in Ttl.

req.gb.db.redis.Ttl.oneMinute // => 60
req.gb.db.redis.Ttl.oneHour // => 3600

Redis Keys

To make a key, use makeKey()

req.gb.db.redis.makeKey('redis', 'is', 'cool') // => 'redis:is:cool'

To create a key that begins with the name of your service, use makeServiceKey()

req.gb.db.redis.makeServiceKey('redis', 'is', 'cool') // => 'my-serv:redis:is:cool'

Caching the Result of a Function Call

Use memoize() to cache the result of a function call.

const redisClient = req.gb.db.redis;

const mySlowDataFunction = async () => {
  // slow things here
};

// return cached value. on cache miss, cache and return result of mySlowdataFunction()
const myVal = redisClient.memoize(
  mySlowDataFunction,
  {
    key: redisClient.makeKey('redis', 'is', 'cool'), // defaults to name of function
    ttlSeconds: redisClient.Ttl.fiveMinutes, // defaults to one minute
  },
);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.