Jedis 连接池的基本使用
jedis直连 每次操作都会创建一个jedis对象,执行完毕后关闭连接后释放,对应的就是一次Tcp连接。 jedis连接池 预先生成一批jedis连接对象放入连接池中,当需要对redis进行操作时从连接池中借用jedis对象,操作完成后归还。这样jedis对象可以重复使用,避免了频繁创建socket连接,节省了连接开销。 方案对比 连接池简单使用 public class Demo { public static void main(String[] args) { //连接池配置对象,包含了很多默认配置 GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); //初始化Jedis连接池,通常来讲JedisPool是单例的 JedisPool jedisPool = new JedisPool(poolConfig, "119.23.226.29", 6379); Jedis jedis = null; try { //1.从连接池获取jedis对象 jedis = jedisPool.getResource(); //2.执行操作 jedis.set("hello", "jedis"); System.out.println(jedis.get("hello")); } catch