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[0]);
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[0]);
}
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[0]);
} 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[0]);
}
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[0]);
} 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[0]);
} 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[0]);
}
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[0]);
} 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[0]);
} 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[0]);
} 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[0]);
} 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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值