maven依赖包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
源码:
package StudyPro.base; import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class TestSample {
// Jedis jedis = new Jedis("10.15.1.19",16379); // 简单连接
@Test
public void test() {
Jedis jedis = new Jedis("10.15.1.19", 16379);
jedis.auth("wC3Xo8E5mlmgMb");
jedis.set("a", "bb");
System.out.println(jedis.get("a"));
jedis.close();
} // 使用连接池
@Test
public void testPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(20);
poolConfig.setMinIdle(10);
poolConfig.setMaxTotal(30);
poolConfig.setMaxWaitMillis(3000);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
JedisPool jedisPool = new JedisPool("10.15.1.19", 16379);
Jedis jedis = jedisPool.getResource();
jedis.auth("wC3Xo8E5mlmgMb");
System.out.println(jedis.get("a"));
jedis.close();
jedisPool.close();
} }