org.redisson.RedissonScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* Copyright (c) 2013-2021 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import org.redisson.api.NodeType;
import org.redisson.api.RFuture;
import org.redisson.api.RScript;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.MasterSlaveEntry;
import org.redisson.connection.NodeSource;
import org.redisson.misc.CompletableFutureWrapper;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
*
* @author Nikita Koksharov
*
*/
public class RedissonScript implements RScript {
private final Codec codec;
private final CommandAsyncExecutor commandExecutor;
public RedissonScript(CommandAsyncExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
this.codec = commandExecutor.getConnectionManager().getCodec();
}
public RedissonScript(CommandAsyncExecutor commandExecutor, Codec codec) {
this.commandExecutor = commandExecutor;
this.codec = codec;
}
@Override
public String scriptLoad(String luaScript) {
return commandExecutor.get(scriptLoadAsync(luaScript));
}
public String scriptLoad(String key, String luaScript) {
return commandExecutor.get(scriptLoadAsync(key, luaScript));
}
@Override
public RFuture scriptLoadAsync(String luaScript) {
Collection nodes = commandExecutor.getConnectionManager().getEntrySet();
List> futures = new ArrayList<>();
nodes.forEach(e -> {
RFuture promise = commandExecutor.async(false, new NodeSource(e),
codec, RedisCommands.SCRIPT_LOAD, new Object[]{luaScript}, true, false);
futures.add(promise.toCompletableFuture());
e.getAllEntries().stream().filter(c -> c.getNodeType() == NodeType.SLAVE).forEach(c -> {
RFuture slavePromise = commandExecutor.async(true, new NodeSource(e, c.getClient()),
codec, RedisCommands.SCRIPT_LOAD, new Object[]{luaScript}, true, false);
futures.add(slavePromise.toCompletableFuture());
});
});
CompletableFuture f = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
CompletableFuture s = f.thenApply(r -> futures.get(0).getNow(null));
return new CompletableFutureWrapper<>(s);
}
@Override
public RFuture scriptLoadAsync(String key, String luaScript) {
return commandExecutor.writeAsync(key, StringCodec.INSTANCE, RedisCommands.SCRIPT_LOAD, luaScript);
}
@Override
public R eval(Mode mode, String luaScript, ReturnType returnType) {
return eval(mode, luaScript, returnType, Collections.emptyList());
}
@Override
public R eval(Mode mode, String luaScript, ReturnType returnType, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy