RedisClient pipeline support. #42
parent
c751f88148
commit
dd5bf7c353
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson.client.handler;
|
||||||
|
|
||||||
|
import org.redisson.client.protocol.CommandData;
|
||||||
|
import org.redisson.client.protocol.CommandsData;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Nikita Koksharov
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommandsListEncoder extends MessageToByteEncoder<CommandsData> {
|
||||||
|
|
||||||
|
private final CommandEncoder encoder = new CommandEncoder();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, CommandsData msg, ByteBuf out) throws Exception {
|
||||||
|
for (CommandData<?, ?> commandData : msg.getCommands()) {
|
||||||
|
encoder.encode(ctx, (CommandData<Object, Object>)commandData, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
public class CommandsData implements QueueCommand {
|
||||||
|
|
||||||
|
private final List<CommandData<?, ?>> commands;
|
||||||
|
private final AtomicBoolean sended = new AtomicBoolean();
|
||||||
|
|
||||||
|
public CommandsData(List<CommandData<?, ?>> commands) {
|
||||||
|
super();
|
||||||
|
this.commands = commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CommandData<? extends Object, ? extends Object>> getCommands() {
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AtomicBoolean getSended() {
|
||||||
|
return sended;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
public interface QueueCommand {
|
||||||
|
|
||||||
|
AtomicBoolean getSended();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package org.redisson;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.redisson.client.RedisClient;
|
||||||
|
import org.redisson.client.RedisConnection;
|
||||||
|
import org.redisson.client.protocol.CommandData;
|
||||||
|
import org.redisson.client.protocol.LongCodec;
|
||||||
|
import org.redisson.client.protocol.RedisCommands;
|
||||||
|
import org.redisson.client.protocol.StringCodec;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.Promise;
|
||||||
|
|
||||||
|
public class RedisClientTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws InterruptedException {
|
||||||
|
RedisClient c = new RedisClient("localhost", 6379);
|
||||||
|
final RedisConnection conn = c.connect();
|
||||||
|
|
||||||
|
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
|
||||||
|
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2);
|
||||||
|
for (int i = 0; i < 100000; i++) {
|
||||||
|
pool.execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
conn.async(StringCodec.INSTANCE, RedisCommands.INCR, "test");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.shutdown();
|
||||||
|
pool.awaitTermination(1, TimeUnit.HOURS);
|
||||||
|
|
||||||
|
Assert.assertEquals(100000L, conn.sync(LongCodec.INSTANCE, RedisCommands.GET, "test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPipeline() throws InterruptedException, ExecutionException {
|
||||||
|
RedisClient c = new RedisClient("localhost", 6379);
|
||||||
|
RedisConnection conn = c.connect();
|
||||||
|
|
||||||
|
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
|
||||||
|
|
||||||
|
List<CommandData<?, ?>> commands = new ArrayList<CommandData<?, ?>>();
|
||||||
|
CommandData<String, String> cmd1 = conn.create(null, RedisCommands.PING);
|
||||||
|
commands.add(cmd1);
|
||||||
|
CommandData<Long, Long> cmd2 = conn.create(null, RedisCommands.INCR, "test");
|
||||||
|
commands.add(cmd2);
|
||||||
|
CommandData<Long, Long> cmd3 = conn.create(null, RedisCommands.INCR, "test");
|
||||||
|
commands.add(cmd3);
|
||||||
|
CommandData<String, String> cmd4 = conn.create(null, RedisCommands.PING);
|
||||||
|
commands.add(cmd4);
|
||||||
|
|
||||||
|
conn.send(commands);
|
||||||
|
|
||||||
|
Assert.assertEquals("PONG", cmd1.getPromise().get());
|
||||||
|
Assert.assertEquals(1, (long)cmd2.getPromise().get());
|
||||||
|
Assert.assertEquals(2, (long)cmd3.getPromise().get());
|
||||||
|
Assert.assertEquals("PONG", cmd4.getPromise().get());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue