Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(data-point-service): resetCache should clear stale data #494

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@
"question",
"code"
]
},
{
"login": "schiavig",
"name": "Giampiero Schiavi",
"avatar_url": "https://avatars.githubusercontent.com/u/85735845?v=4",
"profile": "https://github.com/schiavig",
"contributions": [
"code",
"test",
"bug"
]
}
],
"repoType": "github"
Expand Down
4 changes: 4 additions & 0 deletions packages/data-point-service/lib/cache-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ function before(service, ctx, next) {
const cache = EntityCacheParams.getCacheParams(ctx.context.params);

if (!cache.ttl || ctx.locals.resetCache === true) {
if (ctx.locals.resetCache === true) {
const currentEntryKey = module.exports.generateKey(cache.cacheKey, ctx);
RedisController.deleteSWRStaleEntry(service, currentEntryKey);
}
next();
return false;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/data-point-service/lib/cache-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ describe("before", () => {
.spyOn(RedisController, "getEntry")
.mockReturnValue("noTTLEntry");

mocks.deleteSWRStaleEntry = jest
.spyOn(RedisController, "deleteSWRStaleEntry")
.mockReturnValue(Promise.resolve());

return mocks;
}

Expand All @@ -564,14 +568,15 @@ describe("before", () => {
expect(result).toEqual(false);
expect(mocks.next).toBeCalledWith();
});
it("should call next and return false if resetCache is true", () => {
it("should delete stale and call next returning false if resetCache is true", () => {
const mocks = createMocks();
mocks.ctx.locals.resetCache = true;
const result = CacheMiddleware.before(
mocks.service,
mocks.ctx,
mocks.next
);
expect(mocks.deleteSWRStaleEntry).toBeCalled();
expect(result).toEqual(false);
expect(mocks.next).toBeCalledWith();
});
Expand Down
12 changes: 12 additions & 0 deletions packages/data-point-service/lib/redis-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,23 @@ function deleteSWRControlEntry(service, key) {
return deleteEntry(service, createSWRControlKey(key));
}

/**
* Delete stale entry by key
* @param {Service} service Service instance
* @param {String} key entry key
* @returns {Promise}
*/
function deleteSWRStaleEntry(service, key) {
const staleKey = createSWRStaleKey(key);
return deleteEntry(service, staleKey);
}

module.exports = {
createSWRControlKey,
createSWRStaleKey,
deleteEntry,
deleteSWRControlEntry,
deleteSWRStaleEntry,
getEntry,
getSWRControlEntry,
getSWRStaleEntry,
Expand Down
12 changes: 12 additions & 0 deletions packages/data-point-service/lib/redis-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,15 @@ describe("deleteSWRControlEntry", () => {
expect(service.cache.del).toBeCalledWith("key:swr.control");
});
});

describe("deleteSWRStaleEntry", () => {
it("should delete a stale entry", () => {
const service = {
cache: {
del: jest.fn()
}
};
RedisController.deleteSWRStaleEntry(service, "key");
expect(service.cache.del).toBeCalledWith("key:swr.stale");
});
});