@janiscommerce/redis v2.4.0
redis
Installation
npm install @janiscommerce/redisBreaking changes Since 2.0.0 :warning:
- Config
hostis required to connect. Connection will be ignored if not received. - The package now works as a wrapper of redis package for using Redis commands
- Removed Api method
set(),get(),del(), use redis commands instead - Now async method
connect()must be executed before using any other command.
Configuration
Client configuration
Env Vars
If the env vars REDIS_WRITE_URL is set, will create a Redis connection
Config object parameter
Since 2.3.0
The connect() allows to receive an object with the url.
Settings
:warning: Deprecated :warning:
This package uses @janiscommerce/settings.
In .janiscommercerc.json file requires to have the configuration under the redis.
- The field
hostis required.
See an example below
{
"redis": {
"host": "redis.example.host"
}
}Cluster Mode
The env var REDIS_CLUSTER_MODE must be set with a truthy value.
Env Vars
If the env vars REDIS_WRITE_URL and REDIS_READ_URL will be used for creating a Redis cluster connection.
Config object parameter
Since 2.3.0
The connect() allows to receive an object with the url as String or String Array.
API
connect(config = {})
async | Connects the Redis server using settings.
:new: Parameters
configthe optional configuration.config.urloptional url as String for connecting the client of cluster. Since 2.3.0config.urloptional url as String Array for connecting. Exclusive for in cluster mode. Since 2.3.0- :new:
config.maxRetriesoptional Number indicates the max amount of connection retries. (Default:3)- When the max retries are reached the Client stops retrying and throws an error
- This won't close the cached connection, the cached connection will persist and won't retry to connect.
- If you need to set a limit of retries but retry when your process is executed again, then
try catchthe error and use closeConnection - If you don't want it to retry connection until your process is restarted, then don't need to close the connection.
- If you need to set a limit of retries but retry when your process is executed again, then
- :new:
config.connectTimeoutoptional Number indicates the connection timeout in miliseconds. (Default:5000)
Return
client: The Redis client whenhostis present in settings.undefined: Whenhostis not present in settings.
Throw an Error if Redis Server fails.
closeConnection()
async | Closes the active connection.
Usage
const Redis = require('@janiscommerce/redis');
(async () => {
const redisCluster = await Redis.connect();
await redisCluster.set('product-123', 'blue-shirt');
const value = await redisCluster.get('product-123');
// expected value: blue-shirt
})();
// Usage with custom max retries
(async () => {
try {
const redisCluster = await Redis.connect({
connectTimeout: 1000,
maxRetries: 1
});
}catch(err) {
console.log(err.message);
await Redis.closeConnection();
}
})();Errors
The errors are informed with a RedisError.
This object has a code that can be useful for a debugging or error handling.
The codes are the following:
| Code | Description |
|---|---|
| 1 | Redis error |
| 2 | Max connection retries reached |
:information_source: For more examples see redis