package cn.kgc.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ResourceBundle;
public class RedisUtils {
private static JedisPool pool;
static {
ResourceBundle rb = ResourceBundle.getBundle("cinfig");
String host = rb.getString("host");
int port = Integer.parseInt(rb.getString("port"));
int maxTotal = Integer.parseInt(rb.getString("maxTotal"));
int maxIdle = Integer.parseInt(rb.getString("maxIdle"));
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
pool = new JedisPool(jedisPoolConfig, host, port);
}
public static Jedis getJedis(){
return pool.getResource();
}
public static void close(Jedis jedis){
if(jedis!=null)
jedis.close();
}
public static void close(JedisPool pool){
if(pool!=null)
pool.close();
}
}
对应的config.properties文件
host=localhost
port=6379
maxTotal=50
maxIdle=20
这个是连接Redis数据库的连接文件代码 其中还需要配置java包 不然的话 是不能使用的
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;