MOBILE-3269 filter: Fix memory cache issue with contextLevels

Each contextLevel overrode the other contextLevels, causing some cached data to be lost
main
Dani Palou 2020-03-17 12:23:14 +01:00
parent d9e851373e
commit 02173db1b1
1 changed files with 19 additions and 12 deletions

View File

@ -38,10 +38,12 @@ export class CoreFilterHelperProvider {
protected moduleContextsCache: {
[siteId: string]: {
[courseId: number]: {
[contextLevel: string]: {
contexts: CoreFilterClassifiedFilters,
time: number
}
}
time: number,
},
},
},
} = {};
constructor(logger: CoreLoggerProvider,
@ -110,7 +112,7 @@ export class CoreFilterHelperProvider {
return getFilters().then((contexts) => {
return this.filterProvider.getAvailableInContexts(contexts, siteId).then((filters) => {
this.storeInMemoryCache(options.courseId, filters, siteId);
this.storeInMemoryCache(options.courseId, contextLevel, filters, siteId);
return filters[contextLevel][instanceId] || [];
});
@ -278,14 +280,16 @@ export class CoreFilterHelperProvider {
const siteId = site.getId();
// Check if we have the context in the memory cache.
if (this.moduleContextsCache[siteId] && this.moduleContextsCache[siteId][courseId]) {
const cachedCourse = this.moduleContextsCache[siteId][courseId];
if (this.moduleContextsCache[siteId] && this.moduleContextsCache[siteId][courseId] &&
this.moduleContextsCache[siteId][courseId][contextLevel]) {
const cachedData = this.moduleContextsCache[siteId][courseId][contextLevel];
if (!this.appProvider.isOnline() ||
Date.now() <= cachedCourse.time + site.getExpirationDelay(CoreSite.FREQUENCY_RARELY)) {
Date.now() <= cachedData.time + site.getExpirationDelay(CoreSite.FREQUENCY_RARELY)) {
// We can use cache, return the filters if found.
return cachedCourse.contexts[contextLevel] && cachedCourse.contexts[contextLevel][instanceId];
return cachedData.contexts[contextLevel] && cachedData.contexts[contextLevel][instanceId];
}
}
}
@ -316,11 +320,14 @@ export class CoreFilterHelperProvider {
* @param contexts Filters to store, classified by contextlevel and instanceid
* @param siteId Site ID.
*/
protected storeInMemoryCache(courseId: number, contexts: CoreFilterClassifiedFilters, siteId: string): void {
protected storeInMemoryCache(courseId: number, contextLevel: string, contexts: CoreFilterClassifiedFilters, siteId: string)
: void {
this.moduleContextsCache[siteId] = this.moduleContextsCache[siteId] || {};
this.moduleContextsCache[siteId][courseId] = {
this.moduleContextsCache[siteId][courseId] = this.moduleContextsCache[siteId][courseId] || {};
this.moduleContextsCache[siteId][courseId][contextLevel] = {
contexts: contexts,
time: Date.now()
time: Date.now(),
};
}
}