Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/cache/CacheHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var DisabledCache = require('./DisabledCache');
var utils = require('../utils');

var CACHE_REGIONS = ['applications', 'directories', 'accounts', 'groups',
'groupMemberships', 'tenants', 'accountStoreMappings','apiKeys','idSiteNonces',
'groupMemberships', 'tenants', 'accountStoreMappings', 'apiKeys', 'idSiteNonces',
'customData', 'organizations'];

// singleton of DisabledCache wrapped into Cache instance
Expand Down Expand Up @@ -70,15 +70,19 @@ function CacheHandler(config) {

//private function:
function getCacheByHref(cacheManager, href) {

var region = null;

//href is almost never null, but it is in the case of an AuthenticationResult (at the moment), so check for existence:
//see: https://github.com/stormpath/stormpath-sdk-node/issues/11
if (href) {
region = href.match(/customData/) ? 'customData' : (href.split('/').slice(-2)[0]);
if (href.match(/customData/)) {
region = 'customData';
} else if (href.match(/groups/)) {
region = 'groups';
} else {
region = href.split('/').slice(-2)[0];
}
}

if (!region || CACHE_REGIONS.indexOf(region) === -1) {
return disabledCache;
}
Expand All @@ -104,6 +108,7 @@ function buildCacheableResourcesFromParentObject(object){
var resourcesToCache = [];
var parentResource = {};
if(utils.isCollectionData(object)){

object.items.forEach(function(resource) {
Array.prototype.push.apply(resourcesToCache, buildCacheableResourcesFromParentObject(resource));
});
Expand Down Expand Up @@ -137,11 +142,32 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) {

async.each(
buildCacheableResourcesFromParentObject(data),
function(resource,next) {
function(resource, next) {
getCacheByHref(_this.cacheManager, resource.href)
.put(resource.href, resource, _new, next);
},
cb
function (err) {
if (err) {
return cb(err);
} else if(utils.isCollectionData(data)) {
var parentResource = {};
_.pairs(data).forEach(function(pair) {
var prop = pair[0];
var val = pair[1];
if(val && val.href && _.keys(val).length>1){
parentResource[prop] = {
href: val.href
};
}else{
parentResource[prop] = val;
}
});
getCacheByHref(_this.cacheManager, data.href)
.put(data.href, parentResource, _new, cb);
} else {
cb();
}
}
);

};
Expand Down