Conversion between Lua data types and Redis data types

Source: Internet
Author: User

When Lua executes the Redis command through the call () or Pcall () function, the return value of the command is converted to the LUA data structure. Similarly, when the Lua script runs in the Redis built-in interpreter, the return value of the Lua script is also converted to the Redis Protocol (Protocol), and the value is returned to the client by EVAL.

Conversions between data types follow a design principle: If you convert a Redis value to a LUA value, and then convert the converted Lua value back to the Redis value, the Redis value for this conversion should be the same as the Redis value at the time of the original.

In other words, there is a one by one conversion relationship between the Lua type and the Redis type.

A Redis-to-Lua conversion table.

    • Redis integer reply, lua Number/redis integer converted to LUA numbers
    • Redis Bulk reply, Lua String/redis Bulk replies convert to LUA strings
    • Redis Multi Bulk reply, LUA table (May has other Redis data types nested)/Redis multiple bulk replies converted to LUA tables, there might be other redi in the table s data type
    • Redis status reply, Lua table with a single OK field containing the Status/redis state reply is converted to Lua tables, the OK field in the table contains the status information
    • Redis error reply, LUA table with a single err field containing the Error/redis error reply converted to Lua table, the Err field in the table contains an error message
    • Redis Nil Bulk reply and nil Multi bulk Reply-lua false Boolean Type/redis nil reply and nil multiple replies convert to Lua Boolean false

Lua to Redis conversion table.

    • Lua number-a Redis integer reply (the converted into an integer)/LUA numbers converted to Redis integers
    • Lua string-, Redis bulk Reply/lua strings converted to Redis bulk replies
    • Lua table (array), Redis Multi Bulk reply (truncated to the first nil inside the LUA array if any)/LUA tables (arrays) converted to Red is multiple bulk reply
    • Lua table with a single OK field--Redis status reply/Lua tables with one OK field converted to Redis State reply
    • Lua table with a single err field---Redis error reply/LUA tables with individual ERR fields converted to Redis fault reply
    • Lua Boolean false, Redis Nil bulk reply. /Lua Boolean value false converted to Redis Nil bulk reply

There is an additional rule for converting from Lua to Redis, which does not have a rule that corresponds to the transition from Redis to LUA:

    • Lua boolean true, Redis integer reply with value of 1. /Lua Boolean value True converts to 1 in Redis Integer replies

There are two points that need to be highlighted:

    • There is no difference between integers and floating-point numbers in Lua. Therefore, we always convert the number of Lua into an integer reply, so that the fractional part is rounded off. If you want to return a floating-point number from LUA, you should use it as a string (see, for example, the Zscore command).
    • There is no simple-to-Nils inside LUA arrays, this is a result of the LUA table semantics, so when Redis converts a L UA array into Redis protocol The conversion are stopped if a nil is encountered.

Conversion between Lua data types and Redis data types

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.