@am92/redis
Version:
152 lines (116 loc) • 4.76 kB
Markdown
# @am92/redis
[](https://www.npmjs.com/package/@am92/redis)
[](https://snyk.io/test/npm/@am92/redis)
[](https://opensource.org/licenses/MIT)
[](https://npm-stat.com/charts.html?package=%40m92%2Fredis)
[](https://bundlephobia.com/package/@am92/redis)
<br />
This is an SDK wrapper that simplifies interaction with a Redis instance by providing convenient Redis functionalities. It is built on top of the [Node-Redis](https://www.npmjs.com/package/redis) package (v4.7.x).
For full documentation, visit [here](https://heliumtank92.github.io/am92-redis).
<br />
## Table of Content
- [Installation](#installation)
- [Environment Variables](#environment-variables)
- [Creating an Instance](#creating-an-instance)
- [Self-managed Config](#self-managed-config)
- [Connection Management](#connection-management)
- [Contributors](#contributors)
- [Resources](#resources)
- [License](#license)
<br />
## Installation
```bash
npm install --save @am92/redis
```
<br />
## Environment Variables
The following environment variables need to be set to work with this package:
```sh
##### Redis Config
export REDIS_ENABLED=false
export REDIS_AUTH_ENABLED=false
export REDIS_HOST=
export REDIS_PORT=
export REDIS_AUTH=
export REDIS_KEY_PREFIX=
export REDIS_PING_INTERVAL=
```
| Variable Name | Required | Default | Description |
| --------------------- | -------- | ------------------------ | ------------------------------------- |
| `REDIS_ENABLED` | No | `false` | Enables/Disables Redis functionality |
| `REDIS_AUTH_ENABLED` | No | `false` | Enables/Disables Redis authentication |
| `REDIS_HOST` | No* | - | Redis server host address |
| `REDIS_PORT` | No* | - | Redis server port number |
| `REDIS_AUTH` | No** | - | Redis authentication password |
| `REDIS_KEY_PREFIX` | No | - | Prefix to be added to all Redis keys |
| `REDIS_PING_INTERVAL` | No | `1800000` (30 minutes) | Ping interval in milliseconds |
*Note:*
* *\* - Required fields if `REDIS_ENABLED` is set to `true`.*
* *\*\* - Required fields if `REDIS_AUTH_ENABLED` is set to `true`.*
<br />
## Creating an Instance
```javascript
import { RedisSdk } from '@am92/redis'
const redisSdk = new RedisSdk()
export default redisSdk
```
*Note: You do not need to set any environment variables if you wish to pass your own config.*
## Self-managed Config
If you wish to pass your custom 'config' for the RedisSdk, then you can avoid setting any environment variables defined above and pass your own redis config as follows:
```javascript
import { RedisSdk } from '@am92/redis'
const config = {
CONNECTION_CONFIG: {
socket: {
host: '',
port: 6379,
tls: true
},
password: '',
pingInterval: 0
},
KEY_PREFIX: ''
}
const redisSdk = new RedisSdk(config)
export default redisSdk
```
## Connection Management
To manage redis connections for RedisSdk Instances, `connect` and `disconnect` methods are provided and they can be called as shown below. The `connect` method must be called before before using the RedisSdk Methods.
```javascript
// To establish a connection
await redisSdk.connect()
// To release the connection
await redisSdk.disconnect()
// To force release the connection
await redisSdk.disconnect(true)
```
<br />
## Contributors
<table>
<tbody>
<tr>
<td align="center">
<a href='https://github.com/ankitgandhi452'>
<img src="https://avatars.githubusercontent.com/u/8692027?s=400&v=4" width="100px;" alt="Ankit Gandhi"/>
<br />
<sub><b>Ankit Gandhi</b></sub>
</a>
</td>
<td align="center">
<a href='https://github.com/agarwalmehul'>
<img src="https://avatars.githubusercontent.com/u/8692023?s=400&v=4" width="100px;" alt="Mehul Agarwal"/>
<br />
<sub><b>Mehul Agarwal</b></sub>
</a>
</td>
</tr>
</tbody>
</table>
<br />
## Resources
* [Node-Redis](https://www.npmjs.com/package/redis)
<br />
## License
* [MIT](https://opensource.org/licenses/MIT)
<br />
<br />