《reddis开发与运维》笔记
通信协议:客户端与服务端的协议是TCP,redis定制了redis序列化,高效
java客户端Jedis
# 1. 生成一个 Jedis 对象,这个对象负责和指定 Redis 实例进行通信
Jedis jedis = new Jedis("127.0.0.1", 6379);
# 2. jedis 执行 set 操作
jedis.set("hello", "world");
# 3. jedis 执行 get 操作 , value="world"
String value = jedis.get("hello");jedis.close();
·host:Redis实例的所在机器的IP。
·port:Redis实例的端口。
·connectionTimeout:客户端连接超时。
·soTimeout:客户端读写超时。
jedis连接池
// common-pool 连接池配置,这里使用默认配置,后面小节会介绍具体配置说明
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
// 初始化 Jedis 连接池
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379);
Jedis jedis = null;
try {
// 1. 从连接池获取 jedis 对象
jedis = jedisPool.getResource();
// 2. 执行操作
jedis.get("hello");
} catch (Exception e) {
logger.error(e.getMessage(),e);
} finally {
if (jedis != null) {
// 如果使用 JedisPool , close 操作不是关闭连接,代表归还连接池
jedis.close();
}
}
jedis支持Pipeline批量操作
public void mdel(List<String> keys) {
Jedis jedis = new Jedis("127.0.0.1");
// 1) 生成 pipeline 对象
Pipeline pipeline = jedis.pipelined();
// 2)pipeline 执行命令,注意此时命令并未真正执行
for (String key : keys) {
pipeline.del(key);
}
// 3) 执行命令
pipeline.sync();
}
客户端管理
client list:能列出所有客户端的信息
(1)标识:id、addr、fd、name
(2)输入缓冲区:qbuf、qbuf-free
(3)输出缓冲区:obl、oll、omem
(4)客户端的存活状态client list中的age和idle分别代表当前客户端已经连接的时间和最近一次的空闲时间。
(5)客户端的限制maxclients(客户端连接数)和timeout
(6)客户端类型