From 1d97cf288c50254f4e05522f477c884de299044e Mon Sep 17 00:00:00 2001 From: cz-Michael Date: Mon, 12 Dec 2016 17:57:18 -0500 Subject: [PATCH 1/4] good checkpoint to make collection cache work --- lib/cache/Cache.js | 12 ++++++++- lib/cache/CacheHandler.js | 51 +++++++++++++++++++++++++++++++++------ lib/cache/MemoryStore.js | 2 ++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/cache/Cache.js b/lib/cache/Cache.js index b575eae0..903d16d0 100644 --- a/lib/cache/Cache.js +++ b/lib/cache/Cache.js @@ -87,7 +87,17 @@ Cache.prototype.put = function (key, value, _new, cb) { _new = true; } this.stats.put(_new); - this.store.set(key, new CacheEntry(value), cb); +// console.log('kkkkkkkkkkey', key, JSON.stringify(value, null, 2)) +let cacheEntry = new CacheEntry(value) +// console.log(cacheEntry) +// console.log(this.store) + this.store.set(key, cacheEntry, (err, data)=>{ + // console.log(err, data) + if (err) { + return cb(err) + } + cb(null, data) + }); }; Cache.prototype.delete = function (key, cb) { diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index 93876bf9..89edc586 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -10,7 +10,7 @@ var utils = require('../utils'); var CACHE_REGIONS = ['applications', 'directories', 'accounts', 'groups', 'groupMemberships', 'tenants', 'accountStoreMappings','apiKeys','idSiteNonces', - 'customData', 'organizations']; + 'customData']; // singleton of DisabledCache wrapped into Cache instance var disabledCache = new Cache({store: DisabledCache}); @@ -70,15 +70,20 @@ function CacheHandler(config) { //private function: function getCacheByHref(cacheManager, href) { - +//console.log('GGGGGGGGGET', 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; } @@ -101,9 +106,11 @@ CacheHandler.prototype.get = function getCachedResource(href, callback) { */ function buildCacheableResourcesFromParentObject(object){ +// console.log(JSON.stringify(object, null, 2)) var resourcesToCache = []; var parentResource = {}; if(utils.isCollectionData(object)){ + object.items.forEach(function(resource) { Array.prototype.push.apply(resourcesToCache, buildCacheableResourcesFromParentObject(resource)); }); @@ -124,10 +131,12 @@ function buildCacheableResourcesFromParentObject(object){ resourcesToCache.push(parentResource); } } +// console.log('+++++++++++++++++++', JSON.stringify(object, null, 2)) return resourcesToCache; } CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { +console.log('****************', href) var _this = this; if (typeof _new === 'function') { @@ -138,10 +147,38 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { async.each( buildCacheableResourcesFromParentObject(data), function(resource,next) { - getCacheByHref(_this.cacheManager, resource.href) - .put(resource.href, resource, _new, next); + let cache = getCacheByHref(_this.cacheManager, resource.href) + cache.put(resource.href, resource, _new, next); }, - cb + (err) => { + if (err) { + return cb(err) + } else if(utils.isCollectionData(data)) { + console.log('it is a collection') + 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; + } + }); + let cache = getCacheByHref(_this.cacheManager, data.href) + cache.put(data.href, parentResource, _new, (err) => { + if (err) { +console.log('eeeeeeeeeeeeeerr', err) + return cb(err) + } + cb() + }) + } else { + cb() + } + } ); }; diff --git a/lib/cache/MemoryStore.js b/lib/cache/MemoryStore.js index bfdc452c..ca969cff 100644 --- a/lib/cache/MemoryStore.js +++ b/lib/cache/MemoryStore.js @@ -9,7 +9,9 @@ function MemoryStore() { }; this.set = function set(key, val, cb) { +// console.log('setting store', val) store[key] = val; +// console.log('sssssssstore', JSON.stringify(store, null, 2)) return cb(null, val); }; From 5d441785b90159fc5c87f2546c1db75661a8cb66 Mon Sep 17 00:00:00 2001 From: cz-Michael Date: Tue, 13 Dec 2016 14:01:57 -0500 Subject: [PATCH 2/4] enable groups collection request cache --- lib/cache/Cache.js | 7 +------ lib/cache/CacheHandler.js | 25 +++++++------------------ lib/cache/MemoryStore.js | 2 -- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/lib/cache/Cache.js b/lib/cache/Cache.js index 903d16d0..9b04ad58 100644 --- a/lib/cache/Cache.js +++ b/lib/cache/Cache.js @@ -87,12 +87,7 @@ Cache.prototype.put = function (key, value, _new, cb) { _new = true; } this.stats.put(_new); -// console.log('kkkkkkkkkkey', key, JSON.stringify(value, null, 2)) -let cacheEntry = new CacheEntry(value) -// console.log(cacheEntry) -// console.log(this.store) - this.store.set(key, cacheEntry, (err, data)=>{ - // console.log(err, data) + this.store.set(key, new CacheEntry(value), (err, data)=>{ if (err) { return cb(err) } diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index 89edc586..ca626d91 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -9,8 +9,8 @@ var DisabledCache = require('./DisabledCache'); var utils = require('../utils'); var CACHE_REGIONS = ['applications', 'directories', 'accounts', 'groups', - 'groupMemberships', 'tenants', 'accountStoreMappings','apiKeys','idSiteNonces', - 'customData']; + 'groupMemberships', 'tenants', 'accountStoreMappings', 'apiKeys', 'idSiteNonces', + 'customData', 'organizations']; // singleton of DisabledCache wrapped into Cache instance var disabledCache = new Cache({store: DisabledCache}); @@ -70,7 +70,6 @@ function CacheHandler(config) { //private function: function getCacheByHref(cacheManager, href) { -//console.log('GGGGGGGGGET', 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: @@ -106,7 +105,6 @@ CacheHandler.prototype.get = function getCachedResource(href, callback) { */ function buildCacheableResourcesFromParentObject(object){ -// console.log(JSON.stringify(object, null, 2)) var resourcesToCache = []; var parentResource = {}; if(utils.isCollectionData(object)){ @@ -131,12 +129,10 @@ function buildCacheableResourcesFromParentObject(object){ resourcesToCache.push(parentResource); } } -// console.log('+++++++++++++++++++', JSON.stringify(object, null, 2)) return resourcesToCache; } CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { -console.log('****************', href) var _this = this; if (typeof _new === 'function') { @@ -146,15 +142,14 @@ console.log('****************', href) async.each( buildCacheableResourcesFromParentObject(data), - function(resource,next) { - let cache = getCacheByHref(_this.cacheManager, resource.href) - cache.put(resource.href, resource, _new, next); + function(resource, next) { + getCacheByHref(_this.cacheManager, resource.href) + .put(resource.href, resource, _new, next); }, (err) => { if (err) { return cb(err) } else if(utils.isCollectionData(data)) { - console.log('it is a collection') var parentResource = {}; _.pairs(data).forEach(function(pair) { var prop = pair[0]; @@ -167,14 +162,8 @@ console.log('****************', href) parentResource[prop] = val; } }); - let cache = getCacheByHref(_this.cacheManager, data.href) - cache.put(data.href, parentResource, _new, (err) => { - if (err) { -console.log('eeeeeeeeeeeeeerr', err) - return cb(err) - } - cb() - }) + getCacheByHref(_this.cacheManager, data.href) + .put(data.href, parentResource, _new, cb) } else { cb() } diff --git a/lib/cache/MemoryStore.js b/lib/cache/MemoryStore.js index ca969cff..bfdc452c 100644 --- a/lib/cache/MemoryStore.js +++ b/lib/cache/MemoryStore.js @@ -9,9 +9,7 @@ function MemoryStore() { }; this.set = function set(key, val, cb) { -// console.log('setting store', val) store[key] = val; -// console.log('sssssssstore', JSON.stringify(store, null, 2)) return cb(null, val); }; From 428d7da049d2013fbcdac9b82efa23e608dbe470 Mon Sep 17 00:00:00 2001 From: cz-Michael Date: Wed, 14 Dec 2016 11:06:41 -0500 Subject: [PATCH 3/4] fix lint --- lib/cache/Cache.js | 7 +------ lib/cache/CacheHandler.js | 8 ++++---- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/cache/Cache.js b/lib/cache/Cache.js index 9b04ad58..b575eae0 100644 --- a/lib/cache/Cache.js +++ b/lib/cache/Cache.js @@ -87,12 +87,7 @@ Cache.prototype.put = function (key, value, _new, cb) { _new = true; } this.stats.put(_new); - this.store.set(key, new CacheEntry(value), (err, data)=>{ - if (err) { - return cb(err) - } - cb(null, data) - }); + this.store.set(key, new CacheEntry(value), cb); }; Cache.prototype.delete = function (key, cb) { diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index ca626d91..4fd5d386 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -76,9 +76,9 @@ function getCacheByHref(cacheManager, href) { //see: https://github.com/stormpath/stormpath-sdk-node/issues/11 if (href) { if (href.match(/customData/)) { - region = 'customData' + region = 'customData'; } else if (href.match(/groups/)) { - region = 'groups' + region = 'groups'; } else { region = href.split('/').slice(-2)[0]; } @@ -146,9 +146,9 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { getCacheByHref(_this.cacheManager, resource.href) .put(resource.href, resource, _new, next); }, - (err) => { + function (err) { if (err) { - return cb(err) + return cb(err); } else if(utils.isCollectionData(data)) { var parentResource = {}; _.pairs(data).forEach(function(pair) { From 6211f69a9bae864a30161a6855c0008c48645c45 Mon Sep 17 00:00:00 2001 From: cz-Michael Date: Wed, 14 Dec 2016 11:13:10 -0500 Subject: [PATCH 4/4] lint fix --- lib/cache/CacheHandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index 4fd5d386..5b47ff8c 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -163,9 +163,9 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { } }); getCacheByHref(_this.cacheManager, data.href) - .put(data.href, parentResource, _new, cb) + .put(data.href, parentResource, _new, cb); } else { - cb() + cb(); } } );