Java jedis RedisPool

根据https://www.cnblogs.com/SasaL/p/10709716.html,引入以下两个jar包:

 

 

redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=100
redis.host=192.168.0.235
redis.port=6379
redis.pwd=console.write

 

package cn.sasa.jedis;

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

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

public class RedisPoolUtil {
    static JedisPoolConfig jPoolConf;
    static JedisPool pool;
    static {
        InputStream input = RedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties");
        Properties prop = new Properties();
        try {
            prop.load(input);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        jPoolConf = new JedisPoolConfig();
        jPoolConf.setMaxIdle(Integer.parseInt(prop.getProperty("redis.maxIdle")));
        jPoolConf.setMinIdle(Integer.parseInt(prop.getProperty("redis.minIdle")));
        jPoolConf.setMaxTotal(Integer.parseInt(prop.getProperty("redis.maxTotal")));
        pool = new JedisPool(jPoolConf, "192.168.0.235", 6379, 30, "console.write");
    }
    
    public static Jedis getJedis(){
        Jedis jedis = pool.getResource();
        return jedis;
    }
    
    public static void close() {
        pool.close();
    }
    
}

 

package cn.sasa.jedis;

import redis.clients.jedis.Jedis;

public class JedisTest {
    
    public static void main(String[] args) {
        
        Jedis jedis = RedisPoolUtil.getJedis();
        
        jedis.set("name", "sa2");
         
        String name = jedis.get("name");  
        System.out.println(name);
        RedisPoolUtil.close();
    }

}

 

posted @ 2019-09-16 17:03  SasaL  Views(150)  Comments(0Edit  收藏  举报