Skip to content
Snippets Groups Projects
Commit f41cc62d authored by Johannes Goderbauer's avatar Johannes Goderbauer
Browse files

implemented basic client-global caching mechanisms (2)

parent e80d1615
No related branches found
No related tags found
No related merge requests found
import("system.vars");
function CachedData(pIdentifiyingName, pKeepPerLanguage)
function CachedData(pIdentifiyingName, pKeepPerLanguage, pLocaleOverride)
{
this.identifyingName = pIdentifiyingName;
this.keepPerLanguage = pKeepPerLanguage;
this.runningOnServer = vars.getString("$sys.isserver") == "true";
if (this.runningOnServer)
if (pLocaleOverride)
this.locale = pLocaleOverride;
else if (this.runningOnServer)
this.locale = vars.get("$sys.serverlocale");
else
this.locale = (this.keepPerLanguage ? vars.get("$sys.clientlocale") : "_anyLanguage_");
......@@ -15,12 +17,12 @@ function CachedData(pIdentifiyingName, pKeepPerLanguage)
CachedData.make = function(pIdentifiyingName, pKeepPerLanguage, pDataCallbackFunction)
{
return (new CachedData(pIdentifiyingName, pKeepPerLanguage)).load(pDataCallbackFunction);
return (new CachedData(pIdentifiyingName, pKeepPerLanguage, null)).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
//currently it's not possible to cache the data within the serer-context, so instead the Data-function is called everytime
if (this.runningOnServer)
return pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale);
else
......@@ -35,6 +37,8 @@ CachedData.prototype.load = function(pDataCallbackFunction)
}
data = pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale);
if (data == null)
throw new Error("use unload instead");
vars.set(varname, data);
this._register();
return data;
......@@ -48,17 +52,33 @@ CachedData.prototype.unload = function()
this._unregister();
}
CachedData.prototype.getVariableIdentifier = function()
{
//keep data per user in a global var to assure that translations and grants are correctly applied
var PREFIX = "$global.CachedData.";
var res = PREFIX + this.identifyingName
return res;
};
CachedData.prototype.getVariableName = function()
{
var PREFIX = "$global.CachedData.";
var res = PREFIX + this.identifyingName + "." + this.locale;
var res = this.getVariableIdentifier() + "." + this.locale;
return res;
};
//functions for registry of variables; reserver for later functionality
CachedData.prototype._register = function()
{
var reg = CachedData.getRegistry();
reg.push(this.getVariableName());
vars.set(CachedData.getRegistryName(), reg);
}
//CachedData.prototype._unregister = function(){}
CachedData.getRegistryName = function()
{
return "$global.CachedDataRegistry";
}
};
CachedData.getRegistry = function()
{
......@@ -66,25 +86,29 @@ CachedData.getRegistry = function()
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);
return [];
};
//
//CachedData.prototype._register = function()
//{
// var registry = CachedData.getRegistry();
//
// if (registry[this.getVariableIdentifier()])
//
// 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 varname = this.getVariableName();
// var registry = CachedData.getRegistry();
// if (registry[varname] == undefined)
// throw new Error("Item not found. cannot unregister");//TODO: message
// else
// delete registry[varname];
// vars.set(CachedData.getRegistryName(), registry);
//};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment