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);
}
}
版权声明:本文为qxianx原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。