標籤:
增加池的設定檔redis-pool.properties:
#最大能夠保持idel狀態的對象數redis.pool.maxIdle=200#當池內沒有返回對象時,最大等待時間redis.pool.maxWait=1000#當調用borrow Object方法時,是否進行有效性檢查redis.pool.testOnBorrow=true#當調用return Object方法時,是否進行有效性檢查redis.pool.testOnReturn=true#IPredis.ip=127.0.0.1#Portredis.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 @Test15 public void crudFromRedisWidthSimpleTest(){16 RedisApp app = new RedisApp();17 app.crudFromRedisWidthSimple();18 }19 20 @Test21 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:[email protected]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 jedis4 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
redis之使用commons-pool