Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright (C) 2011 - Will Glozer. All rights reserved.
package com.lambdaworks.redis;
import com.lambdaworks.redis.codec.RedisCodec;
import com.lambdaworks.redis.output.*;
import com.lambdaworks.redis.protocol.*;
import org.jboss.netty.channel.*;
import java.util.*;
import java.util.concurrent.*;
import static com.lambdaworks.redis.protocol.CommandKeyword.*;
import static com.lambdaworks.redis.protocol.CommandType.*;
/**
* An asynchronous thread-safe connection to a redis server. Multiple threads may
* share one {@link RedisAsyncConnection} provided they avoid blocking and transactional
* operations such as {@link #blpop} and {@link #multi()}/{@link #exec}.
*
* A {@link ConnectionWatchdog} monitors each connection and reconnects
* automatically until {@link #close} is called. All pending commands will be
* (re)sent after successful reconnection.
*
* @author Will Glozer
*/
public class RedisAsyncConnection extends SimpleChannelUpstreamHandler {
protected BlockingQueue> queue;
protected RedisCodec codec;
protected Channel channel;
protected long timeout;
protected TimeUnit unit;
protected MultiOutput multi;
private String password;
private int db;
private boolean closed;
/**
* Initialize a new connection.
*
* @param queue Command queue.
* @param codec Codec used to encode/decode keys and values.
* @param timeout Maximum time to wait for a response.
* @param unit Unit of time for the timeout.
*/
public RedisAsyncConnection(BlockingQueue> queue, RedisCodec codec, long timeout, TimeUnit unit) {
this.queue = queue;
this.codec = codec;
this.timeout = timeout;
this.unit = unit;
}
/**
* Set the command timeout for this connection.
*
* @param timeout Command timeout.
* @param unit Unit of time for the timeout.
*/
public void setTimeout(long timeout, TimeUnit unit) {
this.timeout = timeout;
this.unit = unit;
}
public Future append(K key, V value) {
return dispatch(APPEND, new IntegerOutput(codec), key, value);
}
public String auth(String password) {
CommandArgs args = new CommandArgs(codec).add(password);
Command cmd = dispatch(AUTH, new StatusOutput(codec), args);
String status = await(cmd, timeout, unit);
if ("OK".equals(status)) this.password = password;
return status;
}
public Future bgrewriteaof() {
return dispatch(BGREWRITEAOF, new StatusOutput(codec));
}
public Future bgsave() {
return dispatch(BGSAVE, new StatusOutput(codec));
}
public Future> blpop(long timeout, K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys).add(timeout);
return dispatch(BLPOP, new KeyValueOutput(codec), args);
}
public Future> brpop(long timeout, K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys).add(timeout);
return dispatch(BRPOP, new KeyValueOutput(codec), args);
}
public Future brpoplpush(long timeout, K source, K destination) {
CommandArgs args = new CommandArgs(codec);
args.addKey(source).addKey(destination).add(timeout);
return dispatch(BRPOPLPUSH, new ValueOutput(codec), args);
}
public Future clientKill(String addr) {
CommandArgs args = new CommandArgs(codec).add(KILL).add(addr);
return dispatch(CLIENT, new StatusOutput(codec), args);
}
public Future clientList() {
CommandArgs args = new CommandArgs(codec).add(LIST);
return dispatch(CLIENT, new StatusOutput(codec), args);
}
public Future> configGet(String parameter) {
CommandArgs args = new CommandArgs(codec).add(GET).add(parameter);
return dispatch(CONFIG, new StringListOutput(codec), args);
}
public Future configResetstat() {
CommandArgs args = new CommandArgs(codec).add(RESETSTAT);
return dispatch(CONFIG, new StatusOutput(codec), args);
}
public Future configSet(String parameter, String value) {
CommandArgs args = new CommandArgs(codec).add(SET).add(parameter).add(value);
return dispatch(CONFIG, new StatusOutput(codec), args);
}
public Future dbsize() {
return dispatch(DBSIZE, new IntegerOutput(codec));
}
public Future debugObject(K key) {
CommandArgs args = new CommandArgs(codec).add(OBJECT).addKey(key);
return dispatch(DEBUG, new StatusOutput(codec), args);
}
public Future decr(K key) {
return dispatch(DECR, new IntegerOutput(codec), key);
}
public Future decrby(K key, long amount) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(amount);
return dispatch(DECRBY, new IntegerOutput(codec), args);
}
public Future del(K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys);
return dispatch(DEL, new IntegerOutput(codec), args);
}
public Future discard() {
multi = null;
return dispatch(DISCARD, new StatusOutput(codec));
}
public Future echo(V msg) {
CommandArgs args = new CommandArgs(codec).addValue(msg);
return dispatch(ECHO, new ValueOutput(codec), args);
}
public Future exists(K key) {
return dispatch(EXISTS, new BooleanOutput(codec), key);
}
public Future expire(K key, long seconds) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(seconds);
return dispatch(EXPIRE, new BooleanOutput(codec), args);
}
public Future expireat(K key, Date timestamp) {
return expireat(key, timestamp.getTime() / 1000);
}
public Future expireat(K key, long timestamp) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(timestamp);
return dispatch(EXPIREAT, new BooleanOutput(codec), args);
}
public Future> exec() {
MultiOutput multi = this.multi;
this.multi = null;
return dispatch(EXEC, multi);
}
public Future flushall() throws Exception {
return dispatch(FLUSHALL, new StatusOutput(codec));
}
public Future flushdb() throws Exception {
return dispatch(FLUSHDB, new StatusOutput(codec));
}
public Future get(K key) {
return dispatch(GET, new ValueOutput(codec), key);
}
public Future getbit(K key, long offset) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(offset);
return dispatch(GETBIT, new IntegerOutput(codec), args);
}
public Future getrange(K key, long start, long end) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(start).add(end);
return dispatch(GETRANGE, new ValueOutput(codec), args);
}
public Future getset(K key, V value) {
return dispatch(GETSET, new ValueOutput(codec), key, value);
}
public Future hdel(K key, K... fields) {
CommandArgs args = new CommandArgs(codec).addKey(key).addKeys(fields);
return dispatch(HDEL, new IntegerOutput(codec), args);
}
public Future hexists(K key, K field) {
CommandArgs args = new CommandArgs(codec).addKey(key).addKey(field);
return dispatch(HEXISTS, new BooleanOutput(codec), args);
}
public Future hget(K key, K field) {
CommandArgs args = new CommandArgs(codec).addKey(key).addKey(field);
return dispatch(HGET, new ValueOutput(codec), args);
}
public Future hincrby(K key, K field, long amount) {
CommandArgs args = new CommandArgs(codec).addKey(key).addKey(field).add(amount);
return dispatch(HINCRBY, new IntegerOutput(codec), args);
}
public Future