Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

查看 Redis 请求参数 #77

Open
songlonqi-java opened this issue Apr 23, 2024 · 1 comment
Open

查看 Redis 请求参数 #77

songlonqi-java opened this issue Apr 23, 2024 · 1 comment

Comments

@songlonqi-java
Copy link
Collaborator

目前支持的: #19 #76

需要增加别的驱动支持例如:RedissonSpring Data Redis Redis Template

@songlonqi-java
Copy link
Collaborator Author

redisson比较特殊一点

Command 中获取参数的方法是 command.getParams() 方法返回的是Object[]。 这里要说下这个Object数组。

如果是命令,比如 set,get等,那么就是String类型。

如果是参数则是 byte[] 类型, 需要把ObjectByteBuf类型。这个ByteBuf有个特点,第0位是标识符00000011, 最后一位的最高位为1,例如:参数如果是 SET aaaaa ,那么参数列表为:Object["set",byte[3,a,a,a,a,a|0x7f]] , 最后一位打印二进制位:11100001 而 a的二进制却是 01100001。 所以在拿到ByteBuf类型 需要将第0位切掉,最后一位去掉高位的1。

部分java代码:

      for (Object val : args) {
        if (val instanceof ByteBuf) {
          ByteBuf buf = (ByteBuf) val;
          if (buf.hasArray()) {
            String data = new String(buf.array(), buf.arrayOffset() + buf.readerIndex(), buf.readableBytes(), StandardCharsets.UTF_8);
            sb.append(data).append(" ");
          } else {
            byte[] bytes = new byte[buf.readableBytes()];
            buf.getBytes(buf.readerIndex(), bytes);
            try{
              byte[] result = new byte[bytes.length - 1];
              System.arraycopy(bytes, 1, result, 0, bytes.length - 2);
              result[result.length - 1] = (byte) (bytes[bytes.length - 1] & 0x7F);
              String data = new String(result, StandardCharsets.UTF_8);
              sb.append(data).append(" ");
            }catch (Exception e){
              System.out.println(e);
            }
          }
        } else {
          sb.append(val.toString()).append(" ");
        }
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant