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 (Exception e) {
e.printStackTrace();
} finally{
//如果使用JedisPool,那么close操作不是关闭连接,代表归还连接池
if(jedis != null){
jedis.close();
}
}
}
}

连接池封装使用

public class RedisPool {
//jedis连接池,使用static保证连接池在tomcat启动时就加载出来
private static JedisPool pool;
//连接池中的最大连接数
private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
//连接池中的最大空闲连接数
private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "20"));
//连接池中的最小空闲连接数
private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "0")); //借用连接时是否进行验证
private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow", "true"));
//返还连接时是否进行验证
private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return", "true")); private static String redisIp = PropertiesUtil.getProperty("redis.ip");
private static Integer redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis.port"));
private static String password = PropertiesUtil.getProperty("redis.password"); //初始化连接池,只会调用一次
private static void initPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
//连接池耗尽的时候,是否阻塞,false会抛出异常,true会阻塞直到超时抛出异常,默认为true
config.setBlockWhenExhausted(true);
//超时时间2s
pool = new JedisPool(config, redisIp, redisPort, 1000*2, password);
} static {
initPool();
} //从连接池中借用一个实例
public static Jedis getJedis() {
return pool.getResource();
}
}
public class RedisPoolUtil {
public static String set(String key, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = RedisPool.getJedis();
result = jedis.set(key, value);
} catch (Exception e) {
log.error("set key:{} value:{} error", key, value, e);
} finally {
shutdown(jedis);
}
return result;
} public static void shutdown(Jedis jedis) {
if (null != jedis) {
// close会判断连接是否破损而执行对应的回收操作
jedis.close();
}
}
}

Jedis 连接池的基本使用的更多相关文章

  1. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  2. Jedis连接池

    jedis是官方首选的java客户端开发包 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Ja ...

  3. 详解Jedis连接池报错处理

    在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...

  4. 为什么要用Jedis连接池+浅谈jedis连接池使用

    为什么要使用Jedis连接池 Redis作为缓存数据库理论上和MySQL一样需要客户端和服务端建立起来连接进行相关操作,使用MySQL的时候相信大家都会使用一款开源的连接池,例如C3P0.因为直连会消 ...

  5. Jedis与Jedis连接池

    1.Jedis简介 实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis, 对于主流语言,Redis都提供了对应的客户端: https://redis.io/clients 2. ...

  6. 三、redis学习(jedis连接池)

    一.jedis连接池 二.jedis连接池+config配置文件 三.jedis连接池+config配置文件+util工具类 util类 public class JedisPoolUtils { / ...

  7. Java Redis系列3(Jedis的使用+jedis连接池技术)

    Jedis的使用 什么是Jedis? 一款Java操作redis数据库的工具 使用步骤 1.下载redis所需的java包 2.使用步骤 import org.junit.Test; public c ...

  8. jedis连接池详解(Redis)

    转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...

  9. Jedis连接池使用

    构建redis连接池,返还到连接池 private static JedisPool jedisPool = null; private static Jedis jedis; static { je ...

随机推荐

  1. 【编程思想】【设计模式】【行为模式Behavioral】Specification

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/specification.py #!/usr/bin/e ...

  2. jQuery - 按回车键触发跳转

    键盘事件有三种: keyup:按键按下去,抬上来后,事件才生效 (推荐) keydown:按键按下去就生效 keypress:与 keydown 事件类似,当按钮被按下时,会发生该事件,与 keydo ...

  3. 【力扣】两个数组的交集 II

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2]输出:[2,2]示例 2: 输入:nums1 = [4,9,5], nu ...

  4. NGNIX 开启socket分发的使用配置

    worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/err ...

  5. Netty 编解码奥秘

    Netty中编解码 Netty 的解码器有很多种,比如基于长度的,基于分割符的,私有协议的.但是,总体的思路都是一致的. 拆包思路:当数据满足了 解码条件时,将其拆开.放到数组.然后发送到业务 han ...

  6. Redis持久化 aof和rdb的原理配置

    目录 一.介绍 二.RDB持久化(全量写入) rdb原理 rdb模式 rdb触发情况 rdb优势和劣势 rdb文件配置 rdb命令配置 rdb数据恢复 三.AOF持久化(增量写入) aof原理 aof ...

  7. Kerboros 认证

    转:Kerberos介绍(全)

  8. [BUUCTF]REVERSE——reverse3

    reverse3 附件 步骤: 例行查壳儿,32位程序,无壳儿 32位ida载入,shift+f12检索程序里的字符串,得到了有关flag的提示,而且看到了ABCDE--78这种字符串,猜测存在bas ...

  9. ViewModel的创建

    ViewModel的创建 ViewModel本身只是ViewModel这个类的子类: class MainViewModel: ViewModel() { } 在屏幕旋转UI重建的时候, 它是如何拥有 ...

  10. ReentrantLock可重入锁——源码详解

    开始这篇博客之前,博主默认大家都是看过AQS源码的~什么居然没看过猛戳下方 全网最详细的AbstractQueuedSynchronizer(AQS)源码剖析(一)AQS基础 全网最详细的Abstra ...