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!