Last active
December 3, 2018 17:31
-
-
Save derms/c25f16964591e397d600471663751b37 to your computer and use it in GitHub Desktop.
mlRequireCache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cache = require("/lib/mlRequireCache.sjs") | |
/* | |
* Create Content Plugin | |
* | |
* @param id - the identifier returned by the collector | |
* @param options - an object containing options. Options are sent from Java | |
* | |
* @return - your content | |
*/ | |
function createContent(id, options) { | |
// javascript lib to cache | |
const mappUtils = cache.get("/lib/mappingUtils.sjs") | |
//log the cache stats to see statistics | |
console.log(JSON.stringify(cache.stats, null, 2)) | |
return extractInstance(source); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//attached to global object | |
if(!this.mlRequireCache) { | |
const mlRequireCache = { | |
'cache':{}, | |
'stats':{ | |
'cache-id': sem.uuidString(), | |
'hits':{'total':0}, | |
'misses':{'total':0} | |
} | |
} | |
mlRequireCache.get = function(requireModuleURI) { | |
if (!mlRequireCache.cache[requireModuleURI]) { | |
mlRequireCache.cache[requireModuleURI] = require(requireModuleURI) | |
mlRequireCache.stats.misses.total++ | |
mlRequireCache.stats.hits[requireModuleURI] =0 | |
} else { | |
mlRequireCache.stats.hits[requireModuleURI]++ | |
mlRequireCache.stats.hits.total++ | |
} | |
return mlRequireCache.cache[requireModuleURI] | |
} | |
this.mlRequireCache = mlRequireCache | |
} | |
module.exports.get = this.mlRequireCache.get | |
module.exports.stats = this.mlRequireCache.stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment