maven依赖包:
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.9.0</version> 5 </dependency> 6 <dependency> 7 <groupId>org.apache.commons</groupId> 8 <artifactId>commons-pool2</artifactId> 9 <version>2.6.0</version> 10 </dependency>
源码:
1 package StudyPro.base; 2 3 import org.junit.Test; 4 import redis.clients.jedis.Jedis; 5 import redis.clients.jedis.JedisPool; 6 import redis.clients.jedis.JedisPoolConfig; 7 8 public class TestSample { 9 // Jedis jedis = new Jedis("10.15.1.19",16379); 10 11 // 简单连接 12 @Test 13 public void test() { 14 Jedis jedis = new Jedis("10.15.1.19", 16379); 15 jedis.auth("wC3Xo8E5mlmgMb"); 16 jedis.set("a", "bb"); 17 System.out.println(jedis.get("a")); 18 jedis.close(); 19 } 20 21 // 使用连接池 22 @Test 23 public void testPool() { 24 JedisPoolConfig poolConfig = new JedisPoolConfig(); 25 poolConfig.setMaxIdle(20); 26 poolConfig.setMinIdle(10); 27 poolConfig.setMaxTotal(30); 28 poolConfig.setMaxWaitMillis(3000); 29 poolConfig.setTestOnBorrow(true); 30 poolConfig.setTestOnReturn(true); 31 JedisPool jedisPool = new JedisPool("10.15.1.19", 16379); 32 Jedis jedis = jedisPool.getResource(); 33 jedis.auth("wC3Xo8E5mlmgMb"); 34 System.out.println(jedis.get("a")); 35 jedis.close(); 36 jedisPool.close(); 37 } 38 39 }
原文地址:https://www.cnblogs.com/gongxr/p/9406695.html
时间: 2024-11-10 13:15:12