A. Redis scripting environment
To write a Redis script, you should first build its scripting environment. Redis uses LUA as its scripting language. So building the LUA environment is a top priority.
Environment: centos7.0
redis:3.0.2
1.1 Installing the LUA environment
[[email protected] lua]# curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz[[email protected] lua]# tar zxf lua-5.3.0.tar.gz[[email protected] lua]# cd lua-5.3.0[[email protected] lua]# make linux
Executed here, and reported a mistake,
Say what ReadLine is missing. If it is missing, go ahead and install it.
1.2 Installing Readlin
[[email protected] lua]# yum -y install gcc make ncurses-devel readline-devel# 执行完以上操作后,继续以下命令[[email protected] lua]# make linux[[email protected] lua]# make install# 使用lua命令,查看是否安装成[[email protected] lua]# lua
See, the installation was successful.
Second, Hello World script
Start our first Redis script from the classic Hello world.
Start by creating a LUA file in a directory to hold the script.
2.1 Lua Script Content
local‘Hello, World!‘;return msg;
the script is to assign Hello World to msg, and then return back.
2.2 Execute this script
Before executing, make sure that the Redis service is turned on. Then use the command to execute.
Redis-cli–eval/root/lua/first.lua
command explanation: –eval tells Redis-cli to read and run the following Lua script, followed by the path of the LUA file that is stored .
This is a way of doing it, and there is another way to do it.
REDIS-CLI EVAL "$ (cat First.lua)" 0
Both methods of execution will have the same result. In the second way, a shell script is executed in a command equivalent to Redis. The cat First.lua means to get the contents of the file and display it. Then 0 indicates the numeric number of the key used for Redis, which is not used and is set to 0.
Individuals are more inclined to the first form of execution.
Iii. Redis Scripts with parameters
3.1 Script Content
local link_id = redis.call("INCR", KEYS[1])redis.call("HSET",KEYS[2],link_id,ARGV[1])return link_id
script Function Description : Redis increases the first incoming key value by 1 by using the INCR command, and then hset the second passed-in key, as well as the value obtained by INCR as the field, and then argv the first parameter as the value, and then deposits it into the Hashtable. and returns the value of the field to which the key was stored.
3.2 Execute the script.
Way One:
Redis-cli–eval/root/lua/second.lua Links:counter Links:urls, http://www.bnersoft.com/
Way two:
REDIS-CLI eval "$ (cat Second.lua)" 2 links:counter links:urls http://www.bnersoft.com/
Two ways, either, and then using Redis's own client, you can see clearly the new two keys added to the Redis.
[root@localhost lua]# KEYS *
Then through the relevant commands, you can see the values, here is not a demonstration, you can test it yourself.
PS: Beginner Redis, the inaccuracy of the place, hope to teach, also hoped that this article has a little help to everyone.
Write a Redis script together