zhub-client/src/main/java/org/redkalex/cache/redis/RedisCacheRequest.java

62 lines
1.8 KiB
Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkalex.cache.redis;
import org.redkale.net.client.ClientConnection;
import org.redkale.net.client.ClientRequest;
import org.redkale.util.ByteArray;
import java.nio.charset.StandardCharsets;
/**
* @author zhangjx
*/
public class RedisCacheRequest extends ClientRequest {
static final byte[] TRUE = new byte[]{'t'};
static final byte[] FALSE = new byte[]{'f'};
protected String key;
protected String command;
protected byte[][] args;
public <T> RedisCacheRequest prepare(String command, String key, byte[]... args) {
super.prepare();
this.command = command;
this.key = key;
this.args = args;
return this;
}
@Override
public void writeTo(ClientConnection conn, ByteArray writer) {
writer.put((byte) '*');
writer.put(String.valueOf(args.length + 1).getBytes(StandardCharsets.UTF_8));
writer.put((byte) '\r', (byte) '\n');
writer.put((byte) '$');
writer.put(String.valueOf(command.length()).getBytes(StandardCharsets.UTF_8));
writer.put((byte) '\r', (byte) '\n');
writer.put(command.getBytes(StandardCharsets.UTF_8));
writer.put((byte) '\r', (byte) '\n');
for (final byte[] arg : args) {
writer.put((byte) '$');
writer.put(String.valueOf(arg.length).getBytes(StandardCharsets.UTF_8));
writer.put((byte) '\r', (byte) '\n');
writer.put(arg);
writer.put((byte) '\r', (byte) '\n');
}
}
@Override
public String toString() {
return getClass().getSimpleName() + "{command=" + command + ", key=" + key + "}";
}
}