This article describes the method of using Redis in Java. Share it for your reference, as follows:
Install
Before starting to use Redis in Java, we need to make sure that the redis service and Java redis driver are installed and that Java can be used normally on your machine. For Java installation and configuration, please refer to our Java development environment configuration. Next, let us install the Java redis driver:
First of all, you need to download the driver package, download jedis.jar, and make sure to download the latest driver package.
Include this driver package in your classpath.
Connect to the redis service
import redis.clients.jedis.Jedis;public class RedisJava {public static void main(String[] args) {//Connect the local Redis service Jedis jedis = new Jedis("localhost");System.out.println("Connection to server successfully");// Check whether the service runs System.out.println("Server is running: "+jedis.ping());}}Compile the above Java program to ensure that the path to the driver package is correct.
$javac RedisJava.java
$java RedisJava
Connection to server successfully
Server is running: PONG
Redis Java String Example
Redis Java String (String) instance
import redis.clients.jedis.Jedis;public class RedisStringJava {public static void main(String[] args) {//Connect local Redis service Jedis jedis = new Jedis("localhost");System.out.println("Connection to server successfully");//Set redis string data jedis.set("w3ckey", "Redis tutorial");// Get the stored data and output System.out.println("Stored string in redis:: "+ jedis.get("w3ckey"));}}Compile the above program
$javac RedisStringJava.java
$java RedisStringJava
Connection to server successfully
Stored string in redis:: Redis tutorial
Redis Java List (list) instance
import redis.clients.jedis.Jedis;public class RedisListJava {public static void main(String[] args) {//Connect local Redis service Jedis jedis = new Jedis("localhost");System.out.println("Connection to server successfully");//Storage data in the list jedis.lpush("tutorial-list", "Redis");jedis.lpush("tutorial-list", "Mongodb");jedis.lpush("tutorial-list", "Mysql");// Get the stored data and output List<String> list = jedis.lrange("tutorial-list", 0 ,5);for(int i=0; i<list.size(); i++) {System.out.println("Stored string in redis:: "+list.get(i));}}}Compile the above program
$javac RedisListJava.java
$java RedisListJava
Connection to server successfully
Stored string in redis:: Redis
Stored string in redis::Mongodb
Stored string in redis:: Mysql
Redis Java Keys instance
import redis.clients.jedis.Jedis;public class RedisKeyJava {public static void main(String[] args) {//Connect local Redis service Jedis jedis = new Jedis("localhost");System.out.println("Connection to server successfully");// Get data and output List<String> list = jedis.keys("*");for(int i=0; i<list.size(); i++) {System.out.println("List of stored keys:: "+list.get(i));}}}Compile the above program
$javac RedisKeyJava.java
$java RedisKeyJava
Connection to server successfully
List of stored keys:: tutorial-name
List of stored keys::tutorial-list
For more information about Java related content, please check out the topics of this site: "Java+MySQL database programming summary", "Java operation Excel skills summary", "Java data structure and algorithm tutorial", "Java file and directory operation skills summary" and "Java operation DOM node skills summary"
I hope this article will be helpful to everyone's Java programming.