Topsy Lua in Redis

Source: Internet
Author: User
Tags eval lua redis sha1

First, Introduction

Redis has been learning for a while, and the basics are all fine. Starting today to write about some of the things that are related to Redis and Lua scripting, the Lua script is a good thing that can be run on any platform or embedded in most languages to extend its functionality. The Lua script is written in C, is very small, runs fast, and executes every time as an atomic transaction, where we can do a lot of things. Due to a lot of space, once can not outline all, this series may be written in a number of articles, well, today we get to the point.

ii. introduction of Lua

Lua is a small scripting language. A research group in the Catholic University of Rio de Janeiro, Brazil (Pontifical Catholic University of Rio de Janeiro), by Roberto Ierusalimschy, Waldemar Celes and Luiz Henr Ique de Figueiredo was formed and developed in 1993. It is designed to be embedded in the application, providing flexible extension and customization capabilities for the application. LUA is written in standard C and can be compiled and run on almost all operating systems and platforms. LUA does not provide a powerful library, which is determined by its positioning. So Lua is not suitable as a language for developing standalone applications. Lua has a simultaneous JIT project that provides instant compilation capabilities on a specific platform.

Lua scripts can be easily called by C/C + + code, or they can be used in turn to C + + functions, which makes LUA widely available in applications. Not only as an extension script, but also as a normal configuration file, instead of the Xml,ini file format, and easier to understand and maintain. LUA is written in standard C, and the code is simple and beautiful, and can be compiled and run on almost all operating systems and platforms. A complete LUA interpreter but 200k, the speed of Lua is the fastest in all of the scripting engines available. It all determines that Lua is the best choice for embedded scripting.

Iii. Benefits of using LUA scripting

1. Reduce network overhead: Multiple requests can be sent through a script one at a time, reducing network latency and number of requests.

2, atomic operation: Redis will be the entire script as a whole, the middle will not be inserted by other commands. As a result, there is no need to worry about race conditions when writing a script, without using transactions.

3, code reuse: The client sends the footsteps will permanently exist in Redis, so that other clients can reuse this script to complete the same logic.

4, Speed: See the performance comparison with other languages, there is a JIT compiler can significantly improve the performance of most tasks, for those who are still dissatisfied with the performance, you can use the key parts of C implementation, and then integrate with it, you can also enjoy other benefits.

5, can be ported: As long as the platform with the ANSI C compiler can be compiled, you can see it can be run on almost all platforms: from Windows to Linux, the same Mac platform is no problem, to mobile platforms, game consoles, and even the browser can be perfectly used (translated into Ja Vascript).

6, the source is small: 20000 line C code, can be compiled into 182K executable file, load fast, run fast.

Iv. integration of Redis and Lua

1. Call the Lua script syntax:
$ redis-cli--eval Path/to/redis.lua keys[1] keys[2], argv[1] argv[2] ...

--eval, tell Redis-cli to read and run the following LUA script
Path/to/redis.lua, the location of the Lua script
KEYS[1] keys[2], is the key to manipulate, you can specify multiple, in the Lua script through keys[1], keys[2] Get
ARGV[1] argv[2], parameters, obtained in Lua script through argv[1], argv[2].

Note: Keys and argv in the middle of ', ' on both sides of the space, cannot be omitted.

Redis supports most LUA standard libraries

base provides some basic functions
string provides functions for string manipulation
table
math provides mathematical calculation function
debug provides functions for debugging


2. Call the Redis command in the script
Redis commands can be called in scripts using the Redis.call function

Redis.call (' Set ', ' foo ', ' Bar ')
Local Value=redis.call (' Get ', ' foo ') The value of--value is bar

The return value of the Redis.call function is the result of the Redis command's execution

The return value of the Redis command has 5 types, and the Redis.call function converts the 5 types of replies into the corresponding LUA data types, with the following rules (the null result is special and it corresponds to Lua's false)

Redis return value types and LUA data type conversion rules

Redis return value type LUA data Types
Integer reply Number Type
string reply String type
Multi-line String reply Table type (array form)
Status reply Table type (only one OK field stores state information)
Error reply Table type (only one ERR field stores error information)


Redis also provides the Redis.pcall function, the same function as Redis.call, the only difference is that when the command execution error, Redis.pcall will log the error and continue to execute, and Redis.call will return the error directly, will not continue execution. The return statement can be used in the script to return the value to the client, and nil is returned by default if no return statement is executed

LUA data types and Redis return value type conversion rules

lua data type redis return value type
table type (array form)
table type (only one OK field stores state information)
table type (only one ERR field stores error messages) error reply


3. Script-related commands
eval Syntax: eval script Numkeys key [key ...] arg [arg ...]

The data is passed to the script through both the key and ARG parameters, whose values are accessed in the script using the keys and the global variables of the argv two table type respectively.

script: is a LUA script

Numkeys: Indicates that there are several keys, respectively, keys[1],keys[2] ..., if there is a value, starting with the first numkeys+1 is the parameter value, Argv[1],argv[2] ...

Note: The eval command numkeys all the parameters following it into the script with the global variables of the keys and argv two table types, according to the parameter. This parameter cannot be omitted when the script does not require any parameters (set to 0)

       192.168.127.128:6379>eval "Return Redis.call (' Set ', Keys[1],argv[1])" 1 name Liulei       OK       192.168.127.128:6379>get name       "Liulei"


4. Evalsha command
In the case of long scripts, it takes more bandwidth to pass the entire script to redis each time the script is called. To solve this problem, Redis provides the Evalsha command, which allows the developer to execute the script through the SHA1 Digest of the script content, which, like Eval, simply replaces the script content with the SHA1 summary of the script content.

Redis evaluates the SHA1 summary of the script and records it in the script cache when executing the eval command, and Redis looks for the corresponding script content from the script cache based on the summary provided, and then executes the script if found, or returns an error: "NOSCRIPT No Matching script. Use EVAL. "

The general flow of using the Evalsha command in your program is as follows.

1), first calculate the SHA1 summary of the script, and execute the script using the Evalsha command.

2), get the return value, and re-execute the script with the eval command if the "NOSCRIPT" error is returned.

While this process is a little cumbersome, it is fortunate that REDIS clients in many programming languages will do this in lieu of developers. When the eval command is executed, the Evalsha command is attempted first and the eval command is executed if it fails.

Scriptload "Lua-script" adds a script to the cache, but does not execute, returns: SHA1 summary of the script

Script EXISTS LUA-SCRIPT-SHA1 to determine if scripts have been cached

5. SCRIPT FLUSH (the command is case insensitive)
Emptying the script cache, Redis adds the script's SHA1 summary to the script cache for permanent retention and is not deleted, but you can use the Script Flush command to clear all script caches manually.

       192.168.127.128:6379>script flush       OK       192.168.127.128:6379>script flush       OK


6. SCRIPT KILL (the command is case insensitive)
Forces the execution of the current script to terminate. However, if the current steps are performed to write to the Redis data, the script Kill command does not terminate the run of the scripts to prevent the script from executing only part of it. All commands in the script are either executed or not executed.

       192.168.127.128:6379>script kill      (Error) notbusy No scripts in execution right now      192.168.127.128:6379> Script KILL      (Error) notbusy No scripts in execution right       now//This is not currently a script executing, so prompt for this error


7, Lua-time-limit (in redis.conf configuration file)

To prevent a script from running too long to provide services (such as falling into a dead loop), Redis provides the maximum run time for the Lua-time-limit parameter limit script, which defaults to 5 seconds. When the script runs beyond this limit, Redis will start accepting other commands but will not execute (to ensure the atomicity of the script, because the script is not terminated at this time), instead it returns a "BUSY" error.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.