I have the stack overflow issue (https://stackoverflow.com/questions/51049811/whats-the-best-way-to-configure-lru-cache-using-open-resty-lua-scripts-in-nginx) open and am wondering how to use this cache. Apparently it can only be used within the context of the single path, which to me is not very useful, though in our particular case this would seem to fix. However, when I try to set the cache up, I see that it get's created on every time the path is called. When I try to isolate the initialization of the cache to an init_by_lua script any variable I try to use to access it loses scope. For those who do not have access to stack overflow I append the question here:
I have one main proxy.template that defines 3 locations, but want to use only one LRU cache for multiple process initialization calls (since they take so long and nothing else, that is why I am using a script). I want to define a variable that can be passed into each location, but am pretty sure I am not doing this correctly. I have:
#init_by_lua_file $lru_cache /etc/scripts/lua/process_cache.lua;
location /process {
access_by_lua_file /etc/scripts/lua/process_access.lua;
proxy_set_header Content-Type "application/json";
proxy_set_header Accept "application/json";
proxy_ssl_server_name on;
proxy_pass $target;
}
location /process/init {
set_by_lua_file $lru_cache /etc/scripts/lua/process_cache.lua;
add_header Access-Control-Expose-Headers set-cookie;
add_header Access-Control-Allow-Headers set-cookie;
access_by_lua_file /etc/scripts/lua/process_init.lua;
}
The process_cache creates the cache (one per proxy startup) and I would like it to be referenced by the process_init.lua and process_access.lua which do different things. For example, process_init is only called once for a UI initialization and establishes the specific cache entries, process_access checks to make sure the entry hasn't expired and if not uses it, otherwise creates a new entry, so that a long call to another server is not needed.
The above would require the lru_cache variable to be passed amongst the two locations. My latest attempts were in the area of trying to place process_cache.lua within the /process/init path, but then it just gets initialized each time, so starting with an empty cache each /process/init call is useless. Thoughts?