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

Lettuce自定义 Commands

巩才捷
2023-12-01

相关文档:
自定义Command文档
批量执行文档

一、自定义 Commands

如果想要批量执行,可以添加 @BatchSize 注解。

import io.lettuce.core.RedisFuture;
import io.lettuce.core.Value;
import io.lettuce.core.dynamic.Commands;
import io.lettuce.core.dynamic.annotation.Command;
import io.lettuce.core.dynamic.annotation.CommandNaming;
import io.lettuce.core.dynamic.annotation.Param;

import java.util.List;

import static io.lettuce.core.dynamic.annotation.CommandNaming.Strategy.METHOD_NAME;

/**
 * 需要继承自 {@link Commands}
 * 文档地址:https://lettuce.io/core/release/reference/#redis-command-interfaces
 */
public interface KeyCommands extends Commands {

    String set(String key, String value);

    @Command("SET ?1 ?0")
    String set1(String value, String key);

    @Command("SET :key :value")
    String set2(@Param("key") String key, @Param("value") String value);

    @CommandNaming(strategy = METHOD_NAME)
    String mSet(String key1, String value1, String key2, String value2);

    @CommandNaming(strategy = METHOD_NAME)
    RedisFuture<String> get(String key);

    List<String> mget(String... keys);

    @Command("MGET")
    List<Value<String>> mget1(String... keys);

}

二、测试类

import io.lettuce.core.Value;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.dynamic.RedisCommandFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class RedisTest {

    private static RedisClusterClient client = RedisClusterClient.create("redis://localhost:7000");

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        RedisCommandFactory factory = new RedisCommandFactory(client.connect());
        KeyCommands keyCommands = factory.getCommands(KeyCommands.class);
        keyCommands.set("{jz:}qj", "1");
        keyCommands.set("{jz:}qqj", "12");
        keyCommands.set("{jz:}qqqj", "123");

        List<String> res1 = keyCommands.mget("{jz:}qj", "{jz:}qqj", "{jz:}qqqj");
        System.out.println(res1);

        List<Value<String>> res2 = keyCommands.mget1("{jz:}qj", "{jz:}qqj", "{jz:}qqqj");
        System.out.println(res2);

        System.out.println(keyCommands.get("{jz:}qj").get());

        keyCommands.mSet("{jz:}k", "ii", "{jz:}kk", "iioi");

        keyCommands.set1("test", "ki");

        keyCommands.set2("kki", "test1");

    }

}

三、批量执行 Commands

import io.lettuce.core.RedisFuture;
import io.lettuce.core.dynamic.Commands;
import io.lettuce.core.dynamic.batch.BatchExecutor;
import io.lettuce.core.dynamic.batch.BatchSize;
import io.lettuce.core.dynamic.batch.CommandBatching;

@BatchSize(10)
public interface BatchCommands extends Commands, BatchExecutor {

    /**
     * 同步命令无法获取到结果,只能通过 RedisFuture 异步获取
     */
    void set(String key, String value);

    RedisFuture<String> get(String key);

    RedisFuture<String> get(String key, CommandBatching batching);

}

四、批量执行测试类

BatchExecutor.flush() 或者 CommandBatching.flush() 两种方式可在未达到 size 时强制刷新。

import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.dynamic.RedisCommandFactory;
import io.lettuce.core.dynamic.batch.CommandBatching;

import java.util.concurrent.ExecutionException;

public class RedisTest {

    private static RedisClusterClient client = RedisClusterClient.create("redis://localhost:7000");

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        RedisCommandFactory factory = new RedisCommandFactory(client.connect());
        BatchCommands batchCommands = factory.getCommands(BatchCommands.class);
        batchCommands.set("{jz:}qj", "1");
        batchCommands.set("{jz:}qqj", "12");
        batchCommands.set("{jz:}qqqj", "123");

        batchCommands.get("{jz:}qj").thenAcceptAsync(value -> System.out.println(value));
        batchCommands.get("{jz:}qqj", CommandBatching.queue()).thenAcceptAsync(value -> System.out.println(value));
        batchCommands.get("{jz:}qqqj", CommandBatching.flush()).thenAcceptAsync(value -> System.out.println(value));
        System.out.println("------------------");
        Thread.sleep(2000);


        batchCommands.set("{jz:}qj1", "1w");
        batchCommands.set("{jz:}qqj1", "12w");
        batchCommands.set("{jz:}qqqj1", "123w");

        batchCommands.get("{jz:}qj1").thenAcceptAsync(value -> System.out.println(value));
        batchCommands.get("{jz:}qqj1").thenAcceptAsync(value -> System.out.println(value));
        batchCommands.get("{jz:}qqqj1").thenAcceptAsync(value -> System.out.println(value));
        batchCommands.flush();
        System.out.println("------------------");
        Thread.sleep(2000);
    }

}
 类似资料: