关于redis:聊聊RedisToken

32次阅读

共计 5248 个字符,预计需要花费 14 分钟才能阅读完成。

本文次要钻研一下 RedisToken

RedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/RedisToken.java

public interface RedisToken {RedisToken NULL_STRING = string((SafeString) null);
  RedisToken RESPONSE_OK = status("OK");

  RedisTokenType getType();

  <T> T accept(RedisTokenVisitor<T> visitor);

  static RedisToken nullString() {return NULL_STRING;}

  static RedisToken responseOk() {return RESPONSE_OK;}

  static RedisToken string(SafeString str) {return new StringRedisToken(str);
  }

  static RedisToken string(String str) {return new StringRedisToken(safeString(str));
  }

  static RedisToken status(String str) {return new StatusRedisToken(str);
  }

  static RedisToken integer(boolean b) {return new IntegerRedisToken(b ? 1 : 0);
  }

  static RedisToken integer(int i) {return new IntegerRedisToken(i);
  }

  static RedisToken error(String str) {return new ErrorRedisToken(str);
  }

  static RedisToken array(RedisToken... redisTokens) {return new ArrayRedisToken(ImmutableList.of(redisTokens));
  }

  static RedisToken array(Collection<RedisToken> redisTokens) {return new ArrayRedisToken(ImmutableList.from(redisTokens));
  }

  static RedisToken array(Sequence<RedisToken> redisTokens) {return new ArrayRedisToken(redisTokens.asArray());
  }

  static <T> Stream<T> visit(Stream<RedisToken> tokens, RedisTokenVisitor<T> visitor) {return tokens.map(token -> token.accept(visitor));
  }
}
  • RedisToken 接口定义了 getType、accept 办法;它还提供了 nullString、responseOk、string、status、integer、error、array、visit 静态方法

AbstractRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

public abstract class AbstractRedisToken<T> implements RedisToken {

  private static final String SEPARATOR = "=>";
  private static final Equal<AbstractRedisToken<?>> EQUAL =
      Equal.<AbstractRedisToken<?>>of()
          .comparing(AbstractRedisToken::getType)
          .comparing(AbstractRedisToken::getValue);

  private final RedisTokenType type;
  private final T value;

  private AbstractRedisToken(RedisTokenType type, T value) {this.type = requireNonNull(type);
    this.value = value;
  }

  @Override
  public RedisTokenType getType() {return type;}

  public T getValue() {return value;}

  @Override
  public int hashCode() {return Objects.hash(type, value);
  }

  @Override
  public boolean equals(Object obj) {return EQUAL.applyTo(this, obj);
  }

  @Override
  public String toString() {return type + SEPARATOR + value;}

}
  • AbstractRedisToken 申明实现 RedisToken 接口,它的结构器要求输出 type、value

UnknownRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class UnknownRedisToken extends AbstractRedisToken<SafeString> {UnknownRedisToken(SafeString value) {super(RedisTokenType.UNKNOWN, value);
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.unknown(this);
    }
  }
  • UnknownRedisToken 继承了 AbstractRedisToken,其 value 为 SafeString 类型,其 type 为 RedisTokenType.UNKNOWN,其 accept 执行的是 visitor.unknown(this)

StringRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class StringRedisToken extends AbstractRedisToken<SafeString> {StringRedisToken(SafeString value) {super(RedisTokenType.STRING, value);
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.string(this);
    }
  }
  • StringRedisToken 继承了 AbstractRedisToken,其 value 为 SafeString,其 type 为 RedisTokenType.STRING,其 accept 执行的是 visitor.string(this)

StatusRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class StatusRedisToken extends AbstractRedisToken<String> {StatusRedisToken(String value) {super(RedisTokenType.STATUS, value);
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.status(this);
    }
  }
  • StatusRedisToken 继承了 AbstractRedisToken,其 value 为 String,其 type 为 RedisTokenType.STATUS,其 accept 执行的是 visitor.status(this)

ErrorRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class ErrorRedisToken extends AbstractRedisToken<String> {ErrorRedisToken(String value) {super(RedisTokenType.ERROR, value);
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.error(this);
    }
  }
  • ErrorRedisToken 继承了 AbstractRedisToken,其 value 为 String,其 type 为 RedisTokenType.ERROR,其 accept 执行的是 visitor.error(this)

IntegerRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class IntegerRedisToken extends AbstractRedisToken<Integer> {IntegerRedisToken(Integer value) {super(RedisTokenType.INTEGER, value);
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.integer(this);
    }
  }
  • IntegerRedisToken 继承了 AbstractRedisToken,其 value 为 Integer,其 type 为 RedisTokenType.INTEGER,其 accept 执行的是 visitor.integer(this)

ArrayRedisToken

resp-server-0.16.0/src/main/java/com/github/tonivade/resp/protocol/AbstractRedisToken.java

  public static final class ArrayRedisToken extends AbstractRedisToken<Sequence<RedisToken>> {ArrayRedisToken(Sequence<RedisToken> value) {super(RedisTokenType.ARRAY, requireNonNull(value).asArray());
    }

    @Override
    public <T> T accept(RedisTokenVisitor<T> visitor) {return visitor.array(this);
    }

    public int size() {return getValue().size();}
  }
  • ArrayRedisToken 继承了 AbstractRedisToken,其 value 为 Sequence<RedisToken>,其 type 为 RedisTokenType.ARRAY,其 accept 执行的是 visitor.array(this)

小结

RedisToken 接口定义了 getType、accept 办法;它还提供了 nullString、responseOk、string、status、integer、error、array、visit 静态方法;AbstractRedisToken 申明实现 RedisToken 接口,它的结构器要求输出 type、value,它有 UnknownRedisToken、StringRedisToken、StatusRedisToken、ErrorRedisToken、IntegerRedisToken、ArrayRedisToken 这些实现类

doc

  • RedisToken

正文完
 0