您的位置:首页 > 财经 > 产业 > Redis-在springboot环境下执行lua脚本

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

2024/10/6 14:33:20 来源:https://blog.csdn.net/m0_65152767/article/details/139879925  浏览:    关键词:Redis-在springboot环境下执行lua脚本

文章目录

  • 1、什么lua
  • 2、创建SpringBoot工程
  • 3、引入相关依赖
  • 4、创建LUA脚本
  • 5、创建配置类
  • 6、创建启动类
  • 7、创建测试类

1、什么lua

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

2、创建SpringBoot工程

在这里插入图片描述

3、引入相关依赖

<?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脚本

local current = redis.call('GET',KEYS[1])
if current == ARGV[1]then redis.call('SET',KEYS[1],ARGV[2])return true
elseredis.call('SET',KEYS[1],ARGV[3])return true
end
return false

5、创建配置类

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 {//简单序列化@Beanpublic 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脚本,设置返回值类型@Beanpublic RedisScript<Boolean> script() {Resource scriptSource = new ClassPathResource("lua/test.lua");return RedisScript.of(scriptSource, Boolean.class);}}

6、创建启动类

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、创建测试类

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 {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate RedisScript redisScript;@Testvoid contextLoads() {redisTemplate.execute(redisScript, List.of("str"),"spring","mybatis","springboot");}}
127.0.0.1:6379> keys *
1) "balance"
2) "str"
127.0.0.1:6379> get str
"springboot"
127.0.0.1:6379> 

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com