Table of Contents
1. Install LUA
2)while循环
3)break提前退出循环
7、函数
三、Java中执行Lua脚本
1、字符串方式
2、文件方式
3、Luaj概述
四、Redis + Lua(EVAL命令)
1、EVAL命令
Home Database Redis How to use Lua script in Java ecosystem/Redis

How to use Lua script in Java ecosystem/Redis

Jun 02, 2023 pm 10:41 PM
java redis lua

1. Install LUA

Installing LUA on Mac is very simple, just use the brew related commands directly;

brew install lua
Copy after login

How to use Lua script in Java ecosystem/Redis

##Use

lua -vCommand you can see that lua has been installed.

1) Simply use

to create a test.lua file with the content:

How to use Lua script in Java ecosystem/Redis

Execute the command:

lua test.lua
Copy after login

The output is:

How to use Lua script in Java ecosystem/Redis

2. Introduction to lua syntax

Lua provides interactive programming and scripting Formula programming:

  • Interactive programming: Enter the syntax directly in the command line, and you can execute it immediately and see the execution effect.

  • Scripting is programming: write a script file and then execute it.

1. Comments

lua provides two comment methods: single-line comments and multi-line comments

1) Single-line comments
Use two minus signs;

--

2) Multi-line comments
--[[
 多行注释
 多行注释
 --]]
Copy after login
2, keywords

are as follows Lua's reserved keywords, like Java, cannot be used as constants or variables.

How to use Lua script in Java ecosystem/Redis

3. Variables

By default, a variable defined is a global variable; if you want to use a local variable, you need to declare it as

local;

1) Global variables
Global variables do not need to be declared. After assigning a value to a variable, the global variable is created;

Accessing an uninitialized global variable There will be no error, but the result will be: nil

How to use Lua script in Java ecosystem/Redis

As long as the variable is assigned the value nil, the global variable can be deleted; in other words, if and only if the variable is When nil, the variable does not exist.

How to use Lua script in Java ecosystem/Redis

In addition, names that generally start with an underscore followed by a string of uppercase letters (such as _VERSION) are reserved for Lua internal global variables.

2) Local variables
-- 局部变量赋值
local b=2
Copy after login
4. Data types

Lua is a dynamically typed language. Variables do not need to be typed, only variables need to be assigned values. Values ​​can be stored in variables, passed as arguments or returned as results.

There are 8 basic types in Lua: nil, boolean, number, string, userdata, function, thread and table.

How to use Lua script in Java ecosystem/Redis

1) Lua array
In the Lua array, the index value starts from 1 and can be specified to start from 0.

How to use Lua script in Java ecosystem/Redis

2) String operation
  • .. Connect two strings;

  • string.sub() is used to intercept strings;

  • string.sub(s, i [, j])
    Copy after login
  • s: characters to be intercepted String;

  • i: start position of interception;

  • j: end position of interception, default is -1, last character;

string.find() Used for string search

string.find (str, substr, [init, [plain]])
Copy after login

  • Search for the specified string in a specified target string str Content substr. If a matching substring is found, the starting index and ending index of the substring will be returned. If it does not exist, nil will be returned.

  • init Specifies the starting position of the search, the default is 1, it can be a negative number, indicating the number of characters from back to front.

  • #plain Indicates whether to use simple mode, the default is false, true only does a simple search for substrings, false indicates using regular pattern matching.

5. if-else

The result of the conditional expression can be any value. Lua considers false and nil to be false, and true and non-nil to be true.

The overall if-else structure is similar to the high-level language we use (Java, GO). The difference is that if you want to do some other logic after the if() expression in LUA is satisfied, you need to follow

then, and the flow control ends with end.

if(xxx) then
    print("xxx")
else if(xx) then
    print("xx")
else
   print("x")
end
Copy after login

6. Loop

1) for loop
There are two major categories of for statements in Lua programming language: array for loop and generic for loop

1> Array for loop

Syntax format:

for var=exp1,exp2,exp3 do  
    <执行体>  
end
Copy after login

var changes from exp1 to exp2, each change increments var with exp3 as the step size, and executes it once" executor". exp3 is optional and defaults to 1 if not specified.

How to use Lua script in Java ecosystem/Redis

2> 泛型for循环

通过一个迭代器函数来遍历所有值,类似 java 中的 foreach 语句;

语法格式:

--打印数组a的所有值  
a = {"one", "two", "three"}
for i, v in ipairs(a) do
    print(i, v)
end
Copy after login
  • i 是数组索引值,v 是对应索引的数组元素值。

  • ipairs是Lua提供的一个迭代器函数,用来迭代数组。

How to use Lua script in Java ecosystem/Redis

2)while循环

while 循环语句在判断条件为 true 时会重复执行循环体语句。

语法格式:

while(condition)
do
   statements
end
Copy after login
  • statements(循环体语句) 可以是一条或多条语句,condition(条件) 可以是任意表达式;

  • 在 condition(条件) 为 true 时执行循环体语句。

How to use Lua script in Java ecosystem/Redis

3)break提前退出循环

和Java中的break一个作用,用于退出当前循环或语句;

7、函数

在Lua中,函数是对语句和表达式进行抽象的主要方法。类似于Java中的方法。

Lua 函数主要有两种用途:

  • 完成指定的任务,这种情况下函数作为调用语句使用;

  • 计算并返回值,这种情况下函数作为赋值语句的表达式使用;

