This is a simple redis cache that can set and get values from redis. It supports key expiration, auto reconnection to the redis server and various options (see bellow).
- Redis
- Node
-
Install the package:
npm i -S redis-node-cache
-
Start your local redis server (if needed):
redis-server
-
Import the package in your project:
import RedisNodeCache from 'redis-node-cache'; const cache = new RedisNodeCache(options);
redisUrl
The redis url to connect to (e.g.: redis://localhost:6379). Required.reconnectInterval
Reconnection interval when the redis server is unavailable. Default: 500 ms. Optional.prefix
Prefix for the redis keys. Default: "jscache_". Optionallogger
Logger function that will be used to record debug messages. Optional.
-
.set(key, value, expires)
Set a value in the cache with an optional expires timeout in ms. Returns a promise that either resolves to "OK" or throws an error. -
.get(key)
Get a value from the cache. Returns a promise that either resolves to the value (or null if it doesn't exist) or throws an error. -
.connected
Returns true if connected to the redis server, false otherwise.
-
import RedisNodeCache from 'redis-node-cache'; const cache = new RedisNodeCache({ redisUrl: 'redis://localhost:6379' });
-
cache.set('key', 'a string value', 1000 * 60 * 30) .catch((e) => { ... });
-
cache.get('key') .then(value => { ... }) .catch((e) => { ... });