cool hit counter Use of RedisLua_Intefrankly

Use of RedisLua

I. Introduction to Lua

Lua is a lightweight and compact scripting language, written in standard C and open source, designed to be embedded in applications to provide flexibility in extending and customizing them.

Features of Lua

Lightweight: It is written in standard C and open source, compiled to just over 100K, and can be easily embedded in other programs.

Extensible: Lua provides very easy-to-use extension interfaces and mechanisms: the host language (usually C or C++) provides these features, and Lua can use them as if they were already built-in.

Other characteristics:

Support for procedural-oriented programming and functional programming.

Automatic memory management; only one generic type of table (table) is provided, with which arrays, hash tables, collections, objects can be implemented.

Language built-in pattern matching; closure (closure); function can also be seen as a value; provides multi-threading (co-processing, not threads as supported by the OS) support.

Some of the key mechanisms needed for object-oriented programming, such as data abstraction, virtual functions, inheritance and overloading, can be easily supported through closures and tables.

Lua is simple and small, but very powerful, and is used by many large companies and middleware, such as

Blizzard introduces Lua to World of Warcraft

Rovio brings Lua to Angry Birds

Nginx uses Lua as an extension language

Redis supports execution of Lua scripts

If you want to learn Lua comprehensively, you can go to the Lua website.

II. Using Lua in Redis

Redis supports the execution of Lua scripts, which provide more flexibility with Redis commands.

1.Executing Lua scripts in Redis

There are two ways to execute Lua scripts in Redis: eval and evalsha.

The eval command

The syntax rules for the eval command are.

For example.

In the above commandLua The script reads'return "I " .. KEYS[1] .. ARGV[1]', among others .. denotes joining two strings,key The number of individuals is 1 ,KEYS[1] = am,ARGV[1] = lebron。

evalsha command

Sometimes Lua script content is too long to fit in the eval command, and having to take the Lua script with you every time you execute the eval command increases the network overhead of the request. So Redis provides the evalsha command to solve this problem.

We can set theLua The script is loaded into theRedis memory,Redis will return thisLua scriptedSHA1 checksum, Each subsequent execution ofLua The script only needs to use the evalsha command Pair it with thisSHA1 Just check the sum.。

The evalsha command is passed in the same format as the eval command, the only difference between the two commands is whether they use SHA checksums or native Lua scripts.

After loading Lua scripts into Redis memory, the script functions can be reused, thus reducing network overhead.

You can use the script load command to load the contents of the script into Redis memory and return the SHA1 checksum

It is also possible to write the script content to a file and load the Lua script into Redis memory by loading the Lua script file

Lua The script is loaded into theRedis Once in memory, you can use the evalsha command go ahead and implementLua It's scripted.

3. Using Redis commands in Lua scripts

The Lua script in the case above is just the simplest string splicing and return, and does not involve Redis commands.

The Redis command can be invoked in Lua scripts using at

The difference between the two commands is that if fails, the Lua script ends and returns an error; if redis.pcall fails, it ignores the error and continues executing the script.

When using distributed locks, you generally need to use the setnx command to seize the lock, and then you need to use the expire command to set an expiration date on the lock to prevent an exception from causing the lock to never be released. But Redis doesn't provide a command to do both, and we can use Lua scripts to do this for us.

Let's start using Lua scripts to help us implement this atomic operation:

Creating Lua Script Files

Create the setnxex.lua file and write the Lua script.

Writing Lua script files to Redis memory

Get the SHA1 checksum of the Lua script file.

Preempting distributed locks with the evalsha command

Returns 1 to indicate a successful preemption of a distributed lock

Returning 0 means that preemption of the distributed lock failed

4. Managing Lua scripts in Redis memory

Redis provides 4 commands for managing Lua scripts.

script load

This command is used to load Lua scripts into Redis memory, as described earlier.

scripts exists

This command verifies the number of sha1 [sha1 ...] that have been loaded into Redis memory.

Passing only one argument verifies that the script has not been loaded into Redis memory.

script flush

This command is used to clear all Lua scripts from Redis memory.

script kill

This command is used to kill the executing Lua script. If our Lua script execution is time consuming or if there is a problem with the Lua script that causes it to enter a dead loop, you can call this command to kill the ongoing Lua script command.

III. Summary


Lua scripts are executed atomically in Redis and the execution is not affected by other client commands

Lua scripts can help us customize commands to fit our business and can be resident in memory for reuse

Some batch tasks can be written to Lua scripts to reduce network overhead


Redis provides a configuration for lua-time-limit, which defaults to 5 seconds and is the "timeout" for Redis Lua scripts.

Once the Lua script execution time exceeds this value, Redis will stop serving other clients properly. Other clients will receive the following error when executing the command.

At this point, Redis is blocked and cannot be served properly. You can stop the Lua script command with the script kill command or stop the Redis service directly with the shutdown save command.

In this case we would definitely prefer to use the script kill command to stop the Lua script rather than stopping the Redis service directly. However, if a write operation has already been performed in a Lua script, executing the script kill command will report an error and will not stop the execution of the Lua script, only the Redis service.

As you can see Redis Lua scripts, although good, can have serious consequences if not used in the right position.

1、SelfMedia Operations Mastery Tools
2、Symposium on the application of artificial intelligence in the oil sector
3、PHP to get query results
4、Keras author pushes open source framework Lore 15 minutes to get deep learning models from configuration to deployment
5、Psychological analysis of participants in the Traffic Magic Box mining game

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送