strapi-plugin-redis
v1.1.0
Published
Plugin used to centralize management of Redis connections in Strapi
Downloads
13,939
Readme
Table of Contents
- 🚦 Current Status
- 🛑 Foreword
- ✨ Features
- 🤔 Motivation
- 🖐 Requirements
- ⏳ Installation
- 🔧 Configuration
- 🚚 Usage and API
- Contributing
- License
🚦 Current Status
This package is currently under development and should be consider ALPHA in terms of state. I/We are currently accepting contributions and/or dedicated contributors to help develop and maintain this package.
For more information on contributing please see the contrib message below.
🛑 Foreword
This package's lead maintainer is an employee of Strapi however this package is not officially maintained by Strapi Solutions SAS nor Strapi, Inc. and is currently maintained in the free time of the lead maintainer.
Absolutely no part of this code should be considered covered under any agreement you have with Strapi proper including but not limited to any Enterprise Agreement you have with Strapi.
✨ Features
This plugin utilizes 1 core package:
- ioredis - for all connection management
These are the primary features that are finished or currently being worked on:
- [x] Redis Single Node Support
- [x] Redis Replica + Sentinel Support
- [ ] Redis Sharding Support (assumed working, no config samples)
- [x] Multiple connections/databases
🤔 Motivation
The purpose of this package is to have a universal Redis connector for all packages wanting to utilize a Redis database and/or for those wanting to develop custom functions and code within your Strapi project and would prefer a centralized Redis database for various purposes.
A few examples of where Redis could be used within a Strapi application:
- LRU-based response cache for REST
- Apollo server GraphQL cache
- IP Rate-limiting using something like koa2-ratelimit
- Distributed Redis locks for Strapi clusters (useful for clustered usage of cron tasks)
- So much more
If you are currently using this package in your plugin and would like to be featured, please feel free to submit an issue to have your plugin added to the list below:
- strapi-plugin-rest-cache
- strapi-plugin-redcron
- More plugins coming soon!
🖐 Requirements
Supported Strapi Versions:
| Strapi Version | Supported | Tested On | | -------------- | --------- | ------------- | | v3 | ❌ | N/A | | v4.0.x | ✅ | July 2022 | | v4.1.x | ✅ | July 2022 | | v4.2.x | ✅ | July 2022 | | v4.3.x | ✅ | December 2022 | | v4.4.x | ✅ | December 2022 | | v4.5.x | ✅ | December 2022 | | v4.6.x | ✅ | January 2024 | | v4.7.x | ✅ | January 2024 | | v4.8.x | ✅ | January 2024 | | v4.9.x | ✅ | January 2024 | | v4.10.x | ✅ | January 2024 | | v4.11.x | ✅ | January 2024 | | v4.12.x | ✅ | January 2024 | | v4.13.x | ✅ | January 2024 | | v4.14.x | ✅ | January 2024 | | v4.15.x | ✅ | January 2024 | | v4.16.x | ✅ | January 2024 | | v4.17.x | ✅ | January 2024 | | v4.19.x | ✅ | January 2024 |
This plugin will not work with Strapi v3 projects as it utilizes APIs that don't exist in the v3!
⏳ Installation
Install the plugin in your Strapi project or your Strapi plugin.
# Using Yarn (Recommended)
yarn add strapi-plugin-redis
# Using npm
npm install strapi-plugin-redis --save
🔧 Configuration
WIP
Single Redis Node
// path ./config/plugins.js
module.exports = {
redis: {
config: {
connections: {
default: {
connection: {
host: '127.0.0.1',
port: 6379,
db: 0,
},
settings: {
debug: false,
},
},
},
},
},
};
Sentinel Replica (3 node)
// path ./config/plugins.js
module.exports = {
redis: {
config: {
connections: {
default: {
connection: {
sentinels: [
{ host: '192.168.1.101', port: 26379 },
{ host: '192.168.1.102', port: 26379 },
{ host: '192.168.1.103', port: 26379 },
],
name: 'my-redis-replicaSet',
db: 0,
},
settings: {
debug: false,
},
},
},
},
},
};
Using TLS with authentication
// path ./config/plugins.js
const { readFileSync } = require('fs');
module.exports = {
redis: {
config: {
connections: {
default: {
connection: {
// @see https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
host: '127.0.0.1',
port: 6379,
db: 0,
username: 'username',
password: 'secret',
// @see https://github.com/luin/ioredis#tls-options
tls: {
ca: readFileSync('cert.pem'),
},
},
settings: {
debug: false,
},
},
},
},
},
};
🚚 Usage and API
This plugin directly mounts each Redis DB client and it's config on strapi.redis
Config
Access with: strapi.redis.config
The config key contains the entire plugin config including all ioredis instances configurations. These should not be modified after bootstrapping your Strapi application (aka while the server is running).
Connections
Access with: strapi.redis.connections
For each connection either a normal Redis client is created, or if the cluster setting is enabled and you have properly passed in an array of nodes (and optionally any cluster options) a Redis Cluster client.
Redis Client
Accessed with: strapi.redis.connections.default.client
Note you can swap the default key with any other named database you have configured
From here you have full access to the ioredis API.
Contributing
I/We are actively looking for contributors, maintainers, and others to help shape this package. As this plugins sole purpose within the Strapi community is to be used by other developers and plugin maintainers to help ease the connection to Redis databases.
Instead of reinventing the wheel every time you need to connect to Redis, the hope is to centralize the connections in a single plugin that all plugins can piggy back on.
If interested please feel free to email the lead maintainer Derrick at: [email protected] or ping DMehaffy
on Discord.
Please Note: This package is maintained collectively by the strapi community organization. While there may be a lead maintainer, they are not the sole maintainer of this code and this code does not belong to the lead maintainer.
License
See the LICENSE file for licensing information.