All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.server.resp.commands.generic.RANDOMKEY Maven / Gradle / Ivy

There is a newer version: 15.1.4.Final
Show newest version
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