Write the redis script and the redis script together

Source: Internet
Author: User

Write the redis script and the redis script together
I. redis script Environment

To write redis scripts, we should first set up its script environment. Redis uses lua as its scripting language. Therefore, building a lua environment becomes the first priority.

Environment: centos7.0
Redis: 3.0.2

1.1 install the lua Environment

[root@localhost lua]# curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz[root@localhost lua]# tar zxf lua-5.3.0.tar.gz[root@localhost lua]# cd lua-5.3.0[root@localhost lua]# make linux

An error was reported during the execution,

It indicates what readline is missing. If it is missing, install it.

1.2 install readlin

[Root @ localhost lua] # yum-y install gcc make ncurses-devel readline-devel # After performing the preceding operations, continue with the following command [root @ localhost lua] # make linux [root @ localhost lua] # make install # Use The lua command to check whether it is installed as [root @ localhost lua] # lua

The installation is successful.

Ii. hello world script

Let's start our first redis script from the classic hello world.
Create a lua file in a directory to store scripts.
2.1 lua script content

local msg = 'Hello, World!';return msg;

This script assigns hello world to msg and returns it back.
2.2 execute this script
Before executing the command, make sure that the redis service is enabled. Run the following command.

Redis-cli-eval/root/lua/first. lua

Command explanation:-eval tells redis-cli to read and run the subsequent Lua script, followed by the path of the stored lua File.
This is an execution method, and another execution method.

Redis-cli EVAL "$ (cat first. lua)" 0

The two execution methods will get the same result. In the second method, it is equivalent to executing a shell script in the redis command. Cat first. lua means to obtain and display the content of the file. Then 0 indicates the number of the redis key. If this script is not used, it is set to 0.
Individuals prefer the first method of execution.

Iii. redis script 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 description: Redis uses the incr command to increase the first incoming key value by 1, and then uses the hset command to add the second incoming key and the value obtained through incr as the field, then, the first parameter of argv is saved to hashtable as the value. In addition, the field value of the stored key is returned at the end.
3.2 run the script.
Method 1:

Redis-cli-eval/root/lua/second. lua links: counter links: urls, http://www.bnersoft.com/

Method 2:

Redis-cli eval "$ (cat second. lua)" 2 links: counter links: urls http://www.bnersoft.com/

Either of the two methods, and then use the redis client, you can clearly see that two new keys are added in redis.

[root@localhost lua]# KEYS *


Then, you can use the relevant commands to view the values. We will not demonstrate them here. You can test them yourself.

PS: I am a beginner in redis. If you are not sure about this, I hope this article will help you a little bit.

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.