Jedis
样例程序
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.7.0</version> </dependency>
|
private Jedis jedis;
@BeforeEach void setUp() { jedis = new Jedis("192.168.150.101",6379); jedis.auth("123456"); jedis.select(0); }
|
@Test void testString() { String result = jedis.set("name", "张三"); System.out.println("result = " + result); String name = jedis.get("name") ; System.out.println("name = " + name) ; }
|
@AfterEach void tearDown() { if (jedis != null) { jedis.close(); } }
|
Jedis连接池
Jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,因此我们推荐大家使用Jedis连接池代替Jedis的直连方式
public class JedisConnectionFactory { private static final JedisPool jedisPool; static { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(8); poolConfig.setMaxIdle(8); poolConfig.setMinIdle(0); poolConfig.setMaxWaitMillis(1000); jedisPool = new JedisPool(poolConfig, "192.168.72.128", 6379, 1000, "123456"); } public static Jedis getJedis() { return jedisPool.getResource(); } }
|