Java语言连接Redis服务的工具:
- Jedis
- SpringData Redis
- Lettuce
Jedis操作Redis
- 引入对应的依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
编写程序
@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连接池
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();
}