Skip to content

Commit

Permalink
feat(HttpCache): add cache bust parameter
Browse files Browse the repository at this point in the history
This commit adds a cache-busting parameter to the HttpCache.  This
allows a developer to refresh a stale cache for things like updates.
  • Loading branch information
Jonathan Niles authored and jniles committed Sep 10, 2018
1 parent 110204c commit 2ffd167
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
9 changes: 4 additions & 5 deletions client/src/js/services/HttpCacheService.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,16 @@ function HttpCacheService($interval) {
function HttpCache(callback, duration = HTTP_CACHE_DEFAULT_TIMEOUT) {
const cache = new Map();


function read(...args) {
const key = serialize(args);
function read(id, parameters, cacheBust = false) {
const key = serialize(id, parameters);

// if the cache has been populated return the value from memory
if (cache.has(key)) {
if (cache.has(key) && !cacheBust) {
return cache.get(key);
}

// call the callback to get the result and cache it
const promise = callback(...args);
const promise = callback(id, parameters);
cache.set(key, promise);

// remove the result from the cache after a duration. Repeated only once
Expand Down
22 changes: 11 additions & 11 deletions client/src/modules/accounts/AccountStoreService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ AccountStoreService.$inject = [

// Temporary service until caching API services is well designed
function AccountStoreService($q, Accounts, AccountTypes, Store) {
var service = this;
var initialLoad = true;
var initTypeLoad = true;
const service = this;
let initialLoad = true;
let initTypeLoad = true;

var accounts = new Store();
var accountTypes = new Store();
const accounts = new Store();
const accountTypes = new Store();

var request = Accounts.read(null, { detailed : 1 })
.then(function (result) {
const request = Accounts.read(null, { detailed : 1 }, true)
.then((result) => {
accounts.setData(result);
initialLoad = false;
return accounts.data;
});

var typeRequest = AccountTypes.getAccountType()
.then(function (result) {
const typeRequest = AccountTypes.getAccountType()
.then((result) => {
accountTypes.setData(result);
initTypeLoad = false;
return accountTypes.data;
Expand All @@ -38,7 +38,7 @@ function AccountStoreService($q, Accounts, AccountTypes, Store) {

function accountStore() {
if (initialLoad) {
return request.then(function () {
return request.then(() => {
return accounts;
});
}
Expand All @@ -48,7 +48,7 @@ function AccountStoreService($q, Accounts, AccountTypes, Store) {

function typeStore() {
if (initTypeLoad) {
return typeRequest.then(function () {
return typeRequest.then(() => {
return accountTypes;
});
}
Expand Down
11 changes: 6 additions & 5 deletions client/src/modules/accounts/accounts.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ function AccountService(Api, bhConstants, HttpCache) {
.then(service.util.unwrapHttpResponse);
}

const callback = (id, options) => Api.read.call(service, id, options)
.then(handleAccounts);

const callback = (id, options) => Api.read.call(service, id, options);
const fetcher = HttpCache(callback);

/**
Expand All @@ -54,11 +52,14 @@ function AccountService(Api, bhConstants, HttpCache) {
*
* @param {Number} id - the id of the account to fetch (optional).
* @param {Object} options - options to be passed as query strings (optional).
* @param {Boolean} cacheBust - ignore the cache and send the HTTP request directly
* to the server.
* @return {Promise} promise - resolves to either a JSON (if id provided) or
* an array of JSONs.
*/
function read(id, options) {
return fetcher(id, options);
function read(id, options, cacheBust = false) {
return fetcher(id, options, cacheBust)
.then(handleAccounts);
}

function handleAccounts(accounts) {
Expand Down
14 changes: 13 additions & 1 deletion test/client-unit/services/AccountService.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global inject expect */

describe('AccountService', () => {

let Accounts;
let $httpBackend;
let $interval;
Expand Down Expand Up @@ -85,4 +84,17 @@ describe('AccountService', () => {
expect(a).to.deep.equal(resA);
expect(b).to.deep.equal(resB);
});

it('#read() will bust cached values with a third parameter', () => {
let count = 10;

while (count--) {
// third parameter will bust the cache
Accounts.read(1, {}, true);
}

// this would throw if too many requests were called.
expect(() => $httpBackend.flush(10)).not.to.throw();
expect(() => $httpBackend.flush(1)).to.throw();
});
});

0 comments on commit 2ffd167

Please sign in to comment.