码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot整合Redis以及Lua脚本的使用

时间:2021-06-11 18:20:30      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ttext   依赖   inf   cat   Lua脚本   return   rip   img   ali   

一、添加maven依赖

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

二、application.yml配置redis环境信息

spring:
  redis:
    host: 172.16.2.95
    timeout: 1000ms

三、依赖注入

/**
 * Redis工具类
 *
 * @author zjc
 * @date 2021/2/4 14:35
 */
@Component
public class RedisUtil {

    /**
     * redis模板
     */
    private RedisTemplate redisTemplate;
    private ValueOperations<String, String> valueOperations;
    private HashOperations<String, String, String> hashOperations;
    private ListOperations<String, String> listOperations;
    private SetOperations<String, String> setOperations;
    private ZSetOperations<String, String> zSetOperations;

    @Autowired
    public RedisUtil(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.valueOperations = redisTemplate.opsForValue();
        this.hashOperations = redisTemplate.opsForHash();
        this.listOperations = redisTemplate.opsForList();
        this.setOperations = redisTemplate.opsForSet();
        this.zSetOperations = redisTemplate.opsForZSet();
    }

    private static final String LUA_SCRIPT_HGETALL = "local r = {} for _, v in pairs(KEYS) do   r[#r+1] = redis.call(‘HGETALL‘, v) end return r";

    public void lua() {
        List<String> keys = new ArrayList<>();
        keys.add("key1");
        keys.add("key2");
//        DefaultRedisScript<List> getRedisScript = new DefaultRedisScript();
//        getRedisScript.setScriptText(LUA_SCRIPT_HGETALL);
//        getRedisScript.setResultType(List.class);
        Object ob = redisTemplate.execute(RedisScript.of(LUA_SCRIPT_HGETALL, List.class), keys);
        System.out.println(JSONUtil.toJsonStr(ob));
    }
}

注意:

技术图片

 

 返回值只能为Long,Boolean,List,或deserialized。

Springboot整合Redis以及Lua脚本的使用

标签:ttext   依赖   inf   cat   Lua脚本   return   rip   img   ali   

原文地址:https://www.cnblogs.com/Jackbao/p/14873298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!