openresty / lua-resty-memcached

Lua memcached client driver for the ngx_lua based on the cosocket API
211 stars 63 forks source link

Getting key's token always failed by the gets method. #20

Closed vampire007 closed 6 years ago

vampire007 commented 6 years ago

Hey, I got a problem... Maybe it's stupid, but I don't know where to find help, so created this issue. Why do I always get nil value by the gets method? Here is the code which copied from your demo.

location = /hi {
    content_by_lua_block {
    local memcached = require "resty.memcached"

    local memc, err = memcached:new()
    if not memc then
    ngx.say("failed to instantiate memc: ", err)
        return
    end

    memc:set_timeout(1000)

    local ok, err = memc:connect("10.23.233.12", 8201)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    local value, flags, err = memc:get("dog")
    if not value then
        memc:set("dog", 1)
        ngx.say("failed to get dog ", err)
        return
    end

    -- get key's token
    local data, flags, token, err = memc:gets("dog")
    if not token then
        ngx.say("failed to get token")
        return
    end

   local ok, err = memc:cas("dog", data + 1, token)
    if not ok then
        ngx.say("failed to set dog ", err)
        return
    end

    local res, flags, err = memc:get("dog")
    if not res then
        ngx.say("failed to get dog ", err)
        return
    end

    ngx.say("dog: ", res)
    }
}
agentzh commented 6 years ago

@vampire007 I don't see you ever set the key dog in your example above. You are getting nil just because that key does not exist (unless other errors happen).

I added a set() call to your example, and it works fine on my side:

    location /t {
        content_by_lua_block {
            local memcached = require "resty.memcached"

            local memc, err = memcached:new()
            if not memc then
            ngx.say("failed to instantiate memc: ", err)
                return
            end

            memc:set_timeout(1000)

            local ok, err = memc:connect("127.0.0.1", 11211)
            if not ok then
                ngx.say("failed to connect: ", err)
                return
            end

            memc:set("dog", 32)

            local value, flags, err = memc:get("dog")
            if not value then
                memc:set("dog", 1)
                ngx.say("failed to get dog ", err)
                return
            end

            -- get key's token
            local data, flags, token, err = memc:gets("dog")
            if not token then
                ngx.say("failed to get token")
                return
            end

            ngx.say("token: ", token)

           local ok, err = memc:cas("dog", data + 1, token)
            if not ok then
                ngx.say("failed to set dog ", err)
                return
            end

            local res, flags, err = memc:get("dog")
            if not res then
                ngx.say("failed to get dog ", err)
                return
            end

            ngx.say("dog: ", res)
        }
    }

To test this /t:

$ curl localhost:8080/t
token: 704
dog: 33