Skip to content

Instantly share code, notes, and snippets.

@derms
Last active December 3, 2018 17:31
Show Gist options
  • Save derms/c25f16964591e397d600471663751b37 to your computer and use it in GitHub Desktop.
Save derms/c25f16964591e397d600471663751b37 to your computer and use it in GitHub Desktop.
mlRequireCache
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);
}
//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