Redis-在springboot环境下执行lua脚本

文章目录

1、什么lua

"Lua"的英文全称是"Lightweight Userdata Abstraction Layer",意思是"轻量级用户数据抽象层"。

2、创建SpringBoot工程

3、引入相关依赖

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

  <!-- Generated by https://start.springboot.io -->
  <!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
	<groupId>com.atguigu</groupId>
	<artifactId>springboot-lua</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-lua</name>
	<description>springboot-lua</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

4、创建LUA脚本

bash 复制代码
local current = redis.call('GET',KEYS[1])
if current == ARGV[1]
    then redis.call('SET',KEYS[1],ARGV[2])
    return true
else
    redis.call('SET',KEYS[1],ARGV[3])
    return true
end
return false

5、创建配置类

java 复制代码
package com.atguigu.lua.config;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
@Component
public class AppRedisConfiguration {

    //简单序列化
    @Bean
    public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        // 设置键序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置简单类型值的序列化方式
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        // 设置默认序列化方式
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    //加载lua脚本,设置返回值类型
    @Bean
    public RedisScript<Boolean> script() {
        Resource scriptSource = new ClassPathResource("lua/test.lua");
        return RedisScript.of(scriptSource, Boolean.class);
    }

}

6、创建启动类

java 复制代码
package com.atguigu.lua;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
public class SpringbootLuaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootLuaApplication.class, args);
	}

}

7、创建测试类

java 复制代码
package com.atguigu.lua;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import java.util.List;

@SpringBootTest
class SpringbootLuaApplicationTests {
	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private RedisScript redisScript;

	@Test
	void contextLoads() {
		redisTemplate.execute(redisScript, List.of("str"),"spring","mybatis","springboot");
	}

}
bash 复制代码
127.0.0.1:6379> keys *
1) "balance"
2) "str"
127.0.0.1:6379> get str
"springboot"
127.0.0.1:6379> 
相关推荐
m0_748238633 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
甜可儿10 分钟前
redis序列化设置
数据库·redis
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
千层冷面1 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
zr想努力3 小时前
Lua的table类型的增删改查操作
java·开发语言·lua
码有余悸3 小时前
Redis:高性能的键值存储系统
数据库·redis·缓存
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
老朋友此林10 小时前
浅析 Redis 分片集群 Cluster 原理、手动搭建、动态伸缩集群、故障转移
java·数据库·redis
IT学长编程10 小时前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节