introduce

The Lua interpreter has been embedded in the server since version 2.6, allowing users to execute Lua scripts on the server side.

  • The script can directly execute the Redis command on the server, and the general data processing operations can be directly completed by using the Lua language or the function library provided by the Lua interpreter, without returning it to the client for processing.
  • All scripts are executed in the form of transactions, which are not interrupted by other work or cause any race conditions. Lua scripts can be used instead of transactions and optimistic locks.
  • All scripts are reusable, and the same operation can be repeated by invoking the script cache stored inside the server instead of resending the entire script, saving network resources as much as possible
The command
  • EVAL script numkeys key [key …] arg [arg …] Script: Lua script to be executed numkeys: number of database keys to be processed by the script. Key [key…] KEYS[1] fetch the first input key, KEYS[2] fetch the second input key, etc. : Specifies the parameters to be used by the script, which can be obtained by accessing the ARGV array. Explicitly specifying the keys used in the script is intended to coordinate with the Redis cluster checking of keys; otherwise, using the script in the cluster may cause errors

    Users can reuse the same script more easily by explicitly specifying the database keys and related parameters that the script will use, rather than hard-writing the database keys and parameters into the script

Run the Redis command in the Lua script
  • redis.call()

  • redis.pcall()

  • Example: Specify the DBSIZE command

    127.0.0.1:6379> dbsize (integer) 1 127.0.0.1:6379> eval "return redis. Call ('dbsize')" 0 (integer) 1Copy the code
  • Example: Run the GET command to GET the value of MSG and concatenate the value

    127.0.0.1:6379> eval "return 'MSG is:'.. redis.call('get', KEYS[1]) " 1 msg "msg is: hello"Copy the code
  • The difference between redis.call() and redis.pcall() When an error occurs in the script being executed, redis.call() returns the name of the error script and an error message from the EVAL command, whereas redis.pcall() only returns an error message from the EVAL command

Use EVALSHA to reduce network resource consumption
  • Any Lua script executed once by the EVAL command is stored in the server’s script cache. Users can use the EVALSHA command to specify the SHA1 value of the cached script and execute the script again without sending the script
    127.0.0.1:6379> eval "return 'hello world'" 0 "hello world" 127.0.0.1:6379> evalsha 5332031 c6b470dc5a0dd9b4bf2030dea6d65de91 0 "hello world" 127.0.0.1:6379 > evalsha 5332031 c6b470dc5a0dd9b4bf2030dea6d65de91 0 "hello world" 127.0.0.1:6379 > evalsha 5332031 c6b470dc5a0dd9b4bf2030dea6d65de92 0 (error) NOSCRIPT No matching script. Do use EVAL. 127.0.0.1:6379 > evalsha  5332031c6b470dc5a0dd9b4bf2030dea6d65de91 0 "hello world"Copy the code
Script Management Commands
  • SCRIPT EXISTS sha1 [sha1 …] Check to see if the script represented by sha1 has been added to the script cache, return 1 if it is, and 0 if it is not

    127.0.0.1:6379 > script exists c6b470dc5a0dd9b4bf2030dea6d65de91 1) 1 127.0.0.1 (integer) : 5332031 6379 > script exists 5332031c6b470dc5a0dd9b4bf2030dea6d65de912 1) (integer) 0Copy the code
  • SCRIPT LOAD SCRIPT Stores scripts in the SCRIPT cache for future use by EVALSHA

  • SCRIPT FLUSH Clears all scripts stored in the SCRIPT cache

  • If the SCRIPT has been written, you need to run the SHUTDOWN NOSAVE command to force the server not to save data. Otherwise, the wrong data will be saved to the database

Function library
The standard library
  • Base library: Contains Lua’s core functions, such as Assert, ToString, Error, type, etc
  • String library: Contains functions for handling strings, such as find, format, Len, reverse, and so on
  • Table library: Contains functions for processing tables, such as concat, INSERT, remove, sort, and so on
  • Math library: Contains common mathematical calculation functions such as ABS, SQRT, log, etc
  • Debug library: contains functions needed for debugging programs, such as Sethook and gethook
External libraries
  • Struct library: Converts between C structures and Lua values.
  • Cjson library: Convert Lua values to JSON objects, or JSON objects to Lua values.
  • Cmsgpack library: Encode Lua values into MessagePack format, or decode Lua from MessagePack format

value

Calculates sha1 value external function
  • redis.sha1hex