@
rrfeng @
arrow8899 @
ke1e private static void initialPool() {
ApplicationContext applicationContext =
new FileSystemXmlApplicationContext("classpath:config/spring-application-redis.xml");
jedisPool = (JedisPool) applicationContext.getBean("jedisPool");
logger.info("<---jedis pool init--->");
}
/**
* 初始化,加锁防止被多次初始化 pool
*/
private synchronized static void poolInit() {
if (jedisPool == null) {
initialPool();
}
}
/**
* 获取 Jedis 实例
*
* @
return Jedis
*/
public static Jedis getJedis() {
poolInit();
Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
logger.error("get jedis error",e);
} finally {
returnResource(jedis);
logger.debug("<---jedis return resource--->");
}
return jedis;
}
/**
* 释放 jedis 资源
*
* @
param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
public static String get(String key) {
try {
return getJedis().get(key);
} catch (Exception ex) {
logger.error("redis get error",ex);
}
return null;
}
public static String lpop(String key) {
try {
return getJedis().lpop(key);
} catch (Exception ex) {
logger.error("lpop error , key = {}", key, ex);
}
return null;
}