Redis 简明教程

Redis - Java

在开始在 Java 程序中使用 Redis 之前,您需要确保计算机上安装了 Redis Java 驱动程序和 Java。您可以查看我们的 Java 教程,了解如何在您的计算机上安装 Java。

Installation

现在,让我们看看如何设置 Redis Java 驱动程序。

  1. 您需要从路径 Download jedis.jar 下载 jar。务必下载最新版本。

  2. 您需要将 jedis.jar 包含在类路径中。

Connect to Redis Server

import redis.clients.jedis.Jedis;

public class RedisJava {
   public static void main(String[] args) {
      //Connecting to Redis server on localhost
      Jedis jedis = new Jedis("localhost");
      System.out.println("Connection to server sucessfully");
      //check whether server is running or not
      System.out.println("Server is running: "+jedis.ping());
   }
}

现在,让我们编译并运行上述程序以测试与 Redis 服务器的连接。您可以根据需要更改路径。我们假设 jedis.jar 的当前版本可用于当前路径中。

$javac RedisJava.java
$java RedisJava
Connection to server sucessfully
Server is running: PONG

Redis Java String Example

import redis.clients.jedis.Jedis;

public class RedisStringJava {
   public static void main(String[] args) {
      //Connecting to Redis server on localhost
      Jedis jedis = new Jedis("localhost");
      System.out.println("Connection to server sucessfully");
      //set the data in redis string
      jedis.set("tutorial-name", "Redis tutorial");
      // Get the stored data and print it
      System.out.println("Stored string in redis:: "+ jedis.get("tutorial-name"));
   }
}

现在,让我们编译并运行上述程序。

$javac RedisStringJava.java
$java RedisStringJava
Connection to server sucessfully
Stored string in redis:: Redis tutorial

Redis Java List Example

import redis.clients.jedis.Jedis;

public class RedisListJava {
   public static void main(String[] args) {

      //Connecting to Redis server on localhost
      Jedis jedis = new Jedis("localhost");
      System.out.println("Connection to server sucessfully");

      //store data in redis list
      jedis.lpush("tutorial-list", "Redis");
      jedis.lpush("tutorial-list", "Mongodb");
      jedis.lpush("tutorial-list", "Mysql");
      // Get the stored data and print it
      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));
      }
   }
}

现在,让我们编译并运行上述程序。

$javac RedisListJava.java
$java RedisListJava
Connection to server sucessfully
Stored string in redis:: Redis
Stored string in redis:: Mongodb
Stored string in redis:: Mysql

Redis Java Keys Example

import redis.clients.jedis.Jedis;

public class RedisKeyJava {
   public static void main(String[] args) {

      //Connecting to Redis server on localhost
      Jedis jedis = new Jedis("localhost");
      System.out.println("Connection to server sucessfully");
      //store data in redis list
      // Get the stored data and print it
      List<String> list = jedis.keys("*");

      for(int i = 0; i<list.size(); i++) {
         System.out.println("List of stored keys:: "+list.get(i));
      }
   }
}

现在,让我们编译并运行上述程序。

$javac RedisKeyJava.java
$java RedisKeyJava
Connection to server sucessfully
List of stored keys:: tutorial-name
List of stored keys:: tutorial-list