Java Client with Redis Jedis__java

Source: Internet
Author: User
Tags connection pooling redis

Jedis provides a variety of modes of operation: Single connection mode, single connection pool mode, multi-machine distributed + connection pool. Prepare

jedis-2.5.2
Commons-pool2-2.2.jar Use single connection

This method is recommended only for use in the development environment for tuning.

Create a connection
String host = "192.168.56.102"; int port = 6379; Jedis client = new Jedis (host, Port);
Execute set instruction String result = Client.set ("key-string", "Hello, redis!"); System.out.println (String.Format ("Set instruction execution results:%s", result));
Execute get instruction String value = Client.get ("key-string"); System.out.println (String.Format ("Get instruction Execution Result:%s", value));

Run the above code, console output:

Set instruction execution Result: OK
Get instruction execution Result: Hello, redis! Using connection pooling

This approach applies to scenarios that use only a single Redis instance.

Generate Connection Pool configuration information
jedispoolconfig config = new Jedispoolconfig ();
Config.setmaxidle (ten);
Config.setmaxtotal (a);
Config.setmaxwaitmillis (3*1000);

Generate connection pool when application initialization
Jedispool pool = new Jedispool (config, "192.168.56.102", 6379);

During a business operation, get the connection from the connection pool
Jedis client = Pool.getresource ();
try {
    //execute instruction
    String result = Client.set ("key-string", "Hello, redis!");
    System.out.println (String.Format ("Set instruction execution results:%s", result));
    String value = Client.get ("key-string");
    System.out.println (String.Format ("Get instruction Execution Result:%s", value);
} catch (Exception e) {
    //Todo:handle Exception
} finally {
    //Business operation completed, return connection to connection pool
    if (null!= client) { C19/>pool.returnresource (client);
    }

the end of the try block//application is closed, releasing the connection pool resource
Pool.destroy ();

Run the above code, console output:

Set instruction execution Result: OK
Get instruction execution Result: Hello, redis! using connection pooling + distributed

In larger systems, there are often multiple Redis instances doing load balancing. The master-slave backup is also implemented, and when the main instance fails, it is switched to service from the instance.
Similar to memcached clients, Jedis also provides a way for client-side distributed operations, using a consistent hashing algorithm.

Generate a multiple-machine connection Information list
list<jedisshardinfo> shards = new arraylist<jedisshardinfo> ();
Shards.add (New Jedisshardinfo ("127.0.0.1", 6379));
Shards.add (New Jedisshardinfo ("192.168.56.102", 6379));

Generate Connection Pool configuration information
jedispoolconfig config = new Jedispoolconfig ();
Config.setmaxidle (ten);
Config.setmaxtotal (a);
Config.setmaxwaitmillis (3*1000);

Generate connection pool when application initialization
Shardedjedispool pool = new Shardedjedispool (config, shards);

During a business operation, get the connection from the connection pool
Shardedjedis client = Pool.getresource ();
try {
    //execute instruction
    String result = Client.set ("key-string", "Hello, redis!");
    System.out.println (String.Format ("Set instruction execution results:%s", result));
    String value = Client.get ("key-string");
    System.out.println (String.Format ("Get instruction Execution Result:%s", value);
} catch (Exception e) {
    //Todo:handle Exception
} finally {
    //Business operation completed, return connection to connection pool
    if (null!= client) { C23/>pool.returnresource (client);
    }

the end of the try block//application is closed, releasing the connection pool resource
Pool.destroy ();
Run the above code, console output:

Set instruction execution Result: OK
Get instruction execution Result: Hello, 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.