Skip to content

Commit 69bf555

Browse files
Merge pull request #1322 from Real-Dev-Squad/fix/cache-success
Cache new response if successful
2 parents 85c584b + da06fc2 commit 69bf555

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

test/unit/middlewares/cache.test.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("Middleware | Utils | cache", function () {
1919
};
2020

2121
const response = {
22+
statusCode: 200,
2223
send: sinon.spy(),
2324
};
2425

@@ -39,14 +40,16 @@ describe("Middleware | Utils | cache", function () {
3940
expect(response.send.callCount).to.equal(2);
4041
});
4142

42-
it("should invalidate stale the response", function () {
43+
it("should invalidate stale response", function () {
4344
const cacheTestKey = "__cache__2";
4445

4546
const request = {
4647
method: "GET",
4748
originalUrl: "/test2",
4849
};
4950
const response = {
51+
on: sinon.spy(),
52+
statusCode: 200,
5053
send: sinon.spy(),
5154
};
5255

@@ -66,6 +69,9 @@ describe("Middleware | Utils | cache", function () {
6669
cacheMiddlewareForInvalidation(request, response, nextSpy);
6770
response.send(responseBody);
6871

72+
response.on.withArgs("finish").yield();
73+
74+
expect(response.on.callCount).to.equal(1);
6975
expect(nextSpy.callCount).to.equal(2);
7076
expect(response.send.callCount).to.equal(2);
7177

@@ -75,4 +81,79 @@ describe("Middleware | Utils | cache", function () {
7581
expect(nextSpy.callCount).to.equal(3);
7682
expect(response.send.callCount).to.equal(3);
7783
});
84+
85+
it("should not cache the response", function () {
86+
const cacheTestKey = "__cache__3";
87+
const request = {
88+
method: "GET",
89+
originalUrl: "/test3",
90+
};
91+
92+
const response = {
93+
statusCode: 400,
94+
send: sinon.spy(),
95+
};
96+
97+
const nextSpy = sinon.spy();
98+
99+
const cacheMiddleware = cacheResponse({ invalidationKey: cacheTestKey });
100+
101+
cacheMiddleware(request, response, nextSpy);
102+
103+
response.send(responseBody);
104+
105+
expect(nextSpy.callCount).to.equal(1);
106+
expect(response.send.callCount).to.equal(1);
107+
108+
cacheMiddleware(request, response, nextSpy);
109+
response.send(responseBody);
110+
111+
expect(nextSpy.callCount).to.equal(2);
112+
113+
expect(response.send.callCount).to.equal(2);
114+
});
115+
116+
it("should not invalidate stale the response if theres an error", function () {
117+
const cacheTestKey = "__cache__4";
118+
119+
const request = {
120+
method: "GET",
121+
originalUrl: "/test4",
122+
};
123+
const response = {
124+
on: sinon.spy(),
125+
statusCode: 200,
126+
send: sinon.spy(),
127+
};
128+
129+
const nextSpy = sinon.spy();
130+
131+
const cacheMiddlewareForCache = cacheResponse({ invalidationKey: cacheTestKey });
132+
133+
cacheMiddlewareForCache(request, response, nextSpy);
134+
135+
response.send(responseBody);
136+
137+
expect(nextSpy.callCount).to.equal(1);
138+
expect(response.send.callCount).to.equal(1);
139+
140+
response.statusCode = 400;
141+
142+
const cacheMiddlewareForInvalidation = invalidateCache({ invalidationKeys: [cacheTestKey] });
143+
144+
cacheMiddlewareForInvalidation(request, response, nextSpy);
145+
response.send(responseBody);
146+
147+
response.on.withArgs("finish").yield();
148+
149+
expect(response.on.callCount).to.equal(1);
150+
expect(nextSpy.callCount).to.equal(2);
151+
expect(response.send.callCount).to.equal(2);
152+
153+
response.statusCode = 200;
154+
cacheMiddlewareForCache(request, response, nextSpy);
155+
156+
expect(nextSpy.callCount).to.equal(2);
157+
expect(response.send.callCount).to.equal(3);
158+
});
78159
});

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)