LUA Scripting
Redisson provides RScript object to execute Lua script. It has atomicity property and used to process data on Redis or Valkey side. Script could be executed in two modes:
Mode.READ_ONLYscripts are executed as read operationMode.READ_WRITEscripts are executed as write operation
One of the follow types returned as a script result object:
ReturnType.BOOLEAN- Boolean type.ReturnType.INTEGER- Integer type.ReturnType.MULTI- List type of user defined type.ReturnType.STATUS- Lua String type.ReturnType.VALUE- User defined type.ReturnType.MAPVALUE- Map value type.ReturnType.MAPVALUELIST- List of Map value type.
Code example:
RBucket<String> bucket = redisson.getBucket("foo");bucket.set("bar");RScript script = redisson.getScript(StringCodec.INSTANCE);String r = script.eval(Mode.READ_ONLY,"return redis.call('get', 'foo')",RScript.ReturnType.VALUE);// execute the same script stored in Redis or Valkey lua script cache// load lua script into Redis or Valkey cache to all master instancesString res = script.scriptLoad("return redis.call('get', 'foo')");// res == 282297a0228f48cd3fc6a55de6316f31422f5d17// call lua script by sha digestFuture<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY,"282297a0228f48cd3fc6a55de6316f31422f5d17",RScript.ReturnType.VALUE, Collections.emptyList());
Functions
Redisson provides RFunction object to execute Functions. It has atomicity property and used to process data on Redis or Valkey side. Function can be executed in two modes:
Mode.READexecutes function as read operationMode.WRITEexecutes function as write operation
One of the follow types returned as a script result object:
ReturnType.BOOLEAN- Boolean typeReturnType.LONG- Long typeReturnType.LIST- List type of user defined type.ReturnType.STRING- plain String typeReturnType.VALUE- user defined typeReturnType.MAPVALUE- Map Value type. Codec.getMapValueDecoder() and Codec.getMapValueEncoder() methods are used for data deserialization or serializationReturnType.MAPVALUELIST- List type, which consists of objects of Map Value type. Codec.getMapValueDecoder() and Codec.getMapValueEncoder() methods are used for data deserialization or serialization
Code example:
RFunction f = redisson.getFunction();// load functionf.load("lib", "redis.register_function('myfun', function(keys, args) return args[1] end)");// execute functionString value = f.call(RFunction.Mode.READ, "myfun", RFunction.ReturnType.STRING, Collections.emptyList(), "test");
Code example of Async interface usage:
RFunction f = redisson.getFunction();// load functionRFuture<Void> loadFuture = f.loadAsync("lib", "redis.register_function('myfun', function(keys, args) return args[1] end)");// execute functionRFuture<String> valueFuture = f.callAsync(RFunction.Mode.READ, "myfun", RFunction.ReturnType.STRING, Collections.emptyList(), "test");
Code example of Reactive interface usage:
RedissonReactiveClient redisson = redissonClient.reactive();RFunctionReactive f = redisson.getFunction();// load functionMono<Void> loadMono = f.load("lib", "redis.register_function('myfun', function(keys, args) return args[1] end)");// execute functionMono<String> valueMono = f.callAsync(RFunction.Mode.READ, "myfun", RFunction.ReturnType.STRING, Collections.emptyList(), "test");
Code example of RxJava3 interface usage:
RedissonRxClient redisson = redissonClient.rxJava();RFunctionRx f = redisson.getFunction();// load functionCompletable loadMono = f.load("lib", "redis.register_function('myfun', function(keys, args) return args[1] end)");// execute functionMaybe<String> valueMono = f.callAsync(RFunction.Mode.READ, "myfun", RFunction.ReturnType.STRING, Collections.emptyList(), "test");
