HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Before you start using Redis in Java, we need to make sure you have installed the Java redis redis services and drivers on your machine and normal use Java. Java installation configuration can refer to our Java development environment configuration Let's install Java redis drive:
import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { //连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); //查看服务是否运行 System.out.println("Server is running: "+jedis.ping()); } }
Java compile the above procedures to ensure that the driver package path is correct.
$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) { //连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); //设置 redis 字符串数据 jedis.set("w3resourcekey", "Redis tutorial"); // 获取存储的数据并输出 System.out.println("Stored string in redis:: "+ jedis.get("w3resourcekey")); } }
Compile the above program.
$javac RedisStringJava.java $java RedisStringJava Connection to server sucessfully Stored string in redis:: Redis tutorial
import redis.clients.jedis.Jedis; public class RedisListJava { public static void main(String[] args) { //连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); //存储数据到列表中 jedis.lpush("tutorial-list", "Redis"); jedis.lpush("tutorial-list", "Mongodb"); jedis.lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 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 sucessfully Stored string in redis:: Redis Stored string in redis:: Mongodb Stored string in redis:: Mysql
import redis.clients.jedis.Jedis; public class RedisKeyJava { public static void main(String[] args) { //连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); // 获取数据并输出 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 sucessfully List of stored keys:: tutorial-name List of stored keys:: tutorial-list