diff --git a/process/DataCaching_lib/DataCaching_lib.aod b/process/DataCaching_lib/DataCaching_lib.aod new file mode 100644 index 0000000000000000000000000000000000000000..732c9784be4434a3a4776c4865536eaeb260e280 --- /dev/null +++ b/process/DataCaching_lib/DataCaching_lib.aod @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.2.1" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.2.1"> + <name>DataCaching_lib</name> + <majorModelMode>DISTRIBUTED</majorModelMode> + <process>%aditoprj%/process/DataCaching_lib/process.js</process> + <variants> + <element>LIBRARY</element> + </variants> +</process> diff --git a/process/DataCaching_lib/process.js b/process/DataCaching_lib/process.js new file mode 100644 index 0000000000000000000000000000000000000000..6105fbd5e00b826c2d332de2f92859d43f65bb42 --- /dev/null +++ b/process/DataCaching_lib/process.js @@ -0,0 +1,90 @@ +import("system.vars"); + + +function CachedData(pIdentifiyingName, pKeepPerLanguage) +{ + this.identifyingName = pIdentifiyingName; + this.keepPerLanguage = pKeepPerLanguage; + + this.runningOnServer = vars.getString("$sys.isserver") == "true"; + if (this.runningOnServer) + this.locale = vars.get("$sys.serverlocale"); + else + this.locale = (this.keepPerLanguage ? vars.get("$sys.clientlocale") : "_anyLanguage_"); +} + +CachedData.make = function(pIdentifiyingName, pKeepPerLanguage, pDataCallbackFunction) +{ + return (new CachedData(pIdentifiyingName, pKeepPerLanguage)).load(pDataCallbackFunction); +} + +CachedData.prototype.load = function(pDataCallbackFunction) +{ + //currently it's not possible to cache the data within the serer-context, so inestead the Data-function is called everytime + if (this.runningOnServer) + return pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale); + else + { + var varname = this.getVariableName(); + var data; + if (vars.exists(varname)) + { + data = vars.get(varname); + if (data != null) + return data; + } + + data = pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale); + vars.set(varname, data); + this._register(); + return data; + } +}; + +CachedData.prototype.unload = function() +{ + var varname = this.getVariableName(); + vars.set(varname, null); + this._unregister(); +} + +CachedData.prototype.getVariableName = function() +{ + var PREFIX = "$global.CachedData."; + var res = PREFIX + this.identifyingName + "." + this.locale; + return res; +} + +CachedData.getRegistryName = function() +{ + return "$global.CachedDataRegistry"; +} + +CachedData.getRegistry = function() +{ + var registryVarname = CachedData.getRegistryName(); + if (vars.exists(registryVarname)) + return vars.get(registryVarname); + else + return {}; +} + +CachedData.prototype._register = function() +{ + var registry = CachedData.getRegistry(); + if (registry[this.getVariableName()] != undefined) + throw new Error("Already registered. cannot register the same object twice");//TODO: message + else + registry[this.getVariableName()] = {keepPerLanguage: this.keepPerLanguage}; + vars.set(CachedData.getRegistryName(), registry); +}; + +CachedData.prototype._unregister = function() +{ + var registry = CachedData.getRegistry(); + if (registry[this.getVariableName()] == undefined) + throw new Error("Item not found. cannot unregister");//TODO: message + else + delete registry[this.getVariableName()]; + vars.set(CachedData.getRegistryName(), registry); +};