leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.14k stars 247 forks source link

cache.lua:71: attempt to index local 'dict' (a nil value) #724

Open alexz006 opened 3 years ago

alexz006 commented 3 years ago

OpenResty 1.19.3.1, Lapis 1.8.3.

New project:

lapis new --lua
wrote   nginx.conf
wrote   mime.types
wrote   app.lua
wrote   models.lua

app.lua:

local lapis = require("lapis")
local cached = require("lapis.cache").cached

local app = lapis.Application()

app:match("/microcached", cached({
  exptime = 1,
  function(self)
    return "hello world!"
  end
}))

return app

GET /microcached?foo

Error
...cal/openresty/luajit/share/lua/5.1/lapis/nginx/cache.lua:71: attempt to index local 'dict' (a nil value)
Traceback
stack traceback:
    ...cal/openresty/luajit/share/lua/5.1/lapis/nginx/cache.lua:71: in function 'handler'
    ...cal/openresty/luajit/share/lua/5.1/lapis/application.lua:146: in function 'resolve'
    ...cal/openresty/luajit/share/lua/5.1/lapis/application.lua:183: in function <...cal/openresty/luajit/share/lua/5.1/lapis/application.lua:181>
    [C]: in function 'xpcall'
    ...cal/openresty/luajit/share/lua/5.1/lapis/application.lua:189: in function 'dispatch'
    /usr/local/openresty/luajit/share/lua/5.1/lapis/nginx.lua:231: in function 'serve'
    content_by_lua(nginx.conf.compiled:22):2: in main chunk

You need nginx.conf add the line:

http {
  lua_shared_dict page_cache 10m;
  ...
}

and then everything works.

leafo commented 3 years ago

This sounds like an update for docs might be needed? And perhaps a better error message?

alexz006 commented 3 years ago

This sounds like an update for docs might be needed? And perhaps a better error message?

I think that when creating a new project (lapis new), the line lua_shared_dict page_cache 10m; was added by default in the nginx.conf file;