My goal is to store a C# object in a Redis Hash. One of the fields is a DateTime as Ticks (long aka 64bit int). I was hoping to use a Lua script to do a conditional update only if the ChangeEventTime is newer on the incoming arguments, something like:
local existingTime = redis.call('hget', KEYS[1], 'ChangeEventTime')
if existingTime then
if tonumber(existingTime) < tonumber(ARGV[1]) then
redis.call('hset', KEYS[1], 'ChangeEventTime', ARGV[1])
redis.call('hset', KEYS[1], 'Value', ARGV[2])
end
else
redis.call('hset', KEYS[1], 'ChangeEventTime', ARGV[1])
redis.call('hset', KEYS[1], 'Value', ARGV[2])
end
Forgive if my Lua is not right, I am brand new to Redis. But the issue seems to be that Redis only supports Lua 5.1 and that means 64-bit integers are not supported (that was added in Lua 5.3).
Is there any way around this limitation? I need sub-second accuracy on my datetime info (unix timestamp is not going to hack it) but I really would like to do this as an atomic transaction and not have to query the current value, do the compare in C# on the client and then do the update. Because of the potential for high speed changing of data, doing the conditional logic client-side could cause dirty writes.
Because our work queue cannot guarantee time ordered entries, I cannot simply always update the hash with the latest entry from that queue -- it may in fact be stale (Redis already has something newer). Some conditional comparison needs to take place to ensure the entry in Redis is the most recent DateTime.