RedisWrapperImpl
package com.jdl.lomir.chint.core.cache.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jd.jim.cli.Cluster;
import com.jdl.lomir.chint.core.cache.IRedisWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class RedisWrapperImpl<T> implements IRedisWrapper<T> {
private static final String LOG_PREFIX = "[Redis Wrapper]: ";
private final ObjectMapper JSON = new ObjectMapper();
@Resource
private Cluster cluster;
@Override
public Boolean exists(String key) {
return this.cluster.exists(key);
}
@Override
public String get(String key) {
String value = this.cluster.get(key);
log.debug("{}[get {}] << result: [{}]", LOG_PREFIX, key, value);
return value;
}
@Override
public void set(String key, String value) {
this.cluster.set(key, value);
log.debug("{}[set {}] >> value: [{}]", LOG_PREFIX, key, value);
}
public void set(String key,Object value){
this.set(key,value);
log.debug("{}[set {}] >> value: [{}]", LOG_PREFIX, key, value);
}
@Override
public void setex(String key, String value, long num, TimeUnit timeUnit) {
this.cluster.setEx(key, value, num, timeUnit);
log.debug("{}[setex {}] >> value: [{}], ex: [{}], unit: [{}]", LOG_PREFIX, key, value, num, timeUnit.toString());
}
@Override
public Long lpush(String key, String body) {
Long result = this.cluster.lPush(key, body);
log.debug("{}[lpush {}] >> result: [{}], value [{}]", LOG_PREFIX, key, result, body);
return result;
}
public Long rpush(String key, String body) {
Long result = this.cluster.rPush(key, body);
log.debug("{}[rpush {}] >> result: [{}], value [{}]", LOG_PREFIX, key, result, body);
return result;
}
@Override
public Boolean hset(String name, String key, String value) {
Boolean result = this.cluster.hSet(name, key, value);
log.debug("{}[hset {} {}] >> result: [{}], value: [{}]", LOG_PREFIX, name, key, result, value);
return result;
}
@Override
public String hget(String name, String key) {
String value = this.cluster.hGet(name, key);
log.debug("{}[hget {} {}] << value: [{}]", LOG_PREFIX, name, key, value);
return value;
}
@Override
public Long hdel(String name, String key) {
Long result = this.cluster.hDel(name, key);
//todo : 这里打日志是为了更好的监控围栏网点的删除 如果后期有频繁调用的需求 可将日志改成debug 并在删除围栏网点处加好日志
log.info("{}[hdel {} {}] >> result: [{}]", LOG_PREFIX, name, key,result);
return result;
}
@Override
public Set<String> hkeys(String name) {
Set<String> keys = this.cluster.hKeys(name);
try {
log.debug("{}[hkeys {}] << value: [{}]", LOG_PREFIX, name, JSON.writeValueAsString(keys));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return keys;
}
@Override
public List<String> hvals(String name) {
List<String> values = this.cluster.hVals(name);
try {
log.debug("{}[hvals {}] << value: [{}]", LOG_PREFIX, name, JSON.writeValueAsString(values));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return values;
}
@Override
public Map<String, String> hgetall(String name) {
Map<String, String> values = this.cluster.hGetAll(name);
try {
log.debug("{}[hgetall {}] << value: [{}]", LOG_PREFIX, name, JSON.writeValueAsString(values));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return values;
}
@Override
public long llen(String key) {
if (key == null || key.length() < 1) {
log.warn("{}[llen] key is blank", LOG_PREFIX);
return 0;
}
Long count = this.cluster.lLen(key);
log.debug("{}[llen {}] << value: [{}]", LOG_PREFIX, key, count);
return count == null ? 0 : count.longValue();
}
@Override
public List<String> lrange(String key, long start, long end) throws Exception {
if (key == null || key.length() < 1) {
log.warn("{}[lrange] key is blank", LOG_PREFIX);
return null;
}
List<String> list = this.cluster.lRange(key, start, end);
log.debug("{}[lrange {}] << value: [{}], start: [{}], end: [{}]", LOG_PREFIX, key, start, end);
return list;
}
@Override
public String lpop(String key) throws Exception {
if (key == null || key.length() < 1) {
log.warn("{}[lpop] key is blank", LOG_PREFIX);
return null;
}
String result = this.cluster.lPop(key);
log.debug("{}[lpop {}] << value: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public String rpop(String key) {
if (key == null || key.length() < 1) {
log.warn("{}[rpop] key is blank", LOG_PREFIX);
return null;
}
String result = this.cluster.rPop(key);
log.debug("{}[rpop {}] << value: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Long del(String key) {
Long result = this.cluster.del(key);
log.info("{}[del {}] >> result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Long sadd(String key, String... member) {
Long result = this.cluster.sAdd(key, member);
try {
log.debug("{}[sadd {}] >> result: [{}], value: [{}]", LOG_PREFIX, key, result, JSON.writeValueAsString(member));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
@Override
public Long srem(String key, String... member) {
Long result = this.cluster.sRem(key, member);
try {
log.debug("{}[srem {}] >> result: [{}], value: [{}]", LOG_PREFIX, key, result, JSON.writeValueAsString(member));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
@Override
public Long scard(String key) {
Long result = this.cluster.sCard(key);
log.debug("{}[scard {}] << value: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Set<String> smembers(String key) {
Set<String> members = this.cluster.sMembers(key);
try {
log.debug("{}[smembers {}] << value: [{}]", LOG_PREFIX, key, JSON.writeValueAsString(members));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return members;
}
@Override
public Boolean sismember(String key, String member) {
Boolean result = this.cluster.sIsMember(key, member);
log.debug("{}[sismember {}] << result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Long decrBy(String key, long integer) {
Long result = this.cluster.decrBy(key, integer);
log.debug("{}[decrBy {}] << result: [{}], decrement: [{}]", LOG_PREFIX, key, result, integer);
return result;
}
@Override
public Long decr(String key) {
Long result = this.cluster.decr(key);
log.debug("{}[decr {}] << result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Long incrBy(String key, long integer) {
Long result = this.cluster.incrBy(key, integer);
log.debug("{}[incrBy {}] << result: [{}], increment: [{}]", LOG_PREFIX, key, result, integer);
return result;
}
@Override
public Long incr(String key) {
Long result = this.cluster.incr(key);
log.debug("{}[incr {}] << result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Boolean expire(String key, long num, TimeUnit timeUnit) {
Boolean result = this.cluster.expire(key, num, timeUnit);
log.debug("{}[expire {}] << result: [{}], ex: [{}], unit: [{}]", LOG_PREFIX, key, result, num, timeUnit.toString());
return result;
}
@Override
public boolean persist(String key){
Boolean result=this.cluster.persist(key);
log.debug("{}[persist {}] << result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Long hlen(String key) {
Long result = this.cluster.hLen(key);
log.debug("{}[hlen {}] << result: [{}]", LOG_PREFIX, key, result);
return result;
}
@Override
public Boolean hexists(String key, String field) {
Boolean result = this.cluster.hExists(key, field);
log.debug("{}[hexists {}] << result: [{}], field: [{}]", LOG_PREFIX, key, result, field);
return result;
}
@Override
public Long hincrby(String key, String field, Integer value) {
Long result = this.cluster.hIncrBy(key, field, value);
log.debug("{}[hincrby {}] << result: [{}], field: [{}], increment: [{}]", LOG_PREFIX, key, result, field, value);
return result;
}
@Override
public List<String> hMGet(String key, String... fields){
return this.cluster.hMGet(key, fields);
}
@Override
public Long lrem(String key, long count, String value) {
return this.cluster.lRem(key, count, value);
}
@Override
public void ltrim(String key, long begin, long end) {
this.cluster.lTrim(key, begin, end);
}
@Override
public Boolean lock(String key, String value, long timeout) {
if (cluster != null && StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
try {
if (cluster.setNX(key, value)) {
cluster.expire(key, timeout, TimeUnit.SECONDS);
return true;
}
Long ttl = cluster.ttl(key);
if (ttl < 0L) {
cluster.expire(key, timeout, TimeUnit.SECONDS);
}
} catch (Exception var6) {
return false;
}
return false;
} else {
return false;
}
}
@Override
public Boolean unlock(String key) {
if (cluster != null && StringUtils.isNotBlank(key)) {
Boolean returnValue = false;
try {
Long delNumber = cluster.del(key);
return delNumber == 1L;
} catch (Exception var4) {
return returnValue;
}
} else {
return false;
}
}
@Override
public Long ttl(String key) {
return cluster.ttl(key);
}
@Override
public Boolean zadd(String key, double score, String value) {
Boolean result = this.cluster.zAdd(key, score, value);
log.debug("{}[zadd {}] >> result: [{}], score: [{}], value: [{}]", LOG_PREFIX, key, result, score, value);
return result;
}
@Override
public Set<String> zrange(String key, long start, long end) {
return this.cluster.zRange(key, start, end);
}
}
IBaseCacheWrapper
/**
* Project Name:preseparate-service
* File Name:IBaseCacheWrapper.java
* Package Name:com.jd.bluedragon.preseparate.cache
* Date:Nov 8, 20133:29:31 PM
* Copyright (c) 2013, www.jd.com All Rights Reserved.
*/
package com.jdl.lomir.chint.core.cache;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @Description:<br/>
* @author hubert <br/>
* @date: Nov 8, 2013 3:29:31 PM
*/
public interface IBaseCacheWrapper {
void set(String key, String value);
void setex(String key, String value, long num, TimeUnit timeUnit);
String get(String key);
Long del(String key);
Long decrBy(String key, long integer);
Long decr(String key);
Long incrBy(String key, long integer);
Long incr(String key);
Boolean hset(String name, String key, String value);
String hget(String name, String key);
Long hdel(String name, String key);
Set<String> hkeys(String name);
List<String> hvals(String name);
Map<String, String> hgetall(String name);
}
IRedisWrapper
package com.jdl.lomir.chint.core.cache;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public interface IRedisWrapper<T> extends IBaseCacheWrapper{
public Long lpush(String key, String body);
public Long rpush(String key, String body);
public String lpop(String key) throws Exception;
public String rpop(String key);
public void ltrim(String key, long begin, long end);
public Long lrem(String key, long count, String value);
public List<String> lrange(String key, long start, long end) throws Exception;
public long llen(String key);
Long sadd(String key, String... member);
Long srem(String key, String... member);
Long scard(String key);
Set<String> smembers(String key);
Boolean sismember(String key, String member);
public Boolean expire(String key, long num, TimeUnit timeUnit);
boolean persist(String key);
public Long hlen(String key);
public Boolean exists(String key);
public Long hdel(String key, String field);
public Boolean hexists(String key, String field);
public String hget(String key, String field);
public Map<String, String> hgetall(String key);
public Boolean hset(String key, String field, String value);
Long hincrby(String key, String field, Integer value);
public List<String> hMGet(String key, String... fields);
public Boolean lock(String key, String value, long timeout);
public Boolean unlock(String key);
public Long ttl(String key);
public Boolean zadd(String key, double score, String value);
public Set<String> zrange(String key, long start, long end);
}