Redis客户端
一、命令行
- 命令行是Redis自带的一个很方便的客户端,相关的操作在第一篇文章中有介绍。为了提高redis的效率,可以批量设值,mset k1 v1 k2 v2,在前面一篇文章中提到过了
二、Jedis
- Jedis是java的redis客户端,也是使用最广泛的java客户端。
1.1 示例
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1-mozping-SNAPSHOT</version>
</dependency>
</dependencies>
public class MyRedisClient1 {
public static final String REDIS_HOST = "192.168.11.27";
public static final String REDIS_PASSWORD = "Intellifusion@20190108";
public static final int REDIS_PORT = 6379;
public static final Jedis JEDIS = new Jedis(REDIS_HOST);
static {
JEDIS.auth(REDIS_PASSWORD);
}
public static void main(String[] args) throws InterruptedException {
String rsp = JEDIS.set("name", "mozping");
System.out.println("响应:" + rsp);
String ping = JEDIS.ping();
System.out.println("ping的响应:" + ping);
}
}
打印:
响应:OK
ping的响应:PONG
2.2 关键参数
- Jedis的构造方法处理地址和端口之外,还有几个关键的参数
public Jedis(final String host, final int port, final int connectionTimeout, final int soTimeout,final boolean ssl)
connectionTimeout:表示连接超时时间。默认2秒
soTimeout:表示读取数据超时时间。默认2秒
ssl:ssl安全连接,布尔类型,默认false
参数默认值:
private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;
private int soTimeout = Protocol.DEFAULT_TIMEOUT;
public static final int DEFAULT_TIMEOUT = 2000;
三、JedisPool
- JedisPool是Jedis连接池。可以通过JedisPool来封装jedis客户端的工具类,避免频繁的创建jedis连接对象。
如果使用了Spring,可以使用bean的方式来初始化JedisPool,如果没有的话可以使用静态代码块的方式来初始化。
public class JedisUtils {
private JedisPool pool = null;
private String ip = "192.168.42.111";
private int port = 6379;
private String auth = "12345678";
public JedisUtils() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(100);
config.setTestOnBorrow(true);
pool = new JedisPool(config, this.ip, this.port, 100000, this.auth);
}
}
public String get(String key) {
Jedis jedis = null;
String value = null;
try {
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
returnResource(jedis);
}
return value;
}
public String set(String key, String value) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.set(key, value);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
return "0";
} finally {
returnResource(jedis);
}
}
public Long del(String... keys) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.del(keys);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
return 0L;
} finally {
returnResource(jedis);
}
}
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = pool.getResource();
return jedis.exists(key);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
returnResource(jedis);
}
}
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
String res = null;
try {
jedis = pool.getResource();
res = jedis.setex(key, seconds, value);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
returnResource(jedis);
}
return res;
}
public static void returnResource(Jedis jedis) {
try {
if (jedis != null) {
jedis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
四、JedisCluster
- JedisCluster是连接redis集群的客户端。目前尚未分析道redis的架构演变(单机->分片->集群)。后续再分析,
可以阅读参考文献[2]
五、手写Jedis简单客户端
- 手写redis实际上就是了解RESP协议。不了解协议结构,可以阅读参考文献[3]
public class MySimpleRedisClient {
static class RedisClient {
private static Socket socket;
private static OutputStream write;
private static InputStream read;
public RedisClient(String host, int port) throws IOException {
socket = new Socket(host, port);
write = socket.getOutputStream();;
read = socket.getInputStream();
}
public void set(String key, String val) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("*3").append("\r\n");
sb.append("$3").append("\r\n");
sb.append("SET").append("\r\n");
sb.append("$").append(key.getBytes().length).append("\r\n");
sb.append(key).append("\r\n");
sb.append("$").append(val.getBytes().length).append("\r\n");
sb.append(val).append("\r\n");
write.write(sb.toString().getBytes());
byte[] bytes = new byte[1024];
read.read(bytes);
System.out.println("set " + key + " -- > " + val + ", success," + new String(bytes));
}
public void get(String key) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("*2").append("\r\n");
sb.append("$3").append("\r\n");
sb.append("GET").append("\r\n");
sb.append("$").append(key.getBytes().length).append("\r\n");
sb.append(key).append("\r\n");
write.write(sb.toString().getBytes());
byte[] bytes = new byte[1024];
read.read(bytes);
System.out.println("get" + key + " success," + new String(bytes));
System.out.println(new String(bytes));
}
}
public static void main(String[] args) throws IOException {
RedisClient jedis = new RedisClient("127.0.0.1", 6379);
jedis.set("test", "001");
jedis.get("test");
}
}
六、参考