Spring data redis 支持模块吗?

Spring data redis support for modules?

我正在使用 Spring 数据 Redis(版本 2.1.1RELEASE)和 lettuce 的驱动程序(版本 5.1.3RELEASE) 我想使用这个模块:https://oss.redislabs.com/redisjson/ 但驱动程序似乎不支持它。

我尝试使用 execute 方法:

Object response = redisTemplate.execute((RedisCallback<String>) connection -> {
  return  (String) connection.execute("JSON.GET foo");
});

出现错误:

java.lang.IllegalArgumentException: No enum constant io.lettuce.core.protocol.CommandType.JSON.GET FOO

有办法吗?如何使用 Redis 模块?

好的,所以我设法做到了:

首先扩展 ProtocolKeyword 并将所有“_”字符替换为“.”:

public interface ModuleCommand extends ProtocolKeyword {

  @Override
  default byte[] getBytes() {
    return name().replaceAll("_", ".").getBytes(LettuceCharsets.ASCII);
  }
}

其次,使用您希望执行的命令类型创建一个枚举:

public enum JsonCommand implements ModuleCommand {

  JSON_GET,
  JSON_MGET,
  JSON_SET,
  JSON_ARRAPPEND,
  JSON_DEL,
  // other commands you wish to implement
  ;
}

现在是有趣的部分,我已经集成了更多模块,所以我必须概括执行部分:

public abstract class ModuleAbstractManager {


  protected ByteArrayCodec codec = ByteArrayCodec.INSTANCE;

  @Autowired
  private LettuceConnectionFactory connectionFactory;

  protected <T> Optional<T> execute(String key, ModuleCommand jsonCommand, CommandOutput<byte[], byte[], T> output, String... args) {

    List<byte[]> extraArgs = Arrays.stream(args)
        .filter(arg -> !StringUtils.isEmpty(arg))
        .map(String::getBytes)
        .collect(Collectors.toList());

    CommandArgs<byte[], byte[]> commandArgs = new CommandArgs<>(codec)
        .addKey(key.getBytes())
        .addValues(extraArgs);

    LettuceConnection connection = (LettuceConnection) connectionFactory.getConnection();

    try {
      RedisFuture<T> future = connection.getNativeConnection().dispatch(jsonCommand, output, commandArgs);
      return Optional.ofNullable(future.get());
    }
    catch (InterruptedException | ExecutionException e) {
      return Optional.empty();
    }
    finally {
      connection.close();
    }
  }
}

最后是执行本身:

@Service
public class RedisJsonManager extends ModuleAbstractManager {

  public static final String ROOT_PATH = ".";
  public static final String OK_RESPONSE = "OK";
  public static final String SET_IF_NOT_EXIST_FLAG = "NX";


  public Optional<String> getValue(String key) {
    return getValue(key, ROOT_PATH);
  }

  public Optional<String> getValue(String key, String path) {

    if (StringUtils.isEmpty(path)) {
      return Optional.empty();
    }

    return execute(key, JsonCommand.JSON_GET, new ValueOutput<>(codec), path)
        .map(String::new);
  }

  public Optional<String> getValue(String key, List<String> multiPath) {

    if (CollectionUtils.isEmpty(multiPath)) {
      return Optional.empty();
    }

    String[] args = multiPath.toArray(new String[0]);
    return execute(key, JsonCommand.JSON_GET, new ValueOutput<>(codec), args)
        .map(String::new);
  }

  public boolean setValueIfNotExist(String key, String json) {
    return setValue(key, json, ROOT_PATH, true);
  }

  public boolean setValueIfNotExist(String key, String json, String path) {
    return setValue(key, json, path, true);
  }

  public boolean setValue(String key, String json) {
    return setValue(key, json, ROOT_PATH, false);
  }

  public boolean setValue(String key, String json, String path) {
    return setValue(key, json, path, false);
  }

  private boolean setValue(String key, String json, String path, boolean setIfNotExist) {
    return execute(key, JsonCommand.JSON_SET, new StatusOutput<>(codec), path, json, setIfNotExist ? SET_IF_NOT_EXIST_FLAG : "")
        .map(OK_RESPONSE::equals)
        .orElse(false);
  }

  public Long addToArray(String key, String json) {
    return addToArray(key, json, ROOT_PATH);
  }

  public Long addToArray(String key, String json, String path) {
    return execute(key, JsonCommand.JSON_ARRAPPEND, new IntegerOutput<>(codec), path, json).orElse(0L);
  }

  public Long delete(String key) {
    return delete(key, ROOT_PATH);
  }

  public Long delete(String key, String path) {
    return execute(key, JsonCommand.JSON_DEL, new IntegerOutput<>(codec), path).orElse(0L);
  }
}