4、redis之使用commons-pool
增加池的配置文件redis-pool.properties:
#最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.pool.maxWait=1000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #IP redis.ip=127.0.0.1 #Port redis.port=6379
RedisApp.java
package com.yzl; import java.util.ResourceBundle; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Reids之jedis的CRUD操作 * * @author yangzhilong * @see [相关类/方法](可选) * @since [产品/模块版本] (可选) */ public class RedisApp { public Logger log = Logger.getLogger(this.getClass()); private static JedisPool pool; private static ResourceBundle bundle; static{ bundle = ResourceBundle.getBundle("redis-pool"); if(bundle == null){ //假设直接抛出Exception咋必须进行处理,不然编译不会通过 throw new IllegalArgumentException("redis-pool.properties file is not found"); } JedisPoolConfig config = new JedisPoolConfig(); //设置pool的一些参数,可选,详细配置项参见GenericObjectPoolConfig类 config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle"))); config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait"))); config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"))); //使用默认配置时可以使用如下方法初始化池 //pool = new JedisPool(bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port"))); pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port"))); } /** * * 功能描述: <br> * CRUD操作之hello world * * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public void crudFromRedisWidthSimple(){ Jedis jedis = new Jedis(bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port"))); log.info("get connection with simple "); crudMethod(jedis); //关闭连接 jedis.close(); } /** * * 使用common-pool操作redis * * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public void crudFromRedisWidthPool(){ Jedis jedis = pool.getResource(); log.info("get connection from pool , obect is:" + jedis); crudMethod(jedis); //释放链接 pool.returnResourceObject(jedis); } /** * * crud基本操作单元 * * @param jedis * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ private void crudMethod(Jedis jedis){ log.info("insert value to redis~~~"); jedis.set("name", "hello jedis"); log.info("get value from redis, value:" + jedis.get("name")); log.info("delete key from redis~~~"); jedis.del("name"); log.info("get value from redis, value:" + jedis.get("name")); } }
RedisAppTest.java
1 package com.yzl; 2 3 import org.junit.Test; 4 5 /** 6 * RedisApp的测试类 7 * 8 * @author yangzhilong 9 * @see [相关类/方法](可选) 10 * @since [产品/模块版本] (可选) 11 */ 12 public class RedisAppTest { 13 14 @Test 15 public void crudFromRedisWidthSimpleTest(){ 16 RedisApp app = new RedisApp(); 17 app.crudFromRedisWidthSimple(); 18 } 19 20 @Test 21 public void crudFromRedisWidthPoolTest(){ 22 RedisApp app = new RedisApp(); 23 app.crudFromRedisWidthPool(); 24 } 25 }
运行单元测试的结果:
1 2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get connection from pool , obect is:redis.clients.jedis.Jedis@cee47f1 2 2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] insert value to redis~~~ 3 2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get value from redis, value:hello jedis 4 2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] delete key from redis~~~ 5 2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get value from redis, value:null