public class RedisHelper implements NoSqlInterface{
private static JedisPool pool;
private static RedisHelper instance;
private RedisHelper(){
}
public synchronized static RedisHelper getInstance(){
if(instance == null){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(PropertyUtils.getIntVal("redis.max_active"));
config.setMaxIdle(PropertyUtils.getIntVal("redis.max_idle"));
config.setMaxWait(PropertyUtils.getIntVal("redis.max_wait"));
pool = new JedisPool(config,PropertyUtils.getStringVal("redis.host.ip"),PropertyUtils.getIntVal("redis.host.port"));
instance = new RedisHelper();
}
return instance;
}
/**
* 获取Jedis对象
* @return
*/
private static Jedis getJedis(){
return pool.getResource();
}
/**
* 添加value为Stirng的数据
* @param key
* @param value
*/
public void setStringVal(String key,String value){
Jedis jedis = getJedis();
jedis.set(key, value);
pool.returnResource(jedis);
}
/**
* 添加value为Stirng的数据
* @param key
* @param value
*/
public void setStringVal(String key,String value,Long seconds){
Jedis jedis = getJedis();
jedis.set(key, value);
jedis.expire(key,seconds.intValue());
pool.returnResource(jedis);
}
/**
* 根据key获取剩余过期时间
* @param key
* @return
*/
public Long getSurplusExpire(String key){
Jedis jedis = getJedis();
return jedis.ttl(key);
}
/**
* 根据key获取String value
* @param key
* @return
*/
public String getStringVal(String key){
Jedis jedis = getJedis();
String value = jedis.get(key);
pool.returnResource(jedis);
return value;
}
/**
* 移除key对应的value
* @param key
*/
public void remove(String key){
Jedis jedis = getJedis();
jedis.del(key);
pool.returnResource(jedis);
}
/**
* 清空所有的key
*/
public void removeAll(){
Jedis jedis = getJedis();
jedis.flushAll();
pool.returnResource(jedis);
}
/**
* 存储java bean对象
* @param key
* @param obj
*/
public void setObjectVal(String key,Object obj){
try {
Jedis jedis = getJedis();
jedis.set(key.getBytes(), SerializeUtil.serializeObj(obj));
pool.returnResource(jedis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取java bean
* @param key
* @return
*/
public Object getObjectVal(String key){
Jedis jedis = getJedis();
try {
return SerializeUtil.unSerializeObj(jedis.get(key.getBytes()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
pool.returnResource(jedis);
}
return null;
}
public static void main(String[] args) throws InterruptedException {
// Person p = new Person(14,"张三");
// p.setAddr(new Address(10086,"江苏扬州"));
// setObjectVal("zs",p);
//
// Person pp = (Person)getInstance().getObjectVal("zs");
// System.out.println(pp.getId()+":"+pp.getName());
// System.out.println(pp.getAddr().getId()+":"+pp.getAddr().getAddress());
getInstance().setStringVal("nihao", "abc",120L);
new Thread().sleep(1000*5);
System.out.println(getInstance().getJedis().ttl("nihao"));
System.out.println(getInstance().getStringVal("nihao"));
}
}