luttuce(RedisTempate)实现hash expire lua脚本

话不多说先放脚本:

local argv = ARGV
local length = #argv
if length > 0 then  local unpackArgs = {
   }
for i = 1, length - 1 do
 table.insert(unpackArgs, argv[i])
end
if redis.call('exists', KEYS[1]) == 1 then
	redis.call('del', KEYS[1])
	redis.call('hset', KEYS[1], unpack(unpackArgs))
	redis.call('expire', KEYS[1], argv[length])
	return 1
else
	redis.call('hset', KEYS[1], unpack(unpackArgs))
	redis.call('expire', KEYS[1], ARGV[#ARGV])
	return 2
end
end

2.直接String 执行LUA脚本

  /**
     *  lua 脚本 实现物料key 的赋值
     * @param hashKey 物料key
     * @param fieldsAndValues lua 脚本参数
     * @return
     */
    public Long insertIntoHashWithExpireTime(String hashKey, Object... fieldsAndValues) {
   
        Long result = null;
        // unpack(ARGV)在Lua中是用来将数组解包成一系列单独的参数。
        // ARGV[#ARGV]获取的是传递给脚本的最后一个参数(在 Lua 中,# 操作符用于获取表中的元素数量。因此,ARGV[#ARGV] 将返回 ARGV 表中的最后一个元素。)
        // 请求案例:insertIntoHashWithExpireTimeIfKeyExists("myHash", "field1", "value1", "field2", "value2", 60);
        String luaScript =
                            "\nlocal argv = ARGV \n" +
                            "local length = #argv \n"+
                            // 将1 - n-1 的入参写入新的数组
                            "if length > 0 then  local unpackArgs = {} \n"  +
                                    "for i = 1, length - 1 do  \n" +
                                    " table.insert(unpackArgs, argv[i]) \n" +
                            "end\n" +
                            "if redis.call('exists', KEYS[1]) == 1 then \n" +
                                "\tredis.call('del', KEYS[1]) \n" +
                                "\tredis.call('hset', KEYS[1], unpack(unpackArgs)) \n" +
                                "\tredis.call('expire', KEYS[1], argv[length]) \n" +
                                "\treturn 1  \n" +
                            "else \n" +
                                "\tredis.call('hset', KEYS[1], unpack(unpackArgs)) \n" +
                                "\tredis.call('expire', KEYS[1], argv[length]) \n" +
                                "\treturn 2\n" +
                            "end \n" +
                            "end ";
        log.info("luaScript:{}", luaScript);
        try {
   
            DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
            redisScript.setResultType(Long.class);//返回类型是Long
            redisScript.setScriptText(luaScript);
            result = redisTemplate.execute(redisScript, Arrays.asList(hashKey), fieldsAndValues);
            log.debug("redisEVALLuaScript result :{}", result);
        } catch (Exception e) {
   
            e.printStackTrace();
            log.error("e:", e);
        }
        return result;
    }
    @Test
    public void testLua(){
   
        String hashKey = "testHash";
        String field1 = "test3";
        String value1 = "vv1";
        String field2 = "test4";
        String value2 = "vv2";
        int expireTime = 10000;

        // 插入数据并设置过期时间
        redisUtils.insertIntoHashWithExpireTime(hashKey, field1, value1, field2, value2,expireTime);
    }

最终结果:
在这里插入图片描述

相关推荐

  1. python脚本实战

    2023-12-16 07:20:06       14 阅读
  2. 【uniapp】Uniapp cli 自动化打包脚本实现

    2023-12-16 07:20:06       44 阅读
  3. 99个Python脚本实用实例

    2023-12-16 07:20:06       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-16 07:20:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-16 07:20:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-16 07:20:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-16 07:20:06       18 阅读

热门阅读

  1. Lua 模仿C++类

    2023-12-16 07:20:06       40 阅读
  2. PHP中如何进行单元测试和集成测试?

    2023-12-16 07:20:06       42 阅读
  3. 力扣5. 最长回文子串

    2023-12-16 07:20:06       36 阅读
  4. 30天精通Nodejs--第十四天:MongoDB

    2023-12-16 07:20:06       41 阅读
  5. 虾皮Shopee API接口获取商品图片列表

    2023-12-16 07:20:06       46 阅读
  6. register_chrdev函数使用

    2023-12-16 07:20:06       38 阅读
  7. 微信小程序 - 龙骨图集拆分

    2023-12-16 07:20:06       37 阅读
  8. uniapp微信小程序下载base64图片流或https图片

    2023-12-16 07:20:06       40 阅读