SpringDataRedis是否支持模块?

zaq34kh6  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(495)

我正在使用SpringDataRedis(版本2.1.1release)和莴苣(版本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:没有枚举常量io.莴苣.core.protocol.commandtype.json.get foo
有办法吗?如何使用redis模块?

p4rjhz4m

p4rjhz4m1#

好吧,我就这么做了:
第一次延伸 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);
  }
}

相关问题