redis教程-java操作redis(连接池)

idea创建maven工程

略

修改pom.xml

添加依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

https://mvnrepository.com/artifact/redis.clients/jedis/3.3.0

测试

public class Test2 {
    public static void main(String[] args) {
        //创建一个配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //最大连接数,默认是8。不close(),最多调用 jedisPool.getResource() 8次,第9次报错
        config.setMaxTotal(8);
        //最大空闲连接数,默认是8。一般与 maxTotal 相同,性能最优
        config.setMaxIdle(8);
        //最小空闲连接数
        config.setMinIdle(2);
        //创建Jedis连接池对象
        JedisPool jedisPool = new JedisPool(config,"localhost",6379);
        //获取连接
        Jedis jedis = jedisPool.getResource();
        String res=jedis.ping();
        System.out.println(res);
        jedis.set("name","李雷");
        System.out.println(jedis.get("name"));
    }
}

原文出处:/show_1IX7oyCGpVN.html