Java工具类之——RedisUtils类

同C3P0一样,这个是需要配置文件的类,配置文件的格式为properties格式,放在src目录之下,需要用到的工具包为两个,分别是:commons-pool2-2.3.jar和jedis-2.7.0.jar
maxIdle=20
maxTotal=100
minIdle=10
host=192.168.75.140
port=6379


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

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

public class RedisUtil {
	private static JedisPoolConfig poolConfig = new JedisPoolConfig();
	private static JedisPool pool ;
	static{
		//解析配置文件
		try {
			Properties properties = new Properties();
			//使用类加载器 加载配置文件
			properties.load(RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));
			int maxIdle =Integer.parseInt( properties.getProperty("maxIdle"));
			int maxTotal =Integer.parseInt( properties.getProperty("maxTotal"));
			int minIdle =Integer.parseInt( properties.getProperty("minIdle"));

			String host =properties.getProperty("host");
			int port =Integer.parseInt( properties.getProperty("port"));
			poolConfig.setMaxIdle(maxIdle);
			poolConfig.setMaxTotal(maxTotal);
			poolConfig.setMinIdle(minIdle);
			pool= new JedisPool(poolConfig,host, port);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static Jedis getConnection(){
		return pool.getResource();
	}
	public static void close(){
		pool.close();
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值