4

I'm using OpenResty with lua-resty; and obviously for each request the program has its own variables. To share simple strings or configurations across requests I currently use lua-shared-dict.

But, if I need to share and maintain a big variable (e.g.: a complex table made by the parsing of a large INI file) across requests (the variable is created every hour, for example, in order to improve performance), how can I do it?

(e.g.: another example, imagine to translate this in LUA: https://github.com/dangrossman/node-browscap/blob/master/browscap.js; how can I maintain the browser[] array across multiple OpenResty HTTP requests, without having to re-parse it for each request?)

Carmine Giangregorio
  • 943
  • 2
  • 14
  • 35

1 Answers1

5

how can I maintain the browser[] array across multiple OpenResty HTTP requests, without having to re-parse it for each request?

I assume you mean "across multiple OpenResty workers" or "across requests that may hit different workers" as all the requests that hit the same worker can access the same variables, but if so, you probably can't. Since you seem only need to read that browser[] value (as you are parsing a large INI file), you can try a hybrid approach:

  1. Store the result of parsing in a serialized form in one of the lua-shared-dict values (let's say iniFile).
  2. When the request gets in, check if the iniFile variable in that request is nil and if it is, then read iniFile value from lua-shared-dict, deserialize it and store as the value of the iniFile variable that is shared by all the code that is run by same worker.
  3. If you need to refresh it after 1h to keep it up-to-date, store the time when the value is retrieved from the dictionary, and add a check to #2 to re-retrieve when the time exceeds your limit.
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56