redis作業一個Key-Value資料庫目前被用的越來越頻繁,尤其在緩存處理有很不錯的效果。這裡記錄僅一些配置資訊。
1. 配置poolConfig
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(10);//最大空閑連結
poolConfig.setMaxTotal(100);//最大連接配接數
poolConfig.setMaxWaitMillis(1000*60*3);//最大等待毫秒數
poolConfig.setTestOnBorrow(true);//擷取連結檢查有效性
poolConfig.setTestOnReturn(false);//傳回驗證
poolConfig.setBlockWhenExhausted(true);//連結好近是否阻塞
poolConfig.setTestOnCreate(true);//部署時 為True;
jedisPool = new JedisPool(poolConfig, "localhost", 6379); //配置Jedis的配置,端口,伺服器位址
2 擷取連結
/**
* 擷取Jedis執行個體
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();//線程池取連結
return resource;
} else {
// fnReload();
System.out.println(">>>>>>>>>>>>>>>>>>>jedisPool is null>>>>>>>>>>>>>>>>");
return null;
}
} catch (Exception e) {
System.out.println("重新開機redisPool");
if(jedisPool!=null) {
jedisPool.close();
}
// fnReload();
e.printStackTrace();
return null;
}
}
3 使用連結存值,取值
/**
* 存儲值
* @param key
* @param value
*/
public static void addKey(String key,String value) {
Jedis jedis=getJedis();
if(jedis!=null) {
jedis.set(key, value,param0);
close(jedis);//釋放連結
}
}
/**
* 查詢值
* @param key
* @return
*/
public static String getKey(String key) {
Jedis jedis=getJedis();
if(jedis==null) {
return null;
}
if (jedis != null) {
try {
String ansInfo= jedis.get(key);
System.out.println("擷取redis鍵值:"+ansInfo);
return ansInfo;
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
close(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
4 釋放連結
/**
* 釋放jedis資源
* @param jedis
*/
public static synchronized void close(final Jedis jedis) {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
}
}
}
5 其中一開始在測試的時候,沒有使用try catch的方式,連接配接池很快就耗盡,發現是jedis無法釋放;将釋放連結的代碼放置在finally後,問題消失。暫時存疑。
6 完整代碼:
package com.w008.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.params.SetParams;
public class RedisUtil2 {
private static JedisPool jedisPool = null;//redis 池
private static SetParams param0=null;//參數1
static{
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(10);
poolConfig.setMaxTotal(100);
poolConfig.setMaxWaitMillis(1000*60*3);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(false);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnCreate(true);//部署時 為True;
jedisPool = new JedisPool(poolConfig, "localhost", 6379); //配置Jedis的配置,端口,伺服器位址
param0=new SetParams();
param0.px(1000l*60*60);//毫秒數
}
public RedisUtil2() {
}
/**
* 擷取Jedis執行個體
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
// fnReload();
System.out.println(">>>>>>>>>>>>>>>>>>>jedisPool is null>>>>>>>>>>>>>>>>");
return null;
}
} catch (Exception e) {
System.out.println("重新開機redisPool");
if(jedisPool!=null) {
jedisPool.close();
}
// fnReload();
e.printStackTrace();
return null;
}
}
public static void delKey(String key) {
Jedis jedis=jedisPool.getResource();
jedis.del(key);
}
/**
* 存儲值
* @param key
* @param value
*/
public static void addKey(String key,String value) {
Jedis jedis=getJedis();
if(jedis!=null) {
jedis.set(key, value,param0);
close(jedis);
}
}
/**
* 查詢值
* @param key
* @return
*/
public static String getKey(String key) {
Jedis jedis=getJedis();
if(jedis==null) {
return null;
}
if (jedis != null) {
try {
String ansInfo= jedis.get(key);
System.out.println("擷取redis鍵值:"+ansInfo);
return ansInfo;
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
close(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 釋放jedis資源
* @param jedis
*/
public static synchronized void close(final Jedis jedis) {
if (jedis != null) {
try {
jedis.close();
} catch (Exception e) {
}
}
}
public static void main(String[] args) throws Exception {
System.exit(0);
}
}