Try Redis (iv)-java client Jedis use

Source: Internet
Author: User
Tags redis server

Different computer languages have their own clients for Redis.

The official website also lists the client's http://www.redis.io/clients

The Java client is as follows:


Feeling the client is nothing more than making connections and stitching commands. So don't bother to compare which is good, choose the first one. Generally the first one should be good.

Choose Jedis and discover that his code is GITHUB-hosted, open-source. Address is Https://github.com/xetorthio/jedis

Find Maven Dependency

<dependency>    <groupId>redis.clients</groupId>    <artifactid>jedis</artifactid >    <version>2.6.0</version>    <type>jar</type>    <scope>compile</ Scope></dependency>

Copy the above XML into Pom.xml (Maven dependency)


found that both the Jedis-2.6.0.jar and Commons-pool2-2.0.jar packages were added. One should be the core package, a Appache pool package, I guess the client connection pool is used. The concrete still has to go down.

So far, the preparation is OK. A simple example of running is as follows:

Jedis Jedis = new Jedis ("10.13.145.75", 6379); Default port 6379jedis.set ("Key", "HelloWorld"); String result = Jedis.get ("HelloWorld"); SYSTEM.OUT.PRINTLN (result);

Note that I am here to report a connection timeout.


The discovery is caused by the Redis server-side firewall.

Service Iptables Stop
Shut down the firewall, and then execute the code. Ok. It's done.


The code is very simple, directly look at the Jedis source code is how to implement it.

When calling Jedis.set ("key", "HelloWorld"); The time


In Jedis.class , a string set (final string key, String value) is called

Where clients are client objects

/**     * Set The string value as value of the key. The string can ' t be longer than     * 1073741824 bytes (1 GB).     * <p>     * Time Complexity:o (1)     *      * @param key     * @param value     * @return Status code reply     */< C11/>public string Set (Final String key, string value) {Checkisinmulti (); Client.set (key, value); return Client.getstatuscodereply ();    }

In the client.class

    public void set (final string key, final string value) {Set (Safeencoder.encode (key), Safeencoder.encode (value));    }
where Safeencoder.encode (String) is the string that installs the Utf-8 character set and turns it into a byte[] array.

Then the equivalent of calling the parent class

binaryclient.class public void set (final byte[] key, final byte[] value)


public void set (final byte[] key, final byte[] value) {SendCommand (Command.set, key, value);    }

The SendCommand in connection.class is the entry for all sent commands. command is an enumeration object

Protected Connection SendCommand (final Command cmd, final byte[] ... args) {try {    connect ();    Protocol.sendcommand (OutputStream, cmd, args);    pipelinedcommands++;    return this;} catch (Jedisconnectionexception ex) {    //any and exceptions related to connection?    Broken = true;    Throw ex;}    }
The Connect () method is the code of the traditional socket-connected server


Protocol.class

public static void SendCommand (final redisoutputstream OS,    Final command command, Final byte[] ... args) {SendCommand (OS, Command.raw, args);    }
SendCommand is the exit of all sent commands. Send commands directly to Redis-server. The byte[] byte of Set key HelloWorld is sent to Redis-server.

In order to prove his guess, deliberately grabbed a bag.


The discovery was indeed as it had guessed.


As for the get command, it is similar. Just the last call.

Client.sendcommand (Protocol.Command.GET, key);

Grabbed the bag also looked down. A GET key was requested when a request was made to the server

The following messages are captured:



The redis-server returned is Hello world.


The following messages are captured:




Finally, post a Jedis class diagram , probably painted.



Try Redis (iv)-java client Jedis use

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.