Skip to content

Commit d2de7e2

Browse files
committed
MOBILE-1966 glossary: Performance improvements on cache invalidation
1 parent 8f529ea commit d2de7e2

File tree

3 files changed

+84
-34
lines changed

3 files changed

+84
-34
lines changed

www/addons/mod/glossary/services/glossary.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,28 @@ angular.module('mm.addons.mod_glossary')
607607
*/
608608
self.invalidateEntry = function(entryId, siteId) {
609609
return $mmSitesManager.getSite(siteId).then(function(site) {
610-
var key = self._getEntryCacheKey(entryId);
611-
return site.invalidateWsCacheForKey(key);
610+
return site.invalidateWsCacheForKey(self._getEntryCacheKey(entryId));
612611
});
613612
};
614613

614+
/**
615+
* Invalidate cache of all entries in the array.
616+
*
617+
* @param {Array} entries Entry objects to invalidate.
618+
* @param {String} [siteId] Site ID. If not defined, current site.
619+
* @return {Promise} Resolved when data is invalidated.
620+
*/
621+
function invalidateEntries(entries, siteId) {
622+
var keys = [];
623+
angular.forEach(entries, function(entry) {
624+
keys.push(self._getEntryCacheKey(entry.id));
625+
});
626+
627+
return $mmSitesManager.getSite(siteId).then(function(site) {
628+
return site.invalidateMultipleWsCacheForKey(keys);
629+
});
630+
}
631+
615632
/**
616633
* Invalidate the prefetched content except files.
617634
* To invalidate files, use $mmaModGlossary#invalidateFiles.
@@ -641,37 +658,38 @@ angular.module('mm.addons.mod_glossary')
641658
* @module mm.addons.mod_glossary
642659
* @ngdoc method
643660
* @name $mmaModGlossary#invalidateGlossaryEntries
644-
* @param {Object} glossary The glossary object.
645-
* @return {Promise} Promise resolved when data is invalidated.
661+
* @param {Object} glossary The glossary object.
662+
* @param {Object} onlyEntriesList If true, entries won't be invalidated.
663+
* @return {Promise} Promise resolved when data is invalidated.
646664
*/
647-
self.invalidateGlossaryEntries = function(glossary) {
648-
return self.fetchAllEntries(self.getEntriesByLetter, [glossary.id, 'ALL'], true).then(function(entries) {
649-
var promises = [];
665+
self.invalidateGlossaryEntries = function(glossary, onlyEntriesList) {
666+
var promises = [];
650667

651-
angular.forEach(entries, function(entry) {
652-
promises.push(self.invalidateEntry(entry.id));
653-
});
654-
655-
angular.forEach(glossary.browsemodes, function(mode) {
656-
switch(mode) {
657-
case 'letter':
658-
promises.push(self.invalidateEntriesByLetter(glossary.id, 'ALL'));
659-
break;
660-
case 'cat':
661-
promises.push(self.invalidateEntriesByCategory(glossary.id, mmaModGlossaryShowAllCategories));
662-
break;
663-
case 'date':
664-
promises.push(self.invalidateEntriesByDate(glossary.id, 'CREATION', 'DESC'));
665-
promises.push(self.invalidateEntriesByDate(glossary.id, 'UPDATE', 'DESC'));
666-
break;
667-
case 'author':
668-
promises.push(self.invalidateEntriesByAuthor(glossary.id, 'ALL', 'LASTNAME', 'ASC'));
669-
break;
670-
}
671-
});
668+
if (!onlyEntriesList) {
669+
promises.push(self.fetchAllEntries(self.getEntriesByLetter, [glossary.id, 'ALL'], true).then(function(entries) {
670+
return invalidateEntries(entries);
671+
}));
672+
}
672673

673-
return $q.all(promises);
674+
angular.forEach(glossary.browsemodes, function(mode) {
675+
switch(mode) {
676+
case 'letter':
677+
promises.push(self.invalidateEntriesByLetter(glossary.id, 'ALL'));
678+
break;
679+
case 'cat':
680+
promises.push(self.invalidateEntriesByCategory(glossary.id, mmaModGlossaryShowAllCategories));
681+
break;
682+
case 'date':
683+
promises.push(self.invalidateEntriesByDate(glossary.id, 'CREATION', 'DESC'));
684+
promises.push(self.invalidateEntriesByDate(glossary.id, 'UPDATE', 'DESC'));
685+
break;
686+
case 'author':
687+
promises.push(self.invalidateEntriesByAuthor(glossary.id, 'ALL', 'LASTNAME', 'ASC'));
688+
break;
689+
}
674690
});
691+
692+
return $q.all(promises);
675693
};
676694

677695
/**

www/addons/mod/glossary/services/glossary_sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ angular.module('mm.addons.mod_glossary')
192192
// Responses deleted, add a warning.
193193
result.warnings.push($translate.instant('mm.core.warningofflinedatadeleted', {
194194
component: $mmCourse.translateModuleName('glossary'),
195-
name: data.name,
195+
name: data.concept,
196196
error: error.error
197197
}));
198198
});
@@ -208,7 +208,7 @@ angular.module('mm.addons.mod_glossary')
208208
if (result.updated && courseId) {
209209
// Data has been sent to server. Now invalidate the WS calls.
210210
return $mmaModGlossary.getGlossaryById(courseId, glossaryId).then(function(glossary) {
211-
return $mmaModGlossary.invalidateGlossaryEntries(glossary);
211+
return $mmaModGlossary.invalidateGlossaryEntries(glossary, true);
212212
}).catch(function() {
213213
// Ignore errors.
214214
});

www/core/lib/sitesfactory.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,37 @@ angular.module('mm.core')
816816
});
817817
};
818818

819+
/**
820+
* Invalidates all the cache entries with for an array of keys.
821+
*
822+
* @param {Array} keys Keys to search.
823+
* @return {Promise} Promise resolved when the cache entries are invalidated.
824+
*/
825+
Site.prototype.invalidateMultipleWsCacheForKey = function(keys) {
826+
var db = this.db;
827+
if (!db) {
828+
return $q.reject();
829+
}
830+
831+
var allEntries = [],
832+
promises = [];
833+
834+
$log.debug('Invalidating multiple cache keys');
835+
angular.forEach(keys, function(key) {
836+
if (key) {
837+
promises.push(db.whereEqual(mmCoreWSCacheStore, 'key', key).then(function(entries) {
838+
if (entries && entries.length > 0) {
839+
allEntries.concat(entries);
840+
}
841+
}));
842+
}
843+
});
844+
845+
return $q.all(promises).then(function() {
846+
return invalidateWsCacheEntries(db, allEntries);
847+
});
848+
};
849+
819850
/**
820851
* Invalidates all the cache entries whose key starts with a certain value.
821852
*
@@ -1263,9 +1294,10 @@ angular.module('mm.core')
12631294
function invalidateWsCacheEntries(db, entries) {
12641295
var promises = [];
12651296
angular.forEach(entries, function(entry) {
1266-
entry.expirationtime = 0;
1267-
var promise = db.insert(mmCoreWSCacheStore, entry);
1268-
promises.push(promise);
1297+
if (entry.expirationtime > 0) {
1298+
entry.expirationtime = 0;
1299+
promises.push(db.insert(mmCoreWSCacheStore, entry));
1300+
}
12691301
});
12701302
return $q.all(promises);
12711303
}

0 commit comments

Comments
 (0)