jedis连接池

来源:互联网 发布:知乎邮箱注册 编辑:程序博客网 时间:2024/05/18 00:06
<strong>java 自己实现的jedis连接池方式</strong>
package tigase.server.beibei.redis;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class Redis {private static JedisPool pool = null;/***** * 得到  jedis 对象 * @return */public static Jedis getJedis(){inintJedisPool();return pool.getResource();}        /**     * 构建redis连接池     */    public static void inintJedisPool() {        if (pool == null) {            JedisPoolConfig config = new JedisPoolConfig();            //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;            //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。            config.setMaxActive(50);            //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。            config.setMaxIdle(50);            //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;            config.setMaxWait((long)10000);            //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;            config.setTestOnBorrow(true);                        pool = new JedisPool(config, "redis.catpaw.local", 6379);        }    }    /**     * 返还到连接池     * 使用完成后的jedis 必须要从新放回的连接池里,否则通过getResource得到的instance的缓冲区可能还存在数据,出现问题     * @param pool      * @param redis     */    public static void returnResource(JedisPool pool, Jedis redis) {        if (redis != null) {            pool.returnResource(redis);        }    }        /**     * 获取数据     *      * @param key     * @return     */    public static String get(String key){        String value = null;                Jedis jedis = getJedis();        try {            value = jedis.get(key);        } catch (Exception e) {            //释放redis对象            pool.returnBrokenResource(jedis);            e.printStackTrace();        } finally {            //返还到连接池            returnResource(pool, jedis);        }        return value;    }    /**     * 获取数据     *      * @param key     * @return     */    public static String hget(String key,String field){        String value = null;                Jedis jedis = getJedis();        try {            value = jedis.hget(key, field);        } catch (Exception e) {            //释放redis对象            pool.returnBrokenResource(jedis);            e.printStackTrace();        } finally {            //返还到连接池            returnResource(pool, jedis);        }        return value;    }        public static void main(String[] args) {System.out.println(hget("UI_789", "token"));}}
Spring 配置jedis连接池
<pre name="code" class="java"><!-- redis --><!-- POOL配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxActive" value="50" /><property name="maxIdle" value="50" /><property name="maxWait" value="10000" /><property name="testOnBorrow" value="true" /></bean><bean id="jedisPool" class="redis.clients.jedis.JedisPool"><constructor-arg index="0" ref="jedisPoolConfig" /><constructor-arg index="1" value="redis.catpaw.local" /><constructor-arg index="2" value="6379" /></bean><bean id="redis" class="com.cat.cache.SharedHashRedis">    <!-- <property name="app" value="icore"></property> -->    <constructor-arg ref="jedisPool"/>    </bean>



0 0
原创粉丝点击