函数的编写方式如下:

--[[ 函数返回两个值的最大值 --]]
function max(num1, num2)
   if (num1 > num2) then
      result = num1;
   else
      result = num2;
   end
   return result; 
end
-- 调用函数
print("两值比较最大值为 ",max(10,4))
print("两值比较最大值为 ",max(5,6))
Copy after login

How to use Lua script in Java ecosystem/Redis

三、Java中执行Lua脚本

Java中执行Lua脚本有两种方式:字符串的方式、文件的方式;

Java中想要执行LUA脚本,首先需要在pom中引入相关依赖:

<dependency>
    <groupId>org.luaj</groupId>
    <artifactId>luaj-jse</artifactId>
    <version>3.0.1</version>
</dependency>
Copy after login

1、字符串方式

对于简单的lua脚本,可以直接用java字符串写;

package com.saint.base.lua;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;

public class LuaString {
    public static void main(String[] args) {
        String luaStr = "print &#39;Saint is best man&#39;";
        Globals globals = JsePlatform.standardGlobals();
        LuaValue luaValue = globals.load(luaStr);
        luaValue.call();
    }
}
Copy after login

控制台输出:

How to use Lua script in Java ecosystem/Redis

2、文件方式

对于一些比较常用的、复杂的脚本可以选择存放在文件中,在Java中再调用lua文件;

package com.saint.base.lua;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;

import java.io.FileNotFoundException;

public class LuaFile {
    public static void main(String[] args) throws FileNotFoundException {
        // lua脚本的文件路径
        String luaPath = "/xxxx/javaTest.lua";
        Globals globals = JsePlatform.standardGlobals();
        //加载脚本文件login.lua,并编译
        globals.loadfile(luaPath).call();
        LuaValue func1 = globals.get(LuaValue.valueOf("print1"));
        func1.call();

        LuaValue func2 = globals.get(LuaValue.valueOf("print2"));
        String luaResp = func2.call(LuaValue.valueOf("saint-input-param")).toString();
        System.out.println("lua file return is : " + luaResp);
    }
}
Copy after login

lua脚本文件:

How to use Lua script in Java ecosystem/Redis

控制台输出:

How to use Lua script in Java ecosystem/Redis

3、Luaj概述

Luaj在包装执行具体的Lua代码时, 有三种不同的模式;

  • 纯脚本解析执行(不选用任何Compiler)

  • To Lua字节码(LuaC, lua-to-lua-bytecode compiler)(默认选用)

  • To Java字节码(LuaJC, lua-to-java-bytecode compiler)

Luaj中的Globals对象不是线程安全的, 因此最佳实践是每个线程一个Globals对象。

事实上, 可以采用ThreadLocal的方式来存储该对象。

2)性能问题

Lua脚本在JAVA中运行,相比于直接运行Java代码会慢很多,大约1000倍。

四、Redis + Lua(EVAL命令)

在使用Redisson、Jedis+Lua时,我们可以通过redis客户端集成的、手写的LUA脚本来保证一系列命令在Redis中可以"原子执行"。

在redis执行lua脚本时,相当于一个redis级别的锁,不能执行其他操作,类似于原子操作,这也是redisson实现的一个关键点。

比如Redisson中的lua脚本:

How to use Lua script in Java ecosystem/Redis

Redisson如何实现分布式锁,可以看文章:https://www.yisu.com/article/277312.htm

lua脚本中有如下几个概念:

  • redis.call():执行redis命令。

  • KEYS[n]:指脚本中第n个参数,比如KEYS[1]指脚本中的第一个参数。

  • ARGV[n]:指脚本中第n个参数的值,比如ARGV[1]指脚本中的第一个参数的值。

  • 返回值中nil与false同一个意思。

1、EVAL命令

redis2.6.0版本起 采用内置的Lua解释器 通过EVAL命令去执行脚本;

redis中的EVAL命令可以用于执行一段lua代码。命令格式如下:

How to use Lua script in Java ecosystem/Redis

  • 第一个参数script:表示lua脚本的内容;

  • 第二参数numkeys:表示有多少个键值对。

  • 其余参数:先把numkeys个key列出来,再把numkeys个arg列出来。

Lua脚本中可以使用2个函数调用redis命令;

  • redis.call()

  • redis.pcall()

redis.call()与redis.pcall()相似,二者唯一不同之处:

  • 如果执行的redis命令执行失败,redis.call()将产生一个Lua error,从而迫使EVAL命令返回一个错误给命令的调用者;

  • 然而redis.pcall()将会捕捉这个错误,并返回代表这个错误的Lua表。

有那么一段逻辑;

  • 如果Redis某个key的整数值 和 某个value相等,则将key对应的整数值 + 1000;否则将key的值设置为9999;

lua脚本执行命令如下:

EVAL "if redis.call(&#39;get&#39;, KEYS[1]) == ARGV[1] then return redis.call(&#39;INCRBY&#39;, KEYS[1], 1000); else redis.call(&#39;set&#39;, KEYS[1], 9999); return nil; end;" 1 test 100
Copy after login

根据test值的不同,不同的执行结果如下:

How to use Lua script in Java ecosystem/Redis

The above is the detailed content of How to use Lua script in Java ecosystem/Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

See all articles