Skip to content

Commit a5338fa

Browse files
make checks for status code
1 parent 004990e commit a5338fa

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

utils/cache.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ const cacheResponse = (options = {}) => {
160160
const oldSend = res.send;
161161

162162
res.send = (body) => {
163+
if (res.statusCode < 200 || res.statusCode >= 300) {
164+
res.send = oldSend;
165+
return res.send(body);
166+
}
167+
163168
const cacheValue = {
164169
priority: priority,
165170
response: body,
@@ -206,19 +211,23 @@ const invalidateCache = (options = {}) => {
206211
}
207212

208213
return async (req, res, next) => {
209-
try {
210-
for (const key of keys) {
211-
const cachedKeysSet = cachedKeys.getCachedKeys(key);
212-
for (const cachedKey of cachedKeysSet) {
213-
pool.evict(cachedKey);
214+
res.on("finish", () => {
215+
if (res.statusCode < 200 || res.statusCode >= 300) {
216+
return;
217+
}
218+
try {
219+
for (const key of keys) {
220+
const cachedKeysSet = cachedKeys.getCachedKeys(key);
221+
for (const cachedKey of cachedKeysSet) {
222+
pool.evict(cachedKey);
223+
}
224+
cachedKeys.removeModelKey(key);
214225
}
215-
cachedKeys.removeModelKey(key);
226+
} catch (err) {
227+
logger.error(`Error while removing cached response ${err}`);
216228
}
217-
} catch (err) {
218-
logger.error(`Error while removing cached response ${err}`);
219-
} finally {
220-
next();
221-
}
229+
});
230+
next();
222231
};
223232
};
224233

0 commit comments

Comments
 (0)