lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Right on. This clears my confusion. I initially though that each Nginx worker has its own Lua VM, but it's more like their own Lua thread on same Lua VM (on the same http {}). I confirmed this on my machine by running multiple workers, and yes they did share the same template.cache table (so templates got parsed, and loaded only once).

Well, this is even better as the workers may share template cache. And yes, init_by_lua could be used to warmup the cache (or like I said, you may let the cache warm as the templates get used). Precompiling is a little bit different animal in lua-resty-templates. By precompiling you just get string.dumps from compiled and parsed templates that are loaded with Lua's load (returning a function). So you can think that precompiled templates are directly loaded with load, and they do not need to be parsed. So they are kinda binary form of the parsed and loaded template functions. Its more like a production thing, as you don't need to deliver your templates as text. Cache always stores these functions directly, so considering cache, and cache warmed templates, there aren't really difference whether the originals were text or string.dumped binaries (other than cache warming process runs a little bit faster as there is no need to parse anything).

I modified template.compile to return second return valua that can be used to determine if the template was served from the cache. I also tried to implement set $template_caching off; support for Nginx but I found out that accessing it caused problems with init_by_lua as it is not supported on that context. Alternatively I could read the value on each request, but I'm not sure that it's worth it. You may after all write init_by_lua 'template = require "resty.template"; template.caching(false)'; And then just in content_by_lua 'template.render("view.html", { title = "Test" })';


10.3.2014 0.45 kirjoitti "Yichun Zhang (agentzh)" <>:

On Sat, Mar 8, 2014 at 10:39 PM, Hao Wu wrote:
> Thanks for clarifying this, and this is what I meant. I looked up the
> ngx_lua module, it vaguely talked about how the lua state is
> maintained across the workers' process;

Just to be clear: in the ngx_lua module, the Lua VM is created in the
nginx master process before forking off the workers. So if you
(pre)compile your templates in init_by_lua, then you only need to
compile the templates once (in contrast, init_worker_by_lua runs upon
each worker process's startup, so you'll have to pre-compile the
templates for multiple times). Also, with init_by_lua, you can take
advantage of modern operating systems' Copy-On-Write (COW) feature to
further save memory :)

Best regards,