RedisCacheServiceUtil
package com.paic.icorepnbs.web.util;
import com.paic.icorepnbs.common.util.LogUtils; import com.paic.icorepnbs.common.util.SerializableUtil; import com.paic.icorepnbs.common.util.StringUtils; import java.util.Map; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.exceptions.JedisException;
public class RedisCacheServiceUtil { protected JedisPool jedisPool; public static final String KEY_NAME_SEP = ":";
protected Jedis getJedis() throws JedisException { Jedis jedis = null; try { jedis = this.jedisPool.getResource(); } catch (JedisException e) { LogUtils.error("获取jedis连接异常", e, null, new Object); if (jedis != null) this.jedisPool.returnBrokenResource(jedis);
throw e;
}
return jedis;
}
public void setObject(String key, Object object, int cacheSeconds) { Jedis jedis = null; boolean isBroken = false; try { jedis = getJedis(); String storedKey = getRedisStoreKey(object.getClass(), key); jedis.set(storedKey.getBytes("UTF-8"), SerializableUtil.serializable(object));
if (cacheSeconds >= 0)
jedis.expire(storedKey, cacheSeconds);
}
catch (Exception e) {
isBroken = true;
} finally {
release(jedis, isBroken);
}
}
protected String getRedisStoreKey(Class clazz, String key) { StringBuffer keyBuffer = new StringBuffer(clazz.getName()).append(":").append(key);
return keyBuffer.toString();
}
public String getStringValue(String key) { String value = null; Jedis jedis = null; try { jedis = getJedis(); if (jedis.exists(key).booleanValue()) { value = jedis.get(key); value = ((StringUtils.isNotBlank(value)) && (!("nil".equalsIgnoreCase(value)))) ? value : null; } } catch (Exception e) { LogUtils.error("InternetRquestAstrictFilter--->get异常", e, null, new Object); } finally { } return value; }
public Object getObject(String key, Class clazz) { Jedis jedis = null; boolean isBroken = false; Object obj = null; try { jedis = getJedis(); String storedKey = getRedisStoreKey(clazz, key); byte bytes = jedis.get(storedKey.getBytes("UTF-8")); obj = SerializableUtil.deSerializable(bytes); } catch (Exception e) { isBroken = true; } finally { release(jedis, isBroken); } return obj; }
public void setHashField(String redisKey, String itemKey, String value) { boolean isBroken = false; Jedis jedis = null; try { jedis = getJedis(); jedis.hset(redisKey, itemKey, value); } catch (JedisException e) { isBroken = true; LogUtils.error("redis执行失败", e, null, new Object); } finally { release(jedis, isBroken); } }
public void setHashMap(String key, Map<String, String> map, int cacheSeconds) { boolean isBroken = false; Jedis jedis = null; if ((map != null) && (map.size() > 0)) try { jedis = getJedis(); jedis.hmset(key, map); if (cacheSeconds >= 0) jedis.expire(key, cacheSeconds); } catch (JedisException e) { isBroken = true; LogUtils.error("redis执行失败,setHashMap(" + key + "," + map.toString() + "," + cacheSeconds + ")", e, null, new Object); } finally { release(jedis, isBroken); } }
public Map<String, String> getHashMapByKey(String key) { boolean isBroken = false; Jedis jedis = null; Map valueMap = null; try { jedis = getJedis(); valueMap = jedis.hgetAll(key); } catch (JedisException e) { isBroken = true; LogUtils.error("redis执行失败,getHashMapByKey(" + key + ")", e, null, new Object); } finally { release(jedis, isBroken); } return valueMap; }
public String getItemFromMap(String redisKey, String itemKey) { boolean isBroken = false; Jedis jedis = null; String result = ""; try { jedis = getJedis(); result = jedis.hget(redisKey, itemKey); } catch (JedisException e) { isBroken = true; LogUtils.error("redis执行失败,getItemFromMap", e, null, new Object); } finally { release(jedis, isBroken); } return result; }
public long removeItemFromHashMap(String redisKey, String itemKey) { boolean isBroken = false; Jedis jedis = null; try { jedis = getJedis(); long l = jedis.hdel(redisKey, new String { itemKey }).longValue();
return l;
}
catch (JedisException e)
{
isBroken = true;
LogUtils.error("redis执行失败,removeItemFormHashMap(" + redisKey + "," + itemKey + ")", e, null, new Object);
}
finally {
release(jedis, isBroken);
}
return 0L;
}
public void setString(String key, String value, int cacheSeconds) { Jedis jedis = null; boolean isBroken = false; try { jedis = getJedis(); jedis.set(key, value); if (cacheSeconds >= 0) jedis.expire(key, cacheSeconds); } catch (JedisException e) { isBroken = true; LogUtils.error("redis执行失败;" + key + ":" + value, e, null, new Object); } finally { release(jedis, isBroken); } }
public long deleteJedisByKey(String key) { boolean isBroken = false; Jedis jedis = null; try { jedis = getJedis(); long l = jedis.del(key).longValue();
return l;
}
catch (Exception e)
{
isBroken = true;
LogUtils.error("redis执行,移除key:" + key + ",失败。", e, null, new Object);
} finally {
release(jedis, isBroken);
}
return 0L;
}
public boolean existKey(String key) { Jedis jedis = null; boolean isBroken = false; try { jedis = getJedis(); boolean bool1 = jedis.exists(key).booleanValue();
return bool1;
}
catch (Exception e)
{
isBroken = true;
LogUtils.error("existKey异常!", e, null, new Object);
} finally {
release(jedis, isBroken);
}
return false;
}
public Long expire(String key, int seconds) { Long result = null; boolean isBroken = false; Jedis jedis = null; try { jedis = getJedis(); result = jedis.expire(key, seconds); } catch (Exception e) { isBroken = true; LogUtils.error("expire异常:", e, null, new Object); } finally { release(jedis, isBroken); } return result; }
public void release(Jedis jedis, boolean isBroken) { if (jedis != null) if (isBroken) this.jedisPool.returnBrokenResource(jedis); else this.jedisPool.returnResource(jedis); }
public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } }