title: What's the best way to configure LRU cache using open-resty lua scripts in nginx proxy
body: <p>I am using openresty lua (<a href="https://github.com/openresty" rel="nofollow noreferrer">https://github.com/openresty</a>) to configure our nginx proxy.  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:</p>
  
  <pre><code>  #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;
    }
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>