Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Merge pull request #407 from stormpath/feature-add-ability-to-invalid…
Browse files Browse the repository at this point in the history
…ate-cache

Add ability to invalidate the cache of an InstanceResource
  • Loading branch information
typerandom committed Mar 10, 2016
2 parents 5d6ddb6 + 0c0341f commit 7643752
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
49 changes: 49 additions & 0 deletions lib/resource/InstanceResource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var async = require('async');

var Resource = require('./Resource');
var utils = require('../utils');

Expand Down Expand Up @@ -55,4 +57,51 @@ InstanceResource.prototype.delete = function deleteResource(callback) {
this.dataStore.deleteResource(this, callback);
};

InstanceResource.prototype.invalidate = function invalidateResource(callback) {
var self = this;

var tasks = [];
var visited = {};

function makeInvalidationTask(href) {
return function (itemCallback) {
// Swallow any errors. For cache invalidation those aren't that
// important and will also break the async.parallel() flow.
self.dataStore._evict(href, function () {
itemCallback();
});
};
}

function walkBuildInvalidationTasks(source) {
var rootHref = source.href;

if (rootHref in visited) {
return;
}

visited[rootHref] = null;
tasks.push(makeInvalidationTask(source.href));

for (var key in source) {
var item = source[key];

if (!item || !item.href) {
continue;
}

// Only walk child resources.
if (item.href.indexOf(rootHref) === 0) {
walkBuildInvalidationTasks(item);
}
}
}

if (this.href) {
walkBuildInvalidationTasks(this);
}

async.parallel(tasks, callback);
};

module.exports = InstanceResource;
46 changes: 41 additions & 5 deletions test/sp.resource.instanceResource_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var common = require('./common');
var sinon = common.sinon;
var nock = common.nock;

var u = common.u;
var nock = common.nock;
var sinon = common.sinon;
var assert = common.assert;

var Resource = require('../lib/resource/Resource');
var InstanceResource = require('../lib/resource/InstanceResource');
Expand All @@ -23,7 +25,7 @@ describe('Resources: ', function () {
}, ds);
});

describe('call to get resource', function(){
describe('call to get()', function(){
describe('without property name', function(){
var sandbox, error, cb, getResourceSpy;
before(function(){
Expand Down Expand Up @@ -161,7 +163,7 @@ describe('Resources: ', function () {
});
});

describe('call to save resource', function(){
describe('call to save()', function(){
var sandbox, cb, saveResourceSpy;
before(function(){
cb = function(){};
Expand All @@ -182,7 +184,7 @@ describe('Resources: ', function () {
});
});

describe('call to delete resource', function(){
describe('call to delete()', function(){
var sandbox, cb, deleteResourceSpy;
before(function(){
cb = function(){};
Expand All @@ -201,5 +203,39 @@ describe('Resources: ', function () {
deleteResourceSpy.should.have.been.calledWith(instanceResource, cb);
});
});

describe('call to invalidate()', function () {
var sandbox, evictStub, evictedKeys;

before(function (done) {
evictedKeys = [];
sandbox = sinon.sandbox.create();

evictStub = sandbox.stub(ds, '_evict', function (key, callback) {
evictedKeys.push(key);
callback();
});

instanceResource.href = '32a0c55b-9fad-4aeb-8187-1149b4f980ce';
instanceResource.customData = {
href: instanceResource.href + '/ceb8c607-78ca-4cc0-872a-098e895ec1a5'
};

instanceResource.invalidate(done);
});

after(function () {
delete instanceResource['href'];
delete instanceResource['customData'];
sandbox.restore();
});

it('should invalidate the account and all of its child resources', function (done) {
assert.equal(evictedKeys.length, 2);
assert.equal(evictedKeys[0], instanceResource.href);
assert.equal(evictedKeys[1], instanceResource.customData.href);
done();
});
});
});
});

0 comments on commit 7643752

Please sign in to comment.