How to update a Redis value without affecting the remaining TTL

Very self explanatory title, this beauty it's a Lua script running inside Redis interpreter, it will basically grap the ttl from the passed key if exist (ttl > 0) and use it which means that we are overriding the key or if not will just setex the key with the passed data and ttl.

red:eval("local ttl ='ttl', ARGV[1]) if ttl > 0 then return'SETEX', ARGV[1], ttl, ARGV[2]) else return'SETEX', ARGV[1], ARGV[3], ARGV[2]) end", 0, key, data, ttl)  

Since version 2.6.0 Redis have embebed on it's intrepreter Lua, so we can write Lua scripts that could be interpreted by Redis as shown above



Some usefull links: