Skip to content

Commit 9830514

Browse files
committed
fix(implement disable mech.): disable hooks and services by passing DISABLE_REDIS_CACHE
1 parent 6c6b7a8 commit 9830514

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ module.exports = {
155155
};
156156
```
157157

158-
You can also skip cahce hook by passing `hook.params.$skipCacheHook = true`
158+
You can also skip cache hook by passing `hook.params.$skipCacheHook = true`
159+
You can also disable redis-cache hooks and service by passing env. variable `DISABLE_REDIS_CACHE=true`
159160

160161
## TODO:
161162
- TS definitions

src/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
import redis from 'redis';
33
import chalk from 'chalk';
44

5+
const { DISABLE_REDIS_CACHE } = process.env;
6+
57
export default (options: any = {}) => {
68
const errorLogger = options.errorLogger || console.error;
79
const retryInterval = options.retryInterval || 5000;
810

11+
if (DISABLE_REDIS_CACHE) {
12+
return () => {};
13+
}
14+
915
return function client() {
1016
const app = this;
1117
const config = app.get('redis') || {};

src/hooks.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import chalk from 'chalk';
44
import qs from 'qs';
55
import async from 'async';
66

7+
const { DISABLE_REDIS_CACHE } = process.env;
78
const HTTP_OK = 200;
89
const HTTP_NO_CONTENT = 204;
910
const HTTP_SERVER_ERROR = 500;
@@ -29,6 +30,10 @@ function cacheKey(hook) {
2930

3031
export default {
3132
before(passedOptions) {
33+
if (DISABLE_REDIS_CACHE) {
34+
return hook => hook;
35+
}
36+
3237
return function (hook) {
3338
try {
3439
if (hook && hook.params && hook.params.$skipCacheHook) {
@@ -84,6 +89,10 @@ export default {
8489
};
8590
},
8691
after(passedOptions) {
92+
if (DISABLE_REDIS_CACHE) {
93+
return hook => hook;
94+
}
95+
8796
return function (hook) {
8897
try {
8998
if (
@@ -131,6 +140,10 @@ export default {
131140
};
132141
},
133142
purge() {
143+
if (DISABLE_REDIS_CACHE) {
144+
return hook => hook;
145+
}
146+
134147
return function (hook) {
135148
try {
136149
if (

src/services.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import async from 'async';
22

3+
const { DISABLE_REDIS_CACHE } = process.env;
34
const HTTP_OK = 200;
45
const HTTP_NO_CONTENT = 204;
56
const HTTP_SERVER_ERROR = 500;
@@ -157,9 +158,11 @@ export default (options: any = {}) => {
157158
return function () {
158159
const app = this;
159160

160-
app.use(`${pathPrefix}/clear/single`, serviceClearSingle);
161-
app.use(`${pathPrefix}/clear/group`, serviceClearGroup);
162-
app.use(`${pathPrefix}/clear/all`, serviceClearAll);
161+
if (!DISABLE_REDIS_CACHE) {
162+
app.use(`${pathPrefix}/clear/single`, serviceClearSingle);
163+
app.use(`${pathPrefix}/clear/group`, serviceClearGroup);
164+
app.use(`${pathPrefix}/clear/all`, serviceClearAll);
165+
}
163166
};
164167
}
165168

0 commit comments

Comments
 (0)