06. Jedis

Java语言连接Redis服务的工具:

  • Jedis
  • SpringData Redis
  • Lettuce

Jedis操作Redis

  1. 引入对应的依赖
1
2
3
4
5
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
  1. 编写程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
public void testJedis() {
// 1.连接reids
Jedis jedis = new Jedis("127.0.0.1", 6379);

// 2.操作redis
// jedis的方法名和reids对应的命令名完全一样
jedis.set("name", "zhangsan");
String name = jedis.get("name");
System.out.println(name);

// 3.关闭连接
jedis.close();
}

使用Jedis连接池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private JedisPool jp = null;

static {
JedisPoolConfig jpc = new JedisPoolConfig();
jpc.setMaxTotal(30);
jpc.setMaxIdle(10);
String host = "127.0.0.1";
int port = 6379;
jp = new JedisPool(jpc, host, port);
}

public static Jedis getJedis() {
return jp.getResource();
}

06. Jedis
http://binbo-zappy.github.io/2024/12/07/Redis/06-Jedis/
作者
Binbo
发布于
2024年12月7日
许可协议