How use ngx.shared.Dict.flush_all()?

168 views
Skip to first unread message

Ivan Smirnov

unread,
Dec 12, 2016, 5:18:33 AM12/12/16
to openresty-en
    server {
        server_name ivan;
        listen 8080;
        location / {
            rewrite_by_lua_block{
                local data = ngx.shared.data
                ngx.say("data : ")
                for _,v in pairs(ngx.shared.data:get_keys()) do
                    ngx.say(v.." : "..ngx.shared.data:get(v))
                end
            }
        location /clear/ {
            rewrite_by_lua_block{
                local data = ngx.shared.data
                data.flush_all()
            }
        }
    }

after curl http://localhost:8086/clear/ i expect that all keys were expired, but in my nginx error log i see the error:
  2016/12/12 13:07:27 [error] 7198#0: *6 lua entry thread aborted: runtime error: rewrite_by_lua(common.conf:52):3: expecting 1 argument, but seen 0
stack traceback:
coroutine 0:
    [C]: in function 'flush_all'
    rewrite_by_lua(common.conf:52):3: in function <rewrite_by_lua(common.conf:52):1>, client: 127.0.0.1, server: ivan, request: "GET /clear/ HTTP/1.1", host: "localhost:8080"
in documentation flush_all use without arguments.


Itamar Gilad

unread,
Dec 12, 2016, 5:38:55 AM12/12/16
to openresty-en
When calling with the dot you are merely using the function from the instance table, but not passing the reference to it as the first parameter. You need to use lua's syntactic sugar for method calls with colon calls (change data.flush_all() to data:flush_all() ), or pass the instance explicitly (data.flush_all(data), but I think you'll agree that looks silly).

-Itamar

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,
Itamar Gilad
CTO & co-founder, Metapacket
Reply all
Reply to author
Forward
0 new messages