package test.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Maps;
import test.util.ObjectUtils;
import test.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;
public class JedisUtils {
private final static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
public final static int DEFAULT_CACHE_SECONDS = 30 * 60;
public JedisPool jedisPool;
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
public void init() {
if (jedisPool== null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(50);
config.setMaxIdle(5);
config.setMaxWait(1000 * 100);
config.setTestOnBorrow(true);
pool = new JedisPool(config, "host:port", password, 3600, database);
}
}
* 获取缓存
* @param key 键
* @return 值
*/
public String get(String key)throws Exception {
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
logger.debug("get {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("get {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 获取缓存
* @param key 键
* @param cacheSeconds 缓存时间
* @return 值
*/
public String get(String key, int cacheSeconds)throws Exception {
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
logger.debug("get {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("get {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 设置缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public String set(String key, String value, int cacheSeconds) throws Exception{
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.set(key, value);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("set {} = {}", key, value);
} catch (Exception e) {
logger.warn("set {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 获取单个hash缓存
* @param @param key
* @param @param field
* @return String
*/
public String hget(String key, String field) throws Exception {
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
value = jedis.hget(key, field);
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
logger.debug("hget {} = {}", field, value);
} catch (Exception e) {
logger.warn("hget {} = {}", field, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 设置单个hash缓存
* @param @param key
* @param @param field
* @return String
*/
public String hset(String key, String field, String value) throws Exception {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
jedis.hset(key, field, value);
logger.debug("hset {} : {} = {}", key, field, value);
} catch (Exception e) {
logger.warn("hset {} : {} = {}", key, field, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 获取List缓存
* @param key 键
* @return 值
*/
public List<String> getList(String key) throws Exception{
List<String> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.lrange(key, 0, -1);
logger.debug("getList {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getList {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 设置List缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public long setList(String key, List<String> value, int cacheSeconds)throws Exception {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
jedis.del(key);
}
result = jedis.rpush(key, (String[])value.toArray());
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setList {} = {}", key, value);
} catch (Exception e) {
logger.warn("setList {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 向List缓存中添加值
* @param key 键
* @param value 值
* @return
*/
public long listAdd(String key, String... value)throws Exception {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.rpush(key, value);
logger.debug("listAdd {} = {}", key, value);
} catch (Exception e) {
logger.warn("listAdd {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 获取有序zet缓存 (按分值由低到高排序输出)
* @param key 键
* @return 值
*/
public Set<String> getZsetZrange(String key) throws Exception {
return getZsetZrange(key,0 , -1);
}
* 获取有序zet缓存 (按分值由高到低排序输出)
* @param key 键
* @return 值
*/
public Set<String> getZsetZrevrange(String key)throws Exception{
return getZsetZrevrange(key,0 , -1);
}
*
* @Title: getZsetZrangePage
* @Description: 分页获取 获取有序zet缓存 (按分值由低到高排序输出)
* @param @param key
* @param @param end
* @param @return
* @return Set<String>
* @throws
*/
public Set<String> getZsetZrange(String key,long start , long end) throws Exception{
Set<String> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.zrange(key, start, end);
logger.debug("getZset {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getZset {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
*
* @Title: getZsetZrevrange
* @Description: 获取有序zet缓存 (按分值由高到低排序输出)
* @param @param key
* @param @param start
* @param @param end
* @param @return
* @return Set<String>
* @throws
*/
public Set<String> getZsetZrevrange(String key,long start , long end) throws Exception{
Set<String> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.zrevrange (key, start,end);
logger.debug("getZset {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getZset {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 设置Set缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public long setZset(String key, Map<String, Double> scoreMembers, int cacheSeconds) throws Exception{
long result = 0;
Jedis jedis = null;
boolean isExists = false;
try {
jedis = getResource();
if (jedis.exists(key)) {
isExists = true;
}
jedis.zadd(key, scoreMembers);
if (!isExists && cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setZset {} = {}", key, scoreMembers);
} catch (Exception e) {
logger.warn("setZset {} = {}", key, scoreMembers, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 获取Map缓存
* @param key 键
* @return 值
*/
public Map<String, String> getMap(String key) throws Exception{
Map<String, String> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.hgetAll(key);
logger.debug("getMap {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getMap {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 获取Map缓存里某个key的值
* @Title: getMapKeyVal
* @Description: TODO
* @param @param key
* @param @param mapKey
* @param @return
* @return String
* @throws
*/
public String getMapKeyVal(String key,String mapKey) throws Exception{
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
if(jedis.hexists(key, mapKey)){
value = jedis.hget(key, mapKey);
logger.debug("getMap {} {} = {}", key, mapKey,value);
}
}
} catch (Exception e) {
logger.warn("getMap {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 获取Map缓存
* @param key 键
* @return 值
*/
public Map<String, Object> getObjectMap(String key) throws Exception{
Map<String, Object> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
value = Maps.newHashMap();
Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));
for (Map.Entry<byte[], byte[]> e : map.entrySet()){
value.put(StringUtil.toString(e.getKey()), toObject(e.getValue()));
}
logger.debug("getObjectMap {} = {}", key, value);
}
} catch (Exception e) {
logger.warn("getObjectMap {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return value;
}
* 设置Map缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public String setMap(String key, Map<String, String> value, int cacheSeconds)throws Exception {
String result = null;
Jedis jedis = null;
boolean isExists = false;
try {
jedis = getResource();
if (jedis.exists(key)) {
isExists = true;
}
result = jedis.hmset(key, value);
if (!isExists && cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setMap {} = {}", key, value);
} catch (Exception e) {
logger.warn("setMap {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 设置Map缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public String setObjectMap(String key, Map<String, Object> value, int cacheSeconds) throws Exception{
String result = null;
Jedis jedis = null;
boolean isExists = false;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
isExists = true;
}
Map<byte[], byte[]> map = Maps.newHashMap();
for (Map.Entry<String, Object> e : value.entrySet()){
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
}
result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>)map);
if (!isExists && cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setObjectMap {} = {}", key, value);
} catch (Exception e) {
logger.warn("setObjectMap {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 向Map缓存中添加值
* @param key 键
* @param value 值
* @return
*/
public String mapPut(String key, Map<String, String> value) throws Exception{
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hmset(key, value);
logger.debug("mapPut {} = {}", key, value);
} catch (Exception e) {
logger.warn("mapPut {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 向Map缓存中添加值
* @param key 键
* @param value 值
* @return
*/
public String mapObjectPut(String key, Map<String, Object> value)throws Exception {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
Map<byte[], byte[]> map = Maps.newHashMap();
for (Map.Entry<String, Object> e : value.entrySet()){
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
}
result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>)map);
logger.debug("mapObjectPut {} = {}", key, value);
} catch (Exception e) {
logger.warn("mapObjectPut {} = {}", key, value, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 移除Map缓存中的值
* @param key 键
* @param value 值
* @return
*/
public long mapRemove(String key, String mapKey)throws Exception {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hdel(key, mapKey);
logger.debug("mapRemove {} {}", key, mapKey);
} catch (Exception e) {
logger.warn("mapRemove {} {}", key, mapKey, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 移除Map缓存中的值
* @param key 键
* @param value 值
* @return
*/
public long mapObjectRemove(String key, String mapKey) throws Exception {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
logger.debug("mapObjectRemove {} {}", key, mapKey);
} catch (Exception e) {
logger.warn("mapObjectRemove {} {}", key, mapKey, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 判断Map缓存中的Key是否存在
* @param key 键
* @param value 值
* @return
*/
public boolean mapExists(String key, String mapKey)throws Exception {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hexists(key, mapKey);
logger.debug("mapExists {} {}", key, mapKey);
} catch (Exception e) {
logger.warn("mapExists {} {}", key, mapKey, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 判断Map缓存中的Key是否存在
* @param key 键
* @param value 值
* @return
*/
public boolean mapObjectExists(String key, String mapKey)throws Exception {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
logger.debug("mapObjectExists {} {}", key, mapKey);
} catch (Exception e) {
logger.warn("mapObjectExists {} {}", key, mapKey, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 删除缓存
* @param key 键
* @return
*/
public long del(String key) throws Exception{
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)){
result = jedis.del(key);
logger.debug("del {}", key);
}else{
logger.debug("del {} not exists", key);
}
} catch (Exception e) {
logger.warn("del {}", key, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 删除缓存
* @param key 键
* @return
*/
public long delObject(String key)throws Exception {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))){
result = jedis.del(getBytesKey(key));
logger.debug("delObject {}", key);
}else{
logger.debug("delObject {} not exists", key);
}
} catch (Exception e) {
logger.warn("delObject {}", key, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 缓存是否存在
* @param key 键
* @return
*/
public boolean exists(String key)throws Exception {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.exists(key);
logger.debug("exists {}", key);
} catch (Exception e) {
logger.warn("exists {}", key, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 缓存是否存在
* @param key 键
* @return
*/
public boolean existsObject(String key)throws Exception {
boolean result = false;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.exists(getBytesKey(key));
logger.debug("existsObject {}", key);
} catch (Exception e) {
logger.warn("existsObject {}", key, e);
throw new Exception(e);
} finally {
returnResource(jedis);
}
return result;
}
* 获取资源
* @return
* @throws JedisException
*/
public Jedis getResource() throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (JedisException e) {
logger.warn("getResource.", e);
returnBrokenResource(jedis);
throw new Exception(e);
}
return jedis;
}
* 归还资源
* @param jedis
* @param isBroken
*/
public void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
* 释放资源
* @param jedis
* @param isBroken
*/
public void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
* 获取byte[]类型Key
* @param key
* @return
*/
public byte[] getBytesKey(Object object){
if(object instanceof String){
return StringUtil.getBytes((String)object);
}else{
return ObjectUtils.serialize(object);
}
}
* Object转换byte[]类型
* @param key
* @return
*/
public byte[] toBytes(Object object){
return ObjectUtils.serialize(object);
}
* byte[]型转换Object
* @param key
* @return
*/
public Object toObject(byte[] bytes){
return ObjectUtils.unserialize(bytes);
}
}