Why?
This is a zero dependency and slightly opinionated implementation of the Redis package, opinions include:
- Fully Promisified implementation
- Default cache expiry
- Standard cache API
- docker-compose config for easy dev environment setup
- TODO: Exponential backoff retry logic
This package is also fully tested (Unit + Integration) and is backed by Travis CI for all pull requests.
Getting Started
In order to install the package, source from either NPM or Yarn:
yarn add node-redis-app-cache
Setup
Simply require the file in your application:
const {RedisCache} = require('node-redis-app-cache')
From there, specify the default expiry (in seconds) as well as the redis config:
const redisCache = new RedisCache({
defaultExpiry: 1,
redisConfig: "redis://localhost:6379"
})
})
NOTE: the redis config option is simply a passthrough to the node redis config.
API
The API for this package is intentionally simple and only exposes a few methods:
-
set(key, value, expiry) => Promise : Sets a key and value, and an (optional) expiry. If no expiry is set, the default value expiry is used.
-
exists(key) => Promise : Returns true/false on whether the key exists in the specified database.
-
get(key) => Promise : Returns a value given a key.
-
fetch(key, saveMethod) => Promise : Tries to fetch the given key, if the key is not found, calls the save method and tries to persist the return of the save method with the key specified.
TODO: add expiry override
- delete(key) => Promise : Deletes the given key.
The API is also designed so that all APIs return a promise by default. This is not default behavior of the Redis package and is an intentional design decision.
Inspiration
Inspirations for this package include: