lua-resty-redis icon indicating copy to clipboard operation
lua-resty-redis copied to clipboard

read the same redis key, sometimes not same

Open kg1990 opened this issue 8 years ago • 1 comments
trafficstars

when i use redis in lua, i got this problem here is the file structure

filename : init.lua

redis = require "resty.redis"
ssl = require "ngx.ssl"
cjson = require 'cjson'

filename: nginx.conf

init_by_lua_file /opt/nginx/conf.d/lua/init.lua;

server {

    lua_code_cache on;
    location ~ ^/lua_api/([-_a-zA-Z0-9/]+) {
        default_type "application/json"; 
        set $path $1;
        content_by_lua_file /opt/nginx/conf.d/lua/api/$path.lua;
     }
}

filename :regist_limit.lua

local json = require 'cjson';
local vip_red = redis:new()
vip_red:set_timeout(30000) -- 1 sec

vip_redis_host = 'ip address'
vip_redis_port = 'port'

function api_error(code, msg)
    ngx.say(json.encode({errcode = code,errmsg = msg}))
    return
end

local ok, err = vip_red:connect(vip_redis_host, vip_redis_port)
if not ok then
    api_error(50000, "connet redis fail in step 1")
end

local res, err = vip_red:get("regist_limit")
if not res then
    api_error(50001, "connet redis fail in step 2")
end
if res == ngx.null then
  res = 0
else
  res = tonumber(res)
end
ngx.say(json.encode({limit = res}))

the redis set regist_limit key by the value 1 but sometimes when i request the path /lua_api/regist_limit path i got different response

the first one { "limit": 1 }

the second one { "limit": 0 }

i can not figure out where comes wrong, the lua_code_cache?

kg1990 avatar Sep 04 '17 13:09 kg1990

@kg1990 You'll have to debug your own Lua code here and narrow down the problem. If you believe it's a bug in lua-resty-redis, please provide a minimal and standalone example that we can reproduce the problem easily on our side. Or you should provide the definite analyses. Otherwise we cannot help you. This library has been used in extremely heavy traffic in many companies' production environment for years. So I guess it should be some programming or configuration mistakes on your side without seeing real evidences.

agentzh avatar Sep 04 '17 21:09 agentzh