2016-05-13 86 views

回答

3

你可以用一个Lua脚本做到这一点:

local value = redis.call("GET", KEYS[1]) 
if (not value) then 
    redis.call("SET", KEYS[1], ARGV[1]) 
    return ARGV[1] 
end 
return value 

保存为script.lua,并调用它像这样:

$ redis-cli eval "$(cat script.lua") 1 myKey defaultValue