![JAR search and dependency download from the Maven repository](/logo.png)
org.infinispan.server.resp.commands.generic.RANDOMKEY Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infinispan-server-resp Show documentation
Show all versions of infinispan-server-resp Show documentation
Infinispan Resp Protocol Server
package org.infinispan.server.resp.commands.generic;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ThreadLocalRandom;
import org.infinispan.AdvancedCache;
import org.infinispan.server.resp.Resp3Handler;
import org.infinispan.server.resp.RespCommand;
import org.infinispan.server.resp.RespRequestHandler;
import org.infinispan.server.resp.commands.Resp3Command;
import org.infinispan.server.resp.serialization.Resp3Response;
import io.netty.channel.ChannelHandlerContext;
/**
* `RANDOMKEY
` command.
*
* Returns a random key from the current selected cache.
*
*
* @since 15.0
* @see Redis Documentation
*/
public class RANDOMKEY extends RespCommand implements Resp3Command {
public RANDOMKEY() {
super(1, 0, 0, 0);
}
@Override
public CompletionStage perform(Resp3Handler handler, ChannelHandlerContext ctx, List arguments) {
AdvancedCache cache = handler.cache();
CompletableFuture cs = cache.sizeAsync()
.thenApply(size -> {
if (size == 0)
return null;
// Try to insert some randomness in the returned key.
return cache.keySet().stream()
.skip(ThreadLocalRandom.current().nextInt(size.intValue()))
.findAny()
.orElse(null);
});
return handler.stageToReturn(cs, ctx, Resp3Response.BULK_STRING_BYTES);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy