Skip to content

Commit

Permalink
fix(data-point-service): resetCache should clear stale data (#494)
Browse files Browse the repository at this point in the history
* fix(data-point-service): resetCache should clear stale data

resetCache doesn't remove stale data so, it happens that for removed contents it still provide old
data. Implemented the clear of stale data on promise chain when the clear cache parameter is applied

fix #492
  • Loading branch information
schiavig authored Feb 12, 2024
1 parent 3054143 commit 8f2e60f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
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");
});
});

0 comments on commit 8f2e60f

Please sign in to comment.