jedis使用commoms-pool池化实现redis,项目需导入commons-pool.jar
1.编写redis.properties文件,配置信息如下:
#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=10.11.20.140
#Port
redis.port=6379
redis.pwd=
2.编写RedisConfig.java文件
public class RedisConfig {
public static JedisPool examRedisPool;
/* 绑定redis配置文件,并生成examRedisPool */
static {
examRedisPool = createPool("config/redis/redis");
if(examRedisPool==null){
throw new RuntimeException("无法连接到redis");
}
}
static JedisPool createPool(String redisProName){
ResourceBundle bundle = ResourceBundle.getBundle(redisProName);
if (bundle == null) {
throw new RuntimeException("redis配置文件找不到");
}
JedisPoolConfig config = new JedisPoolConfig();
JedisPool pool = null;
config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
config.setMaxWait(Integer.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf("redis.pool.testOnBorrow"));
config.setTestOnReturn(Boolean.valueOf("redis.pool.testOnReturn"));
String pwd = bundle.getString("redis.pool.pwd");
if (pwd.isEmpty()) {
pool = new JedisPool(config, bundle.getString("redis.pool.ip"),
Integer.valueOf(bundle.getString("redis.pool.port")));
return pool;
} else {
pool = new JedisPool(config, bundle.getString("redis.pool.ip"),
Integer.valueOf(bundle.getString("redis.pool.port")), 2000, pwd);
return pool;
}
}
}
3.编写JedisUtils.java类,操作redis中的数据
public class JedisUtils {
/**
* 从Redis 中获取对象
*
* @param <T>
*
* @param key
* 键值
* @return
* @return 在Redis存储的键值为key的值
*/
protected static <T> T getObject(JedisPool pool, String key) {
Jedis cliet = null;
try {
cliet = pool.getResource();
byte[] result = cliet.get(key.getBytes());
if (result != null && result.length > 0) {
T t = (T) SerializeUtil.unserialize(result);
return t;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(cliet);
} finally {
pool.returnResource(cliet);
}
return null;
}
/**
* 获取键值为key的字符串
*
* @param key
* 键值
* @return 键值为key的字符串
*/
protected static String getStr(JedisPool pool, String key) {
Jedis cliet = null;
try {
cliet = pool.getResource();
return cliet.get(key);
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(cliet);
} finally {
pool.returnResource(cliet);
}
return null;
}
/**
* 存储对象值
*
* @param key
* 键值
* @param value
* 值
* @param expire
* 失效时间(以秒为单位)
*/
protected static void setObject(JedisPool pool, String key, Object value, int expire) {
if (value == null)
return;
Jedis cliet = null;
try {
cliet = pool.getResource();
if (expire <= 0) {
cliet.set(key.getBytes(), SerializeUtil.serialize(value));
} else {
cliet.setex(key.getBytes(), expire, SerializeUtil.serialize(value));
}
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(cliet);
} finally {
pool.returnResource(cliet);
}
}
/**
* 删除对象值
*
* @param key
* 键值
* @param value
* 值
* @param expire
* 失效时间(以秒为单位)
*/
protected static void removeObject(JedisPool pool, String key) {
if (key == null)
return;
Jedis cliet = null;
try {
cliet = pool.getResource();
cliet.del(key.getBytes());
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(cliet);
} finally {
pool.returnResource(cliet);
}
}
/**
* 存储字符串信息
*
* @param key
* 键值
* @param value
* 值
* @param expire
* 失效时间(以秒为单位)
*/
protected static void setStr(JedisPool pool, String key, String value, int expire) {
Jedis cliet = null;
try {
cliet = pool.getResource();
if (expire <= 0) {
cliet.set(key, value);
} else {
cliet.setex(key, expire, value);
}
} catch (Exception e) {
e.printStackTrace();
pool.returnBrokenResource(cliet);
} finally {
pool.returnResource(cliet);
}
}
}