Java使用jedis

maven

         <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
            <version>2.4.2version>
        dependency>

Java代码如下

    @Test
    public void demo2(){
        /*
          创建连接池
         */
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大连接数
        config.setMaxTotal(20);
        //设置最大空闲连接数
        config.setMaxIdle(30);
        JedisPool jedisPool = new JedisPool(config,"192.168.124.129",6379);

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set("name","hello jedis");
            String name = jedis.get("name");
            System.out.println("name = " + name);
        }catch (Exception e){
        }finally {
            if(jedis != null){
                jedis.close();
            }
            if(config != null ){
                config.clone();
            }
        }
    }

你可能感兴趣的:(redis)