Jedis Client Implementation
Maven Pom File Join dependency
<Dependencies> <Dependency> <groupId>Redis.clients</groupId> <Artifactid>Jedis</Artifactid> <version>2.1.0</version> </Dependency> <Dependency> <groupId>Junit</groupId> <Artifactid>Junit</Artifactid> <version>4.8.2</version> <Scope>Test</Scope> </Dependency> </Dependencies>
Jedis Simple to use
/ * * Jedistest.java * */package COM.X.JAVA2000_WL; Import Org.junit.Before; Import Org.junit.Test; Import Redis.clients.jedis.Jedis; /** * Jedis Simple use * @author HTTP://BLOG.CSDN.NET/JAVA2000_WL * @version<b>1.0</b>*/public class Jedissimpletest {private Jedis Jedis; /** * Initialize connection *<BR>------------------------------<BR>*/@Before public void Beforeclass () {Jedis = new Jedis ("127.0.0.1"); Jedis.auth ("Java2000_wl"); }/** * Set new *<BR>------------------------------<BR>*/@Test public void Testset () {jedis.set ("blog", "Java2000_wl"); }/** * Get *<BR>------------------------------<BR>*/@Test public void Testget () {System.out.println (Jedis.get ("blog")); }/** * Modify key *<BR>------------------------------<BR>*/@Test public void Testrenamekey () {jedis.rename ("blog", "Blog_new"); }/** * Delete by key *<BR>------------------------------<BR>*/@Test public void Testdel () {Jedis.del ("blog_new"); }/** * Get all the keys *<BR>------------------------------<BR>*/@Test public void Testkeys () {System.out.println (Jedis.keys ("*")); } }
Using the Commons-pool connection pool
/** Jedispooltest.java*/ PackageCOM.X.JAVA2000_WL; ImportJava.util.ResourceBundle; ImportOrg.junit.Assert; ImportOrg.junit.BeforeClass; Importorg.junit.Test; ImportRedis.clients.jedis.Jedis; ImportRedis.clients.jedis.JedisPool; ImportRedis.clients.jedis.JedisPoolConfig; /*** Jedis Pool operation *@author HTTP://BLOG.CSDN.NET/JAVA2000_WL * @version<b>1.0</b>*/ Public classJedispooltest {Private StaticJedispool Jedispool; /*** initpoolconfig * <br>------------------------------<br> *@return */ Private Staticjedispoolconfig Initpoolconfig () {jedispoolconfig jedispoolconfig=NewJedispoolconfig (); //controls the maximum number of Jedis instances in a pool that have a status of idleJedispoolconfig.setmaxactive (1000); //Maximum number of objects that can remain idleJedispoolconfig.setmaxidle (300); //Timeout periodJedispoolconfig.setmaxwait (1000); //whether alidate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available;Jedispoolconfig.settestonborrow (true); //whether the validate operation is done in advance when the pool is also givenJedispoolconfig.settestonreturn (true); returnJedispoolconfig; } /*** Initialize Jedis connection pool * <br>------------------------------<br>*/@BeforeClass Public Static voidbefore () {Jedispoolconfig jedispoolconfig=Initpoolconfig (); //Property File read parameter informationResourceBundle bundle = Resourcebundle.getbundle ("Redis_config"); String Host= Bundle.getstring ("Redis.host"); intPort = integer.valueof (bundle.getstring ("Redis.port")); intTimeout = integer.valueof (bundle.getstring ("Redis.timeout")); String Password= Bundle.getstring ("Redis.password"); //Constructing Connection PoolsJedispool =NewJedispool (Jedispoolconfig, host, port, timeout, password); } @Test Public voidTestset () {Jedis Jedis=NULL; //get a Jedis instance from the pool Try{Jedis=Jedispool.getresource (); Jedis.set ("Blog_pool", "JAVA2000_WL"); } Catch(Exception e) {//destroying ObjectsJedispool.returnbrokenresource (Jedis); Assert.fail (E.getmessage ()); } finally { //It will also go to the connection poolJedispool.returnresource (Jedis); }} @Test Public voidTestget () {Jedis Jedis=NULL; Try { //get a Jedis instance from the poolJedis =Jedispool.getresource (); System.out.println (Jedis.get ("Blog_pool")); } Catch(Exception e) {//destroying ObjectsJedispool.returnbrokenresource (Jedis); Assert.fail (E.getmessage ()); } finally { //It will also go to the connection poolJedispool.returnresource (Jedis); } } }
Remember to destroy object Returnbrokenresource when an exception occurs, and then connect Returnresource when you are finished using it.
Java basic operations for Redis-1