Skip to content

Commit

Permalink
removed caching while getting groups
Browse files Browse the repository at this point in the history
  • Loading branch information
bountyC0d3r committed Nov 30, 2022
1 parent fed3ea1 commit f52482f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/services/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,26 +320,32 @@ class GroupService {
* @return {Promise} Resolves to ID array.
*/
async getGroupTreeIds(rootGroupId, maxage = 5 * 60 * 1000) {
const now = Date.now();
const cache = this.private.cache.groupTreeIds;
//TODO: Once the fix is validated and working, remove all commented code related to caching
/**
* Removing the caching mechanism,
* as the group created on the day will only be pickedup on the next day
* and stored in the cache
*/
// const now = Date.now();
// const cache = this.private.cache.groupTreeIds;

/* Clean-up: removes stale records from the cache. */
const CLEAN_UP_INTERVAL = 24 * 60 * 60 * 1000; // 1 day in ms.
if (now - cache.lastCleanUp > CLEAN_UP_INTERVAL) {
_.forOwn(cache, ({ timestamp }, key) => {
if (now - timestamp > CLEAN_UP_INTERVAL) delete cache[key];
});
cache.lastCleanUp = now;
}
// const CLEAN_UP_INTERVAL = 24 * 60 * 60 * 1000; // 1 day in ms.
// if (now - cache.lastCleanUp > CLEAN_UP_INTERVAL) {
// _.forOwn(cache, ({ timestamp }, key) => {
// if (now - timestamp > CLEAN_UP_INTERVAL) delete cache[key];
// });
// cache.lastCleanUp = now;
// }

/* If result is found in cache, and is fresh enough, return it. */
const cached = cache[rootGroupId];
if (cached && now - cached.timestamp < maxage) return _.clone(cached.data);
// const cached = cache[rootGroupId];
// if (cached && now - cached.timestamp < maxage) return _.clone(cached.data);

/* Otherwise, fetch result from the API, write it to the cache, and
* finally return that. */
const res = reduceGroupIds(await this.getGroup(rootGroupId));
cache[rootGroupId] = { data: res, timestamp: now };
// cache[rootGroupId] = { data: res, timestamp: now };
return _.clone(res);
}

Expand Down Expand Up @@ -409,4 +415,4 @@ export function getService(tokenV3) {
return lastInstance;
}

export default undefined;
export default undefined;

0 comments on commit f52482f

Please sign in to comment.