当前位置: 首页 > 工具软件 > Safe Lua > 使用案例 >

lua redisson执行lua脚本

东郭远航
2023-12-01

lua redisson执行lua脚本

          

             

                                    

相关类与接口

        

RedissonClient

public interface RedissonClient {

    RScript getScript();
    RScript getScript(Codec var1);

        

Redisson

public class Redisson implements RedissonClient {

    public RScript getScript() {
        return new RedissonScript(this.commandExecutor);
    }

    public RScript getScript(Codec codec) {
        return new RedissonScript(this.commandExecutor, codec);
    }

          

RScript:redissonScript实现了该接口

public interface RScript extends RScriptAsync {

    <R> R evalSha(RScript.Mode var1, String var2, RScript.ReturnType var3, List<Object> var4, Object... var5);
    <R> R evalSha(String var1, RScript.Mode var2, String var3, RScript.ReturnType var4, List<Object> var5, Object... var6);
    <R> R evalSha(RScript.Mode var1, String var2, RScript.ReturnType var3);

    <R> R eval(String var1, RScript.Mode var2, String var3, RScript.ReturnType var4, List<Object> var5, Object... var6);
    <R> R eval(RScript.Mode var1, String var2, RScript.ReturnType var3, List<Object> var4, Object... var5);
    <R> R eval(RScript.Mode var1, String var2, RScript.ReturnType var3);
              //直接执行脚本

    String scriptLoad(String var1);     //先加载脚本(缓存到redis),evalSha调用该返回值执行脚本

    List<Boolean> scriptExists(String... var1);

    void scriptKill();
    void scriptFlush();


********
内部枚举:RScript.ReturnType

    public static enum ReturnType {
        BOOLEAN(RedisCommands.EVAL_BOOLEAN_SAFE),
        INTEGER(RedisCommands.EVAL_LONG),
        MULTI(RedisCommands.EVAL_LIST),
        STATUS(RedisCommands.EVAL_STRING),
        VALUE(RedisCommands.EVAL_OBJECT),
        MAPVALUE(RedisCommands.EVAL_MAP_VALUE),
        MAPVALUELIST(RedisCommands.EVAL_MAP_VALUE_LIST);

        private final RedisCommand<?> command;

        private ReturnType(RedisCommand<?> command) {
            this.command = command;
        }

        public RedisCommand<?> getCommand() {
            return this.command;
        }
    }


********
内部枚举:RScript.Mode

    public static enum Mode {
        READ_ONLY,     //只读
        READ_WRITE;    //读写

        private Mode() {
        }
    }
}

       

               

                                    

使用示例

        

test.lua

redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1])

         

RedissonLuaTest

public class RedissonLuaTest {

    public static void main(String[] args) throws Exception{
        Config config = new Config();
        config.useSingleServer().setAddress("redis://localhost:6380");

        RedissonClient client = Redisson.create(config);
        RScript rScript = client.getScript();

        String script = IOUtils.toString(new FileInputStream("./test.lua"), StandardCharsets.UTF_8);
        String value = rScript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE, Collections.singletonList("key"), "瓜田李下");
        System.out.println("直接执行脚本:"+value);

        String evalSha = rScript.scriptLoad(script);
        String value2 = rScript.evalSha(RScript.Mode.READ_WRITE, evalSha, RScript.ReturnType.VALUE, Collections.singletonList("key2"), "海贼王");
        System.out.println("先缓存,后执行脚本:"+value2);
    }
}

          

控制台输出

return redis.call("get", KEYS[1]), 1, key, PooledUnsafeDirectByteBuf(ridx: 0, widx: 15, cap: 256)] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null] using node localhost/127.0.0.1:6380... RedisConnection@32821182 [redisClient=[addr=redis://localhost:6380], channel=[id: 0xac407b67, L:/127.0.0.1:50197 - R:localhost/127.0.0.1:6380], currentCommand=null, usage=1]
20:25:21.595 [redisson-netty-2-3] DEBUG org.redisson.command.RedisExecutor - connection released for command (EVAL) and params [redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1]), 1, key, PooledUnsafeDirectByteBuf(ridx: 0, widx: 15, cap: 256)] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null] using connection RedisConnection@32821182 [redisClient=[addr=redis://localhost:6380], channel=[id: 0xac407b67, L:/127.0.0.1:50197 - R:localhost/127.0.0.1:6380], currentCommand=CommandData [promise=java.util.concurrent.CompletableFuture@5c697f53[Completed normally], command=(EVAL), params=[redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1]), 1, key, PooledUnsafeDirectByteBuf(ridx: 0, widx: 15, cap: 256)], codec=org.redisson.codec.MarshallingCodec], usage=0]
直接执行脚本:瓜田李下


20:25:21.596 [main] DEBUG org.redisson.command.RedisExecutor - acquired connection for command (SCRIPT LOAD) and params [redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1])] from slot NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=23, freeConnectionsCounter=value:63:queue:0, freezeReason=null, client=[addr=redis://localhost:6380], nodeType=MASTER, firstFail=0]]] using node localhost/127.0.0.1:6380... RedisConnection@41474609 [redisClient=[addr=redis://localhost:6380], channel=[id: 0x30723635, L:/127.0.0.1:50198 - R:localhost/127.0.0.1:6380], currentCommand=null, usage=1]
20:25:21.599 [redisson-netty-2-4] DEBUG org.redisson.command.RedisExecutor - connection released for command (SCRIPT LOAD) and params [redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1])] from slot NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=24, freeConnectionsCounter=value:64:queue:0, freezeReason=null, client=[addr=redis://localhost:6380], nodeType=MASTER, firstFail=0]]] using connection RedisConnection@41474609 [redisClient=[addr=redis://localhost:6380], channel=[id: 0x30723635, L:/127.0.0.1:50198 - R:localhost/127.0.0.1:6380], currentCommand=CommandData [promise=java.util.concurrent.CompletableFuture@307dbd30[Completed normally], command=(SCRIPT LOAD), params=[redis.call("set",KEYS[1], ARGV[1])
return redis.call("get", KEYS[1])], codec=org.redisson.codec.MarshallingCodec], usage=0]
20:25:21.600 [main] DEBUG org.redisson.command.RedisExecutor - acquired connection for command (EVALSHA) and params [6c45d62f3875095a03105f73b04c64e77b17fa1e, 1, key2, PooledUnsafeDirectByteBuf(ridx: 0, widx: 12, cap: 256)] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null] using node localhost/127.0.0.1:6380... RedisConnection@2002317052 [redisClient=[addr=redis://localhost:6380], channel=[id: 0xcbb1cd3f, L:/127.0.0.1:50200 - R:localhost/127.0.0.1:6380], currentCommand=null, usage=1]
20:25:21.602 [redisson-netty-2-9] DEBUG org.redisson.command.RedisExecutor - connection released for command (EVALSHA) and params [6c45d62f3875095a03105f73b04c64e77b17fa1e, 1, key2, PooledUnsafeDirectByteBuf(ridx: 0, widx: 12, cap: 256)] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null] using connection RedisConnection@2002317052 [redisClient=[addr=redis://localhost:6380], channel=[id: 0xcbb1cd3f, L:/127.0.0.1:50200 - R:localhost/127.0.0.1:6380], currentCommand=CommandData [promise=java.util.concurrent.CompletableFuture@429c4759[Completed normally], command=(EVALSHA), params=[6c45d62f3875095a03105f73b04c64e77b17fa1e, 1, key2, PooledUnsafeDirectByteBuf(ridx: 0, widx: 12, cap: 256)], codec=org.redisson.codec.MarshallingCodec], usage=0]
先缓存,后执行脚本:海贼王

        

            

 类似资料: