This plugin allows you to easily use Radis to cache
strapi-for-redis plugin allows you to easily use Radis to cache
Install degit for getting last commits:
-
install redis database
-
install ioredis package
yarn add ioredis
git clone https://github.com/marefati110/redis-for-strapi
# OR
degit https://github.com/marefati110/redis-for-strapi
Now move redis-api-folder to strapi plugin folder.
Create middleware.json
in ./config/environments/**/middleware.json
{
...
"redis": {
"enabled": true,
...
}
}
Goto ./config/middleware.json
and change the config like this
{
"timeout": 100,
"load": {
"before": ["responseTime", "logger", "cors", "responses"],
"order": [],
"after": ["parser", "router","redis"]
}
}
To cache a variable, just use the function below
// also can be async
strapi.redis_setCache(ctx,variable,expite_time);
// default is 24h (optional)
I recommend use it before ctx.send(variable)
you can use ioredis package like this
strapi.redis
You can perform your functions before or after retrieving data from the redis database.
./plugins/redis-for-strapi/config/before.js
./plugins/redis-for-strapi/config/after.js
Define your function and put the function name in the list (exec_befor, exec_after)
- function must have one argument that are ctx
- functoin must return true or false
'use strict';
const Before = (ctx) =>{
console.log('This msg show for every request.');
return true;
};
const exec_before = [Before];
module.exports = { exec_before }; // Dont Change it
- @marefati110 - Idea & Initial work