Osdate speed dating
Without to mention that like all the optimistic locking approaches does not work when there is too much contention. With scripting all this specific problems are solved in a general way without making the Redis server a mess with a big number of commands, and without trying to implement our "little language" that will later turn in an ill conceived real language.After all it is so simple to conditionally decrement server side, right? So welcome to our first example, decrementing a value only if the value is greater than a given value we pass as argument.Lua scripting is exposed to Redis as a single command called EVAL. It is followed by a variable number of arguments, prefixed by the number of keys that are in those arguments.So for instance if I call eval somescript 3 a b c d e f the arguments a, b, c are considered keys, all the rest of the arguments are considered as normal arguments.We already saw how returning an array from the Lua script generates a multi bulk reply, but here is the full list of returned types: It is as simple as calling Redis with the command as first argument followed by all the other arguments. Numbers are automatically converted into strings, as Redis commands accept only strings arguments from clients even when the actual meaning of the argument is a number.Now the really interesting part is how the redis() function return values to Lua: using exactly the reverse of the conversion table above.The above program produces the following output as you could expect.Another common request in the mailing list is to provide "NX" or "EX" versions of Redis commands, that is, commands that are executed only if the target key does not exists or exists.
With our current EVAL semantics every instance is the same, regardless of the fact it was just started or not, and regardless of its file.
The reason is, if you respect this semantic, the Eval command will be for Redis not different from any other command.
Redis only knows a few things about registered commands: the number of arguments, and what arguments are keys.
) is that is almost our only salvation from making Redis bloated.
For instance yesterday an user wrote in the Redis mailing list writing about how to conditionally decrement a counter, only if the current counter value is greater than zero.For example knowing about keys makes Redis Cluster able to forward your requests to the right node.The EVAL command is designed in order to be not different. If you want to do strange stuff, you can access all the key space from Lua scripts: we are free to shoot in our own foots :) The above example shows two important points: the first is that our arguments are correctly passed via KEYS and ARGS tables.But I actually had a lot of fun coding, and managed to attend a concert, to play at bowling, drink some good vodka, and watch the "Source Code" movie, so I'll call this a busy weekend :) So after some Lua C API crash course, roughly 400 lines of code, and 8 hours of work in two days, this is the result in the scripting branch at github, but most of the code is self contained in the scripting.c file if you want to understand how the implementation works (hint: it is really simple).