<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
void setUp() {
jedis = new Jedis("255.255.255.255", 6379);
jedis.auth("123456");
jedis.select(0);
}
@Test
public void demo1() {
String result = jedis.set("name", "虎哥");
System.out.println("result=" + result);
String name = jedis.get("name");
System.out.println("name=" + name);
}
@Test
public void demo2() {
jedis.hset("user:1", "name", "Jack");
jedis.hset("user:1", "age", "21");
Map<String, String> map = jedis.hgetAll("user:1");
System.out.println(map);
}
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
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
void setUp() {
jedis = new Jedis("255.255.255.255", 6379);
jedis.auth("123456");
jedis.select(0);
}
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
@Test
public void demo1() {
String result = jedis.set("name", "虎哥");
System.out.println("result=" + result);
String name = jedis.get("name");
System.out.println("name=" + name);
}
@Test
public void demo2() {
jedis.hset("user:1", "name", "Jack");
jedis.hset("user:1", "age", "21");
Map<String, String> map = jedis.hgetAll("user:1");
System.out.println(map);
}
}
工具下载地址:https://github.com/lework/RedisDesktopManager-Windows/releases
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisConnectionFactory {
private static final JedisPool jedisPool;
static {
//配置连接池
JedisPoolConfig poolConfig = new JedisPoolConfig();
//最大连接
poolConfig.setMaxTotal(8);
//最大空闲连接
poolConfig.setMaxIdle(8);
//最小空闲连接
poolConfig.setMinIdle(8);
//设置最长等待时间,单位ms
poolConfig.setMaxWaitMillis(1000);
//创建连接池对象
jedisPool = new JedisPool(poolConfig, "8.134.216.251", 6379, 1000, "123456");
}
//获取Jedis对象
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
void setUp() {
//直接获取连接
//jedis = new Jedis("8.134.216.251", 6379);
//jedis.auth("123456");
//连接池获取连接
jedis = JedisConnectionFactory.getJedis();
jedis.select(0);
}