@janiscommerce/redis
v2.4.0
Published
A Driver to use Redis
Downloads
59
Readme
redis
Installation
npm install @janiscommerce/redis
Breaking changes Since 2.0.0 :warning:
- Config
host
is 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
host
is 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
config
the optional configuration.config.url
optional url as String for connecting the client of cluster. Since 2.3.0config.url
optional url as String Array for connecting. Exclusive for in cluster mode. Since 2.3.0- :new:
config.maxRetries
optional 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 catch
the 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.connectTimeout
optional Number indicates the connection timeout in miliseconds. (Default:5000
)
Return
client
: The Redis client whenhost
is present in settings.undefined
: Whenhost
is 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