LUA Example? How would I do this?

62 views
Skip to first unread message

marc perkel

unread,
Jan 10, 2016, 11:30:53 AM1/10/16
to Redis DB
Here's what I would like to do if Redis has the feature using LUA to do it.

I create or update a hash as follows:

HINCRBY myhash count 1

I'd like to have a background process with would set "myhash lastaccess" to the current time. (seconds in eopch).

Is there some trick in Redis/LUA that I'm missing that will do something like that?

marc perkel

unread,
Jan 10, 2016, 11:39:51 AM1/10/16
to Redis DB
Another example.

I want to decrement the count by 1 and if the count reaches 0 then delete the key.

I just cant seem to wrap my brain around how this is done. Just need a simple example to get started.

Itamar Haber

unread,
Jan 10, 2016, 12:26:15 PM1/10/16
to redi...@googlegroups.com

There's only one way (currently) to invoke Lua scripts in Redis, and that's by explicitly rubbing them using EVAL (or EVALSHA) - no tricks nor trigger-like/time-based manners. This means that your first example is solvable only with a client application that periodically updates the relevant key.

As for your second example, Lua can definitely help with that. Instead of directly calling INCRBY to do the decrement, call a script that wraps it and the logic (if + DEL). That script would probably look something like:

    if redis.call('INCRBY', KEYS[1], -1) == 0) then redis.call('DEL', KEYS[1]) end

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Itamar Haber

unread,
Jan 10, 2016, 5:21:52 PM1/10/16
to redi...@googlegroups.com

Edits:
^rubbing^running
Extra unneeded closing parenthesis in snippet, should be:

        if redis.call('INCRBY', KEYS[1], -1) == 0 then redis.call('DEL', KEYS[1]) end

Reply all
Reply to author
Forward
0 new messages