• Jedis 连接池的基本使用


    jedis直连

    每次操作都会创建一个jedis对象,执行完毕后关闭连接后释放,对应的就是一次Tcp连接。

    img

    jedis连接池

    预先生成一批jedis连接对象放入连接池中,当需要对redis进行操作时从连接池中借用jedis对象,操作完成后归还。这样jedis对象可以重复使用,避免了频繁创建socket连接,节省了连接开销。

    img

    方案对比

    img

    连接池简单使用

    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();
            }
        }
    }
    
  • 相关阅读:
    二叉树的序列化与反序列化
    寻找重复的子树
    [ABC216H] Random Robots
    Codeforces Round #741 (Div. 2)
    [Gym 102798K] Tree Tweaking
    CF798E Mike and code of a permutation
    CF1149E Election Promises
    [BZOJ 4311] 向量
    CF1268D Invertation in Tournament
    [做题笔记] 浅谈势能线段树在特殊区间问题上的应用
  • 原文地址:https://www.cnblogs.com/danhuang/p/12843596.html
Copyright © 2020-2023  润新知