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.*;
/**
* A synchronous thread-safe connection to a redis server. Multiple threads may
* share one {@link RedisConnection} 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 RedisConnection extends SimpleChannelUpstreamHandler {
protected BlockingQueue> queue;
protected RedisCodec codec;
protected Channel channel;
private int timeout;
private TimeUnit unit;
private String password;
private int db;
private MultiOutput multi;
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 responses.
* @param unit Unit of time for the timeout.
*/
public RedisConnection(BlockingQueue> queue, RedisCodec codec, int 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(int timeout, TimeUnit unit) {
this.timeout = timeout;
this.unit = unit;
}
public Long append(K key, V value) {
Command cmd = dispatch(APPEND, new IntegerOutput(codec), key, value);
return getOutput(cmd);
}
public String auth(String password) {
this.password = password;
CommandArgs args = new CommandArgs(codec).add(password);
Command cmd = dispatch(AUTH, new StatusOutput(codec), args);
return getOutput(cmd);
}
public String bgrewriteaof() {
Command cmd = dispatch(BGREWRITEAOF, new StatusOutput(codec));
return getOutput(cmd);
}
public String bgsave() {
Command cmd = dispatch(BGSAVE, new StatusOutput(codec));
return getOutput(cmd);
}
public List blpop(long timeout, K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys).add(timeout);
Command> cmd = dispatch(BLPOP, new ValueListOutput(codec), args);
return getOutput(cmd);
}
public List brpop(long timeout, K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys).add(timeout);
Command> cmd = dispatch(BRPOP, new ValueListOutput(codec), args);
return getOutput(cmd);
}
public V brpoplpush(long timeout, K source, K destination) {
CommandArgs args = new CommandArgs(codec);
args.addKey(source).addKey(destination).add(timeout);
Command cmd = dispatch(BRPOPLPUSH, new ValueOutput(codec), args);
return getOutput(cmd);
}
public String clientKill(String addr) {
CommandArgs args = new CommandArgs(codec).add(KILL).add(addr);
Command cmd = dispatch(CLIENT, new StatusOutput(codec), args);
return getOutput(cmd);
}
public String clientList() {
CommandArgs args = new CommandArgs(codec).add(LIST);
Command cmd = dispatch(CLIENT, new StatusOutput(codec), args);
return getOutput(cmd);
}
public List configGet(String parameter) {
CommandArgs args = new CommandArgs(codec).add(GET).add(parameter);
Command> cmd = dispatch(CONFIG, new StringListOutput(codec), args);
return getOutput(cmd);
}
public String configResetstat() {
CommandArgs args = new CommandArgs(codec).add(RESETSTAT);
Command cmd = dispatch(CONFIG, new StatusOutput(codec), args);
return getOutput(cmd);
}
public String configSet(String parameter, String value) {
CommandArgs args = new CommandArgs(codec).add(SET).add(parameter).add(value);
Command cmd = dispatch(CONFIG, new StatusOutput(codec), args);
return getOutput(cmd);
}
public Long dbsize() {
Command cmd = dispatch(DBSIZE, new IntegerOutput(codec));
return getOutput(cmd);
}
public String debugObject(K key) {
CommandArgs args = new CommandArgs(codec).add(OBJECT).addKey(key);
Command cmd = dispatch(DEBUG, new StatusOutput(codec), args);
return getOutput(cmd);
}
public Long decr(K key) {
Command cmd = dispatch(DECR, new IntegerOutput(codec), key);
return getOutput(cmd);
}
public Long decrby(K key, long amount) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(amount);
Command cmd = dispatch(DECRBY, new IntegerOutput(codec), args);
return getOutput(cmd);
}
public Long del(K... keys) {
CommandArgs args = new CommandArgs(codec).addKeys(keys);
Command cmd = dispatch(DEL, new IntegerOutput(codec), args);
return getOutput(cmd);
}
public String discard() {
Command cmd = dispatch(DISCARD, new StatusOutput(codec));
multi = null;
return getOutput(cmd);
}
public V echo(V msg) {
CommandArgs args = new CommandArgs(codec).addValue(msg);
Command cmd = dispatch(ECHO, new ValueOutput(codec), args);
return getOutput(cmd);
}
public Boolean exists(K key) {
Command cmd = dispatch(EXISTS, new BooleanOutput(codec), key);
return getOutput(cmd);
}
public Boolean expire(K key, long seconds) {
CommandArgs args = new CommandArgs(codec).addKey(key).add(seconds);
Command cmd = dispatch(EXPIRE, new BooleanOutput(codec), args);
return getOutput(cmd);
}
public Boolean expireat(K key, Date timestamp) {
long seconds = timestamp.getTime() / 1000;
CommandArgs args = new CommandArgs(codec).addKey(key).add(seconds);
Command cmd = dispatch(EXPIREAT, new BooleanOutput(codec), args);
return getOutput(cmd);
}
public List