periodicjs.core.controller
Advanced tools
Comparing version 2.9.5 to 3.0.0
@@ -15,6 +15,5 @@ /* | ||
CoreUtilities, | ||
useCache = (global.CoreCache)? true: false; | ||
useCache = (global.CoreCache)? true: false, | ||
logger, appSettings, theme, applocals; | ||
var logger, appSettings, theme, applocals; | ||
/** | ||
@@ -169,3 +168,3 @@ * A core constructor that provides numerous controller helper functions. | ||
res.render(renderview, responseData,function(err,renderedview){ | ||
if(useCacheTest){ | ||
if(useCacheTest && global.CoreCache){ | ||
global.CoreCache.ViewCache.set({key:cachekey,val:renderedview},function(err){ | ||
@@ -217,3 +216,3 @@ if(err){ | ||
if(useCacheTest){ | ||
if(useCacheTest && global.CoreCache){ | ||
cachetype = global.CoreCache.ViewCache.type; | ||
@@ -334,4 +333,42 @@ cachekey = global.CoreCache.generateKeyFromRequestUrl(cachetype,req.originalUrl); | ||
selection = options.selection, | ||
query; | ||
query, | ||
cached = (typeof options.cached === 'boolean' && options.cached===true) && true, | ||
useCacheTest = (useCache && cached), | ||
cachetype = (global.CoreCache) ? global.CoreCache.DataCache.type : null, | ||
cachekey = (global.CoreCache) ? global.CoreCache.generateKeyFromRequestUrl(cachetype,model.modelName+'+'+docid) : null, | ||
cachedData, | ||
cachekeyexpirename = model.modelName.toLowerCase()+'_doc_cache_expires', | ||
// cachedDataDoc, | ||
queryCallback = function(err,doc){ | ||
if(useCacheTest && global.CoreCache && doc){ | ||
console.log('cachekeyexpirename',cachekeyexpirename,global.CoreCache.options[cachekeyexpirename]); | ||
global.CoreCache.DataCache.set({ | ||
key:cachekey, | ||
val:doc, | ||
expires:global.CoreCache.options[cachekeyexpirename] | ||
},function(err,expires){ | ||
if(err){ | ||
logger.error(err); | ||
} | ||
logger.silly('cached',cachekey,'expires',expires); | ||
}); | ||
callback(err,doc); | ||
} | ||
else{ | ||
callback(err,doc); | ||
} | ||
}, | ||
getDataQuery = function(){ | ||
if (population) { | ||
model.findOne(query).sort(sort).select(selection).populate(population).exec(queryCallback); | ||
} | ||
else { | ||
model.findOne(query).sort(sort).select(selection).exec(queryCallback); | ||
} | ||
}; | ||
// console.log('loadModel cached',cached); | ||
if (CoreUtilities.isValidObjectID(docid)) { | ||
@@ -361,7 +398,20 @@ query = { | ||
if (population) { | ||
model.findOne(query).sort(sort).select(selection).populate(population).exec(callback); | ||
if(useCacheTest && global.CoreCache){ | ||
cachedData = global.CoreCache.DataCache.get( | ||
{ | ||
key:cachekey | ||
}, | ||
function(err,cachedDataDoc){ | ||
if(cachedDataDoc){ | ||
logger.silly('X-Periodic-Data-Cache','hit',cachekey); | ||
callback(null,cachedDataDoc); | ||
} | ||
else{ | ||
logger.silly('X-Periodic-Data-Cache','miss',cachekey); | ||
getDataQuery(); | ||
} | ||
}); | ||
} | ||
else { | ||
model.findOne(query).sort(sort).select(selection).exec(callback); | ||
else{ | ||
getDataQuery(); | ||
} | ||
@@ -383,13 +433,63 @@ }; | ||
callback = options.callback, | ||
population = options.population; | ||
population = options.population, | ||
cached = (typeof options.cached === 'boolean' && options.cached===true) && true, | ||
useCacheTest = (useCache && cached), | ||
cachetype = (global.CoreCache)? global.CoreCache.DataCache.type : null, | ||
cachekey, | ||
cachedData, | ||
cachekeyexpirename = model.modelName.toLowerCase()+'_doc_cache_expires', | ||
queryDocsCallback = function(err,documents){ | ||
// console.log('cachekeyexpirename',cachekeyexpirename,global.CoreCache.options[cachekeyexpirename]); | ||
if(useCacheTest && global.CoreCache && documents){ | ||
global.CoreCache.DataCache.set({ | ||
key:cachekey, | ||
val:documents, | ||
expires:global.CoreCache.options[cachekeyexpirename] | ||
},function(err,expires){ | ||
if(err){ | ||
logger.error(err); | ||
} | ||
logger.silly('cached',cachekey,'expires',expires); | ||
}); | ||
callback(err,documents); | ||
} | ||
else{ | ||
callback(err,documents); | ||
} | ||
}, | ||
getDocsDataQuery = function(){ | ||
if (population) { | ||
model.find(query).sort(sort).select(selection).limit(limit).skip(offset).populate(population).exec(queryDocsCallback); | ||
} | ||
else { | ||
model.find(query).sort(sort).select(selection).limit(limit).skip(offset).exec(queryDocsCallback); | ||
} | ||
}; | ||
sort = (sort) ? sort : '-createdat'; | ||
offset = (offset) ? offset : 0; | ||
limit = (limit) ? limit : 500; | ||
if(global.CoreCache){ | ||
cachekey = global.CoreCache.generateKeyFromRequestUrl(cachetype,model.modelName+'+'+JSON.stringify(query)+'+'+sort+'+'+offset+'+'+selection+'+'+limit+'+'+population); | ||
} | ||
if (population) { | ||
model.find(query).sort(sort).select(selection).limit(limit).skip(offset).populate(population).exec(callback); | ||
if(useCacheTest && global.CoreCache){ | ||
cachedData = global.CoreCache.DataCache.get( | ||
{ | ||
key:cachekey | ||
}, | ||
function(err,cachedDataDoc){ | ||
if(cachedDataDoc){ | ||
logger.silly('X-Periodic-Data-Cache','hit',cachekey); | ||
callback(null,cachedDataDoc); | ||
} | ||
else{ | ||
logger.silly('X-Periodic-Data-Cache','miss',cachekey); | ||
getDocsDataQuery(); | ||
} | ||
}); | ||
} | ||
else { | ||
model.find(query).sort(sort).select(selection).limit(limit).skip(offset).exec(callback); | ||
else{ | ||
getDocsDataQuery(); | ||
} | ||
@@ -411,3 +511,5 @@ }; | ||
appendid = options.appendid, | ||
responseData = {}; | ||
responseData = {}, | ||
cached = (typeof options.cached === 'boolean' && options.cached===true) && true, | ||
useCacheTest = (useCache && cached); | ||
@@ -450,2 +552,6 @@ model.create(newdoc, function (err, saveddoc) { | ||
} | ||
if(useCacheTest){ | ||
clearModelDocCache({model:model,doc:saveddoc}); | ||
} | ||
} | ||
@@ -508,2 +614,86 @@ }.bind(this)); | ||
var clearModelDocCache = function(options){ | ||
var cachetype, | ||
// req= options.req, | ||
doc = options.doc, | ||
model = options.model, | ||
datacachetype = global.CoreCache.DataCache.type, | ||
datacachekey_byid, | ||
datacachekey_byname, | ||
asyncDeleteFunctions=[], | ||
deleteKeysArray=[], | ||
deleteViewKeyFunction = function(delkey){ | ||
return function(cb) { | ||
global.CoreCache.ViewCache.del({key:delkey},cb); | ||
}; | ||
}, | ||
getKeyURLFromCacheModelOptions = function(modelroute,id){ | ||
var delkey = '/'+path.join(modelroute.replace(':id',id)); | ||
return global.CoreCache.generateKeyFromRequestUrl(cachetype,delkey); | ||
}, | ||
deleteDataCache = function(){ | ||
if(doc.id){ | ||
datacachekey_byid = global.CoreCache.generateKeyFromRequestUrl(datacachetype,model.modelName+'+'+doc.id); | ||
global.CoreCache.DataCache.del({key:datacachekey_byid},function(err,status){ | ||
logger.silly('deleted memory cache',datacachekey_byid,status); | ||
}); | ||
} | ||
if(doc.name){ | ||
datacachekey_byname = global.CoreCache.generateKeyFromRequestUrl(datacachetype,model.modelName+'+'+doc.name); | ||
global.CoreCache.DataCache.del({key:datacachekey_byname},function(err,status){ | ||
logger.silly('deleted memory cache',datacachekey_byname,status); | ||
}); | ||
} | ||
}, | ||
deleteViewCache = function(options){ | ||
// console.log('deleteViewCache options',options); | ||
var doccache = options.doccache, | ||
doclistcache = options.doclistcache, | ||
docid = options.docid, | ||
docname = options.docname;//, | ||
// reqdoc = options.reqdoc; | ||
if(doccache && doccache.length >0 /* && reqdoc */){ | ||
for(var gcid in doccache){ | ||
if(docname){ | ||
deleteKeysArray.push(getKeyURLFromCacheModelOptions(doccache[gcid],docname)); | ||
} | ||
deleteKeysArray.push(getKeyURLFromCacheModelOptions(doccache[gcid],docid)); | ||
} | ||
} | ||
if(doclistcache && doclistcache.length >0 && docname /* && reqdoc */){ | ||
for(var gcil in doclistcache){ | ||
deleteKeysArray.push(getKeyURLFromCacheModelOptions(doclistcache[gcil],docname)); | ||
} | ||
} | ||
}; | ||
cachetype = global.CoreCache.ViewCache.type; | ||
deleteDataCache(); | ||
// if(req && req.controllerData){ | ||
// console.log('req.controllerData',req.controllerData); | ||
deleteViewCache({ | ||
doccache : global.CoreCache.options[model.modelName.toLowerCase()+'_doc_cache'], | ||
doclistcache : global.CoreCache.options[model.modelName.toLowerCase()+'_list_cache'], | ||
docid : doc.id, | ||
docname : doc.name//, | ||
// reqdoc : req.controllerData[model.modelName.toLowerCase()] | ||
}); | ||
// } | ||
if(deleteKeysArray.length>0){ | ||
for(var y in deleteKeysArray){ | ||
asyncDeleteFunctions.push(deleteViewKeyFunction(deleteKeysArray[y])); | ||
} | ||
async.series(asyncDeleteFunctions,function(err){ | ||
if(err){ | ||
logger.error(err); | ||
} | ||
logger.silly('cleared cache for ',deleteKeysArray); | ||
}); | ||
} | ||
// console.log('deleteKeysArray',deleteKeysArray); | ||
}; | ||
/** | ||
@@ -525,3 +715,5 @@ * short hand mongoose update document query | ||
responseData = {}, | ||
updateOperation; | ||
updateOperation, | ||
cached = (typeof options.cached === 'boolean' && options.cached===true) && true, | ||
useCacheTest = (useCache && global.CoreCache && cached && req.headers.periodicCache!=='no-periodic-cache'); | ||
@@ -581,2 +773,7 @@ if (options.removeFromArray) { | ||
} | ||
if(useCacheTest){ | ||
updatedoc.id = id; | ||
clearModelDocCache({model:model,doc:updatedoc,req:req}); | ||
} | ||
} | ||
@@ -587,3 +784,3 @@ }); | ||
model.findByIdAndUpdate(id, updateOperation, function (err, saveddoc) { | ||
console.log('tried to save', updateOperation); | ||
// console.log('tried to save', updateOperation); | ||
if (err) { | ||
@@ -640,2 +837,7 @@ if (callback) { | ||
} | ||
//refresh cache | ||
if(useCacheTest){ | ||
updatedoc.id = id; | ||
clearModelDocCache({model:model,doc:updatedoc,req:req}); | ||
} | ||
//save revision | ||
@@ -696,3 +898,5 @@ if (options.saverevision && originalrevision) { | ||
// res = options.res, | ||
callback = options.callback; | ||
callback = options.callback, | ||
cached = (typeof options.cached === 'boolean' && options.cached===true) && true, | ||
useCacheTest = (useCache && cached && global.CoreCache); | ||
@@ -702,4 +906,14 @@ model.remove({ | ||
}, callback); | ||
if(useCacheTest){ | ||
// updatedoc.id = id; | ||
clearModelDocCache({ | ||
model:model, | ||
doc:{ | ||
id:deleteid | ||
} | ||
}); | ||
} | ||
}; | ||
module.exports = Controller; |
{ | ||
"name": "periodicjs.core.controller", | ||
"description": "Customizable CMS platform", | ||
"version": "2.9.5", | ||
"version": "3.0.0", | ||
"main": "index.js", | ||
@@ -23,17 +23,16 @@ "engines": { | ||
"async": "0.9.0", | ||
"fs-extra": "0.9.1", | ||
"semver": "2.3.0", | ||
"periodicjs.core.utilities": "~1.90.10" | ||
"fs-extra": "0.12.0", | ||
"periodicjs.core.utilities": "~3.0.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "1.9.1", | ||
"chai": "1.10.0", | ||
"grunt": ">=0.4.5", | ||
"grunt-contrib-jshint": ">=0.4.3", | ||
"grunt-contrib-watch": ">=0.4.1", | ||
"grunt-contrib-jshint": ">=0.10.0", | ||
"grunt-contrib-watch": ">=0.6.1", | ||
"grunt-jsbeautifier": ">=0.2.7", | ||
"grunt-jsdoc": ">=0.5.6", | ||
"jsdoc": "3.3.0-alpha5", | ||
"grunt-jsdoc": ">=0.5.7", | ||
"jsdoc": "3.3.0-alpha10", | ||
"grunt-newer": ">=0.7.0", | ||
"grunt-simple-mocha": ">=0.4.0", | ||
"mocha": "1.9.0" | ||
"mocha": "2.0.1" | ||
}, | ||
@@ -62,5 +61,5 @@ "keywords": [ | ||
"homepage": "https://github.com/typesettin/periodicjs.core.controllerhelper", | ||
"_id": "periodicjs.core.controller@2.0.3", | ||
"_shasum": "1c6e20aac7d24724e247e52b941eb8db4c314ad7", | ||
"_from": "periodicjs.core.controller@2.0.3", | ||
"_id": "periodicjs.core.controller@2.9.5", | ||
"_shasum": "9ced54a6098dae93f226e27ea677cf43350d27f1", | ||
"_from": "periodicjs.core.controller@2.9.5", | ||
"_npmVersion": "2.0.0-beta.0", | ||
@@ -82,7 +81,7 @@ "_npmUser": { | ||
"dist": { | ||
"shasum": "1c6e20aac7d24724e247e52b941eb8db4c314ad7", | ||
"tarball": "http://registry.npmjs.org/periodicjs.core.controller/-/periodicjs.core.controller-2.0.3.tgz" | ||
"shasum": "9ced54a6098dae93f226e27ea677cf43350d27f1", | ||
"tarball": "http://registry.npmjs.org/periodicjs.core.controller/-/periodicjs.core.controller-2.9.5.tgz" | ||
}, | ||
"directories": {}, | ||
"_resolved": "https://registry.npmjs.org/periodicjs.core.controller/-/periodicjs.core.controller-2.0.3.tgz" | ||
"_resolved": "https://registry.npmjs.org/periodicjs.core.controller/-/periodicjs.core.controller-2.9.5.tgz" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129280
3
1495
+ Addedfs-extra@0.12.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@2.4.0(transitive)
+ Addedncp@0.6.0(transitive)
+ Addedperiodicjs.core.utilities@3.0.0(transitive)
- Removedsemver@2.3.0
- Removedfs-extra@0.9.1(transitive)
- Removedjsonfile@1.1.1(transitive)
- Removedncp@0.5.1(transitive)
- Removedperiodicjs.core.utilities@1.90.10(transitive)
- Removedsemver@2.3.0(transitive)
Updatedfs-extra@0.12.0