Redisの接続プーリングツール

Redisの接続プーリングツール

package cn.hp.jedis.utils;

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 JedisPoolUtils {
    private static JedisPool jedisPool;
    static {
        //读取jedis配置文件(通过反射拿到当前类加载器对象,然后加载配置文件)
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        //通过Properties 工具类 读取数据
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将读取的配置文件信息配置到JedisPoolConfig 对象中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.valueOf(properties.getProperty("maxTotal")));
        config.setMaxIdle(Integer.valueOf(properties.getProperty("maxIdle")));
        //初始化 JedisPool
        jedisPool = new JedisPool(config,properties.getProperty("host"),Integer.valueOf(properties.getProperty("port")));
    }
    /**
     * 从池子中拿到一个链接对象
     * @return
     */
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

プロフィールコード

host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
公開された68元の記事 ウォン称賛7 ビュー2517

おすすめ

転載: blog.csdn.net/Cui6023056/article/details/104836347