欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jedis--连接池

程序员文章站 2022-05-18 09:25:24
...

黑马程序员

Jedis–连接池

Jedis--连接池
Jedis--连接池
Jedis--连接池

    @Test
    public void test6(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(50);
        jedisPoolConfig.setMaxIdle(10);

        JedisPool jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379);
        Jedis resource = jedisPool.getResource();
        resource.set("name","huang");

        resource.close();
    }

Jedis–连接池工具类

Jedis--连接池
Jedis--连接池
Jedis--连接池
jedis.properties

host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
package cn.itcast.jedis.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisPoolUtil {

    public static JedisPool jedisPool;

    static {
        InputStream resourceAsStream = JedisPoolUtil.class.getClassLoader().getResourceAsStream("jedis.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
        jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));

        jedisPool = new JedisPool(jedisPoolConfig, properties.getProperty("host"), Integer.parseInt(properties.getProperty("port")));

    }

    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}

    @Test
    public void test7(){

        Jedis resource = JedisPoolUtil.getJedis();
        resource.set("hello","huang");
        System.out.println(resource.get("hello"));
        resource.close();
    }
相关标签: jedis