Use of Redis Commons-pool

Source: Internet
Author: User
Tags delete key

To increase the pool configuration file redis-pool.properties:

# Maximum number of objects that can hold the Idel state redis.pool.maxidle=200# maximum wait time when no objects are returned in the pool redis.pool.maxwait=1000  # If the Borrow object method is called, does the validation check redis.pool.testonborrow=true# when calling return When the object method is checked for validity redis.pool.testonreturn=true#IPredis.ip=127.0.0.1#  Portredis.port=6379

Redisapp.java

 PackageCom.yzl;ImportJava.util.ResourceBundle;ImportOrg.apache.log4j.Logger;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;/*** Crud operation of Reids Jedis * *@authorYangzhilong *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/ Public classRedisapp { PublicLogger log = Logger.getlogger ( This. GetClass ()); Private StaticJedispool Pool; Private StaticResourceBundle Bundle; Static{Bundle= Resourcebundle.getbundle ("Redis-pool"); if(Bundle = =NULL){            //Suppose the direct throw exception must be processed, otherwise the compilation will not pass            Throw NewIllegalArgumentException ("Redis-pool.properties file is not found"); } jedispoolconfig config=NewJedispoolconfig (); //set some parameters of the pool, optional, detailed configuration items see Genericobjectpoolconfig classConfig.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"))); //using the default configuration, you can initialize the pool using the following methods//pool = new Jedispool (bundle.getstring ("Redis.ip"), Integer.valueof (bundle.getstring ("Redis.port" ));Pool =NewJedispool (config, bundle.getstring ("Redis.ip"), Integer.valueof (bundle.getstring ("Redis.port")))); }        /*** Function Description: <br> * crud operation Hello World *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/     Public voidcrudfromrediswidthsimple () {Jedis Jedis=NewJedis (bundle.getstring ("Redis.ip"), Integer.valueof (bundle.getstring ("Redis.port")))); Log.info ("Get connection with simple");        Crudmethod (Jedis); //Close ConnectionJedis.close (); }        /*** * use Common-pool to operate Redis * *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/     Public voidCrudfromrediswidthpool () {Jedis Jedis=Pool.getresource (); Log.info ("Get connection from the pool, Obect is:" +Jedis);        Crudmethod (Jedis); //Release LinkPool.returnresourceobject (Jedis); }        /*** * CRUD Basic operating unit * *@paramJedis *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/    Private voidCrudmethod (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  PackageCom.yzl;2 3 Importorg.junit.Test;4 5 /**6 * Redisapp Test class7  *8  * @authorYangzhilong9  * @see[Related Classes/methods] (optional)Ten  * @since[Product/module version] (optional) One  */ A  Public classRedisapptest { -      - @Test the      Public voidcrudfromrediswidthsimpletest () { -Redisapp app =NewRedisapp (); -App.crudfromrediswidthsimple (); -     } +      - @Test +      Public voidcrudfromrediswidthpooltest () { ARedisapp app =NewRedisapp (); at App.crudfromrediswidthpool (); -     } -}

Results of running Unit tests:

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

Use of Redis Commons-pool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.