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