How Lua Code is Run in the Translator: Caching Variables
When an Iguana channel starts for the first time, all variables and functions are called. Afterwards, when the Translator is called again, only the main module is called. The following code is an example of how to cache configurations in the Translator and refresh the configurations after an expiry time.
local function getDateTime()
return json.parse{data=net.http.get{url='http://worldtimeapi.org/api/timezone/America/Argentina/Salta', live=true}}.datetime
end
local datetime = getDateTime()
local cache_time = os.ts.gmtime()
local cache_age = 60
iguana.logInfo('The following datetime was generated when the channel started for the first time:\n'..datetime)
function main()
-- All global/local variables and functions are called when the channel is started
-- Defined functions aren't called unless they are called
-- Generally as a rule of thumb, if it shows up on the annotations then the
-- code is run everytime the channel polls - if not then the code is called
-- once when the channel starts
iguana.logInfo('The following datetime is the current value of datetime:\n'..datetime..'\n\n'..
'Seconds since the datetime variable was assigned: '..os.ts.gmtime()-cache_time)
-- Refresh the datetime variable if older than cache_age
if (os.ts.gmtime()-cache_time) >= cache_age then
iguana.logInfo('Refreshing datetime variable...')
datetime = getDateTime()
cache_time = os.ts.gmtime()
end
end
Please import into a From Translator channel component, start the channel, and then observe the logs for that channel to see the workflow.
Any function can be used to read and define the configuration variables, and a common one we see is a JSON configuration file that is read and used to populate configuration variables.
These can be encrypted and decrypted by Iguana as well (Signing data with the crypto API, Filter API), and just placed on the production server so that credentials are not visible to any developers.