Using jedis2.8.0 to connect to Redis

Source: Internet
Author: User

Download the latest Jedis client jedis2.8.0, found on the internet to find Jedis use the connection Pool tool class, get to find are the lower version of the Jedis notation:

Returnresource ();

Returnbrokenresource ();

These two methods expired ~, can not find the latest version of the wording, pretending to be attempted ~ ~

So went to see the source code, found that the source of comments written very clearly:

/**   * @deprecatedstarting from Jedis 3.0 This method won't be exposed. * Resource cleanup should is done using@see {@linkredis.clients.jedis.jedis#close ()}*/@Override @Deprecated Public voidReturnresource (FinalJedis Resource) {    if(Resource! =NULL) {      Try{resource.resetstate ();      Returnresourceobject (Resource); } Catch(Exception e) {Returnbrokenresource (Resource); Throw NewJedisexception ("Could not return the resource to the pool", E); }    }  }

With the close () method, you can follow the close () method:

  @Override  public  void   close () { if  (dataSource! = Span style= "color: #0000ff;" >null   if   (Client.isbroken ()) { this . Datasource.returnbrokenresource (this  );  else   { this . Datasource.returnresource (this        );    }}  else   {client.close (); }  }

DataSource for the definition of the connection pool, if the connection pool is not empty, do some return connection operation, Jedis connection pool design is based on Org.apache.commons.pool2, these two methods are not followed, but is to change the value of the connection property to be available.

If the connection pool is empty, disconnect from the Redis service directly

So when it comes to writing some basic classes, it should look like this:

 public  <T> void   set (T item) {Jedis Jedis  =null  ;  try   {Jedis  = Jedisclient.getjedis ();            String v  = Cachekeyutil.getdefaultkey (item);        Jedis.set (V, iserializer.serialize (item));  catch   (Exception e) {Logger.warn (        /span> "Set" , item, e);  finally   {
if (null! = Jsdis) {
"/ span> jedis.close ();
}                    }    }

Borrow someone else's, must also ~ ~ ~

Using jedis2.8.0 to connect to Redis

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.