diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index 93876bf9..5b47ff8c 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -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 @@ -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; } @@ -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)); }); @@ -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(); + } + } ); };