Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current date and time in lua in redis

Tags:

redis

lua

How can I get current date / time in Lua embedded in Redis?

I need to have it in following format - YYYY-MM-DD, HH:MM:SS

Tried with os.date() but it does not recognize it.

like image 956
Nick Avatar asked Jul 15 '15 14:07

Nick


2 Answers

Redis' Lua sandbox has only a handful of libraries, and os isn't one of these.

You can call the Redis TIME from Lua like so:

local t = redis.call('TIME')

However, you'll need to find a way to convert the epoch to the desired format and also note that it will stop you script from performing any writes (as it is a non-deterministic command).

Update: as of Redis v3.2, there is a new replication mode for scripts that is effect-based (rather than code-based). When using this mode you can actually call all the random, non-deterministic commands. More information is at EVAL's documentation page

like image 165
Itamar Haber Avatar answered Oct 26 '22 05:10

Itamar Haber


This was already discussed in the comments, but the correct answer should have an answer:

The current time is non-deterministic i.e. it returns different values on repeated calls. This hurts replication. For this reason, the current time should be passed into your LUA script as a parameter.

like image 27
Chronial Avatar answered Oct 26 '22 06:10

Chronial