Redis connection usage in Java development

Source: Internet
Author: User
Tags connection pooling

This post is only a note record of my Redis introductory study.
What is Redis? Simply put, it is a key-value type of database. About its advantages and disadvantages, I do not have a deep study and understanding, dare not to be raved.

Jedis:redis the official Preferred Java Client Development package.

Because Redis is installed in a Linux environment, we need to do the following before using it:
1. Install Virtual machine
2. Build Linux Environment
3. Installing Redis
4. Introduce the Jedis jar package in the Java project and add it to the project's build path

Once the preparation is done, Redis can be used in program development. As with other relational databases, the use of Redis can be divided into the following 4 steps:
1. Establish the connection
2. Save data
3. Access to Data
4. Release the connection

Like JDBC, Jedis also offers two connection modes: Singleton mode, connection pooling mode, code examples are as follows:

public class Jedisdemo {

@Test
/**
* Single case Mode connection
*/
public void Demo1 () {
1. Set the IP address and port
Jedis Jedis = new Jedis ("192.168.130.1", 6379);
2. Save data
Jedis.set ("Name1", "Java");
3. Read data
String value1 = Jedis.get ("name1");
System.out.println (value1);
4. Releasing Resources
Jedis.close ();
}

@Test
/**
* Connection Pool Mode connection
*/
public void Demo2 () {
1. Get the configuration object for the connection pool
Jedispoolconfig config = new Jedispoolconfig ();
2. Set the maximum number of connections
Config.setmaxtotal (30);
3. Set the maximum number of idle connections
Config.setmaxidle (10);
4. Get the connection pool
Jedispool Jedispool = new Jedispool (config, "192.168.130.1", 6379);
5. Get the Core object
Jedis Jedis = null;
try{
6. Get the link by connecting pools
Jedis = Jedispool.getresource ();
7. Save Data
Jedis.set ("name2", "Python");
8. Access to Data
String value2 = Jedis.get ("name2");
System.out.println (value2);
} catch (Exception e) {
E.printstacktrace ();
} finally{
9. Releasing Resources
if (Jedis! = null) {
Jedis.close ();
}
if (Jedispool! = null) {
Jedispool.close ();
}
}
}
  
@Test
public static void Main (string[] args) {
Jedisdemo Jedisdemo = new Jedisdemo ();
Jedisdemo.demo1 ();
Jedisdemo.demo2 ();
}
}

Redis in Java Development Connection 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.