Java 使用 Redis
安装
开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java。 Java 的安装配置可以参考我们的 Java 开发环境配置 接下来让我们安装 Java redis 驱动:
- 首先你需要下载驱动包 下载 jedis.jar,确保下载最新驱动包。
- 在你的 classpath 中包含该驱动包。
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
连接到 redis 服务
注意:
⊙ 防火墙关闭:ufw disable
⊙ IP 地址设置:#bind 127.0.0.1
Class : RedisJava
package redisdemo;import redis.clients.jedis.Jedis;
public class RedisJava {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接本地的 Redis 服务</span> Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.3.22"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"Connection to server sucessfully"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看服务是否运行</span> System.out.println("Server is running: " +<span style="color: rgba(0, 0, 0, 1)"> jedis.ping()); }
}
Console :
Connection to server sucessfully
Server is running: PONG
Redis Java String(字符串) 实例
Class : RedisStringJava
package redisdemo;import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("192.168.3.22");
System.out.println("Connection to server sucessfully");
//设置 redis 字符串数据
jedis.set("lime", "Redis tutorial");
// 获取存储的数据并输出
System.out.println("Stored string in redis::"+ jedis.get("lime"));
}
}
Console :
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis Java List(列表) 实例
Class :
package redisdemo;import java.util.List;
import redis.clients.jedis.Jedis;
public class RedisListJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("192.168.3.22");
System.out.println("Connection to server sucessfully");
//存储数据到列表中
jedis.lpush("tutorial-list", "lime");
jedis.lpush("tutorial-list", "oracle");
jedis.lpush("tutorial-list", "LimeOracle");
// 获取存储的数据并输出
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));
}
}
}
Console :
Connection to server sucessfully
Stored string in redis:: LimeOracle
Stored string in redis:: oracle
Stored string in redis:: lime
Redis Java Keys 实例
Class :
package redisdemo;import java.util.Iterator;
import java.util.Set;import redis.clients.jedis.Jedis;
public class RedisKeyJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("192.168.3.22");
System.out.println("Connection to server sucessfully");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取数据并输出</span> Set<String> keys = jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">); Iterator</span><String> it=<span style="color: rgba(0, 0, 0, 1)">keys.iterator() ; </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){ String key </span>=<span style="color: rgba(0, 0, 0, 1)"> it.next(); System.out.println(key); } }
}
Console :
Connection to server sucessfully
li
limeKey
tutorial-list
啦啦啦
啦啦啦