Properties properties = ResourcesUtils.getRuntimeProperties("redis");
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.valueOf(properties.getProperty("redis_maxTotal")));
config.setMaxIdle(Integer.valueOf(properties.getProperty("redis_maxIdle")));
config.setTestOnBorrow(Boolean.valueOf(properties.getProperty("redis_testOnBorrow")));
config.setMaxWaitMillis(Long.valueOf(properties.getProperty("redis_maxWait")));
jedisPool = new JedisPool(config,properties.getProperty("redis_host"),Integer.valueOf(properties.getProperty("redis_port")),Integer.valueOf(properties.getProperty("redis_timeout")));
运行时可以从pool中获取jedis对象,然后利用jedis的相关方法操作缓存。建议使用如下格式:
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
jedis.lpush(key,value);
}
catch (Exception e){
if(jedis != null){
jedisPool.returnBrokenResource(jedis);
}
}
finally {
if(jedis != null){
jedisPool.returnResource(jedis);
}
}
public class ObjectTranscoder {
public static byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv=null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
try {
if(os!=null)os.close();
if(bos!=null)bos.close();
}catch (Exception e2) {
e2.printStackTrace();
}
}
return rv;
}
public static Object deserialize(byte[] in) {
Object rv=null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if(in != null) {
bis=new ByteArrayInputStream(in);
is=new ObjectInputStream(bis);
rv=is.readObject();
is.close();
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(is!=null)is.close();
if(bis!=null)bis.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return rv;
}
}
maxActive:控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted。
maxIdle:控制一个pool最多有多少个状态为idle(空闲)的jedis实例;
whenExhaustedAction:表示当pool中的jedis实例都被allocated完时,pool要采取的操作;默认有三种。
WHEN_EXHAUSTED_FAIL --> 表示无jedis实例时,直接抛出NoSuchElementException;
WHEN_EXHAUSTED_BLOCK --> 则表示阻塞住,或者达到maxWait时抛出JedisConnectionException;
WHEN_EXHAUSTED_GROW --> 则表示新建一个jedis实例,也就说设置的maxActive无用;
maxWait:表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
testOnBorrow:在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
testOnReturn:在return给pool时,是否提前进行validate操作;
testWhileIdle:如果为true,表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
timeBetweenEvictionRunsMillis:表示idle object evitor两次扫描之间要sleep的毫秒数;
numTestsPerEvictionRun:表示idle object evitor每次扫描的最多的对象数;
minEvictableIdleTimeMillis:表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
softMinEvictableIdleTimeMillis:在minEvictableIdleTimeMillis基础上,加入了至少minIdle个对象已经在pool里面了。如果为-1,evicted不会根据idle time驱逐任何对象。如果minEvictableIdleTimeMillis>0,则此项设置无意义,且只有在timeBetweenEvictionRunsMillis大于0时才有意义;