Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
feat(caching): #630 replace data when ready as opposed to invalidatin…
Browse files Browse the repository at this point in the history
…g first
  • Loading branch information
oyiptong committed May 4, 2016
1 parent d81211d commit 49f6ca7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
28 changes: 7 additions & 21 deletions lib/ActivityStreams.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ ActivityStreams.prototype = {
/* note: this will execute for each of the 3 notifications that occur
* when adding a visit: frecency:-1, frecency: real frecency, title */
if (!this._populatingCache.places) {
this._invalidatePlacesCache();
this._asyncBuildPlacesCache();
}

Expand Down Expand Up @@ -300,13 +299,14 @@ ActivityStreams.prototype = {
if (simplePrefs.prefs["query.cache"]) {
if (!this._populatingCache.places) {
this._populatingCache.places = true;
let opt = {replace: true};
yield Promise.all([
this._memoized.getTopFrecentSites(),
this._memoized.getRecentBookmarks(),
this._memoized.getRecentLinks(),
this._memoized.getHighlightsLinks(),
this._memoized.getHistorySize(),
this._memoized.getBookmarksSize(),
this._memoized.getTopFrecentSites(opt),
this._memoized.getRecentBookmarks(opt),
this._memoized.getRecentLinks(opt),
this._memoized.getHighlightsLinks(opt),
this._memoized.getHistorySize(opt),
this._memoized.getBookmarksSize(opt),
]);
this._populatingCache.places = false;
Services.obs.notifyObservers(null, "activity-streams-places-cache-complete", null);
Expand Down Expand Up @@ -360,20 +360,6 @@ ActivityStreams.prototype = {
}
},

/**
* Invalidates the places pageload cache
*/
_invalidatePlacesCache() {
this._memoizer.invalidateMemos([
"getTopFrecentSites",
"getRecentBookmarks",
"getRecentLinks",
"getHighlightsLinks",
"getHistorySize",
"getBookmarksSize",
]);
},

/**
* Sets up communications with the pages and manages the lifecycle of workers
*/
Expand Down
13 changes: 12 additions & 1 deletion lib/Memoizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ Memoizer.prototype = {
*/
memoize(key, func) {
return (...args) => {

// figure out if we need to replace the cache for this query
let replace = false;
args = args.filter(arg => {
if (typeof arg === "object" && "replace" in arg) {
replace = arg.replace;
return false;
}
return true;
});

// allow object params to be passed, as is expected of query functions
let repr = this._makeRepr(args);

if (this._cacheEnabled && this.hasRepr(key, repr)) {
if (this._cacheEnabled && !replace && this.hasRepr(key, repr)) {
Services.obs.notifyObservers(null, `${key}-cache`, "hit");
return new Promise(resolve => resolve(this.getData(key, repr)));
} else {
Expand Down
42 changes: 42 additions & 0 deletions test/test-Memoizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@ exports.test_memoizer = function*(assert) {
assert.equal(result, 2, "cached result is obtained");
};

exports.test_memoizer_replace_opt = function*(assert) {
let count = 0;
let testFunc = () => {
return ++count;
};
let func = gMemoizer.memoize("testKey", testFunc);
let result;

result = yield func();
assert.equal(result, 1, "test function executes");

result = yield func();
assert.equal(result, 1, "cached result is obtained");

result = yield func({replace: true});
assert.equal(result, 2, "test function executes");

result = yield func();
assert.equal(result, 2, "cached result is obtained");
};

exports.test_memoizer_replace_opt_sub_key = function*(assert) {
let count = 0;
let testFunc = () => {
return ++count;
};
let func = gMemoizer.memoize("testKey", testFunc);
let result;

result = yield func("sub-key");
assert.equal(result, 1, "test function executes");

result = yield func("sub-key");
assert.equal(result, 1, "cached result is obtained");

result = yield func("sub-key", {replace: true});
assert.equal(result, 2, "test function executes");

result = yield func("sub-key");
assert.equal(result, 2, "cached result is obtained");
};

exports.test_memoizer_prefs = function*(assert) {
simplePrefs.prefs["query.cache"] = false;
let count = 0;
Expand Down

0 comments on commit 49f6ca7

Please sign in to comment.