Jedis官网快速跳转
Jedis 的 Github 官方网站跳转:redis/jedis
1. Jedis的基本使用
1.1 引入Jedis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
另外,我们引入 junit5 的单元测试依赖:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
1.2 创建Jedis对象,建立连接
在这里我们使用 junit5 进行单元测试:
/**
* @BeforeEach:在每个测试方法之前都会执行,这里是初始化 jedis 对象
*/
@BeforeEach
void setUp() {
// 1. 建立连接
jedis = new Jedis("192.168.200.130", 6379);
// 2. 设置密码
jedis.auth("123456");
// 3. 选择库
jedis.select(0);
}
1.3 使用jedis,方法名和redis命令一致
@Test
public void testString() {
// 存入String数据
String result = jedis.set("name", "张三");
System.out.println("result:" + result);
// 获取数据
String name = jedis.get("name");
System.out.println("name:" + name);
}
@Test
public void testHash() {
jedis.hset("user:1", "name", "jack");
jedis.hset("user:1", "age", "21");
Map<String, String> map = jedis.hgetAll("user:1");
map.forEach((k, v) -> {
System.out.println("key:" + k + ", value:" + v);
});
}
测试结果:
1.4 释放资源
/**
* @AfterEach:在每个测试方法之后都会执行,这里是销毁 jedis 对象
*/
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
2. Jedis连接池
Jedis本身是线程不安全的,并且频繁的创建和销毁连接有性能消耗,因此建议使用
Jedis 连接池
代替 Jedis 的直连方式。
创建 JedisConnectionFactory
工具类,从该工具类中获取连接:
package org.example.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
public class JedisConnectionFactory {
private static final JedisPool jedisPool;
static {
// 配置连接池
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//连接池最大连接数
jedisPoolConfig.setMaxTotal(8);
// 最大空闲连接
jedisPoolConfig.setMaxIdle(8);
// 最小空闲连接(当一段时间后没人访问,就会降低到最小连接)
jedisPoolConfig.setMinIdle(0);
// 等待连接池中连接时长
jedisPoolConfig.setMaxWait(Duration.ofSeconds(10));
// 创建连接池对象
jedisPool = new JedisPool(
jedisPoolConfig, //jedis连接池配置对象
"192.168.200.130", //redis的ip地址(虚拟机或本机或云服务器)
6379, //端口号
1000, //超时时间
"123456"); //密码
}
/**
* 调用该方法返回 jedis 对象
* @return
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
接下来就不需要自己创建 Jedis 对象并进行连接了,直接调用该工具类获取连接即可:
package org.example;
import org.example.util.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import java.util.Map;
public class JedisTest {
private Jedis jedis;
/**
* @BeforeEach:在每个测试方法之前都会执行,这里是初始化 jedis 对象
*/
@BeforeEach
void setUp() {
// 1. 获取连接
jedis = JedisConnectionFactory.getJedis();
// 2. 选择库
jedis.select(0);
}
/**
* 测试String数据类型的使用
*/
@Test
public void testString() {
// 存入String数据
String result = jedis.set("name", "lisi");
System.out.println("result:" + result);
// 获取数据
String name = jedis.get("name");
System.out.println("name:" + name);
}
/**
* 测试 Hash 数据类型的使用
*/
@Test
public void testHash() {
jedis.hset("user:1", "name", "jack");
jedis.hset("user:1", "age", "21");
Map<String, String> map = jedis.hgetAll("user:1");
map.forEach((k, v) -> {
System.out.println("key:" + k + ", value:" + v);
});
}
/**
* @AfterEach:在每个测试方法之后都会执行,这里是销毁 jedis 对象
*/
@AfterEach
void tearDown() {
if (jedis != null) {
// 将该连接返回到连接池中
jedis.close();
}
}
}