/**
* @author chengmo on 2020/7/16
*/
public class RedisServiceImpl implements RedisService {
private static final GLog LOG = ();
private RedissonClient client;
private StringRedisTemplate stringRedisTemplate;
private final String namespace;
public RedisServiceImpl( { String namespace)
this.namespace = + +
(namespace, "-", "_") +
;
}
public Long increment(String key) {
String redisKey = this.rebuildKey(key);
Long req = null;
try {
ValueOperations<String, String> op = ();
req = (redisKey);
} catch (Exception e) {
("increment [Key:{0}] from redis failed!", e, redisKey);
}
return req;
}
public boolean hasKey(String key) {
String redisKey = this.rebuildKey(key);
try {
return (redisKey);
} catch (Exception e) {
("hasKey [Key:{0}] from redis failed!", e, redisKey);
}
return false;
}
public String get(String key) {
String redisKey = this.rebuildKey(key);
String result;
try {
result = ().get(redisKey);
if (null == result) {
return null;
}
return result;
} catch (Exception e) {
("Get [Key:{0}] from redis failed!", e, redisKey);
}
return null;
}
public boolean delete(String key) {
String redisKey = this.rebuildKey(key);
try {
(redisKey);
return true;
} catch (Exception e) {
("Delete [Key:{0}]from redis failed!", e, redisKey);
}
return false;
}
public boolean set(String key, String value) {
String redisKey = this.rebuildKey(key);
try {
().set(redisKey, value);
return true;
} catch (Exception e) {
("Set [Key:{0}][Value:{1}] into redis failed!", e, redisKey, value);
}
return false;
}
public boolean set(String key, String value, Long timeout) {
String redisKey = this.rebuildKey(key);
try {
().set(redisKey, value, timeout, );
return true;
} catch (Exception e) {
("Set [Key:{0}][Value:{1}][TimeOut:{2}] into redis failed!", e, redisKey, value, timeout);
}
return false;
}
public boolean setExpire(String key, Long expire) {
String redisKey = this.rebuildKey(key);
try {
(redisKey, expire, );
return true;
} catch (Exception e) {
("setExpire [Key:{0}][handleAutoRenew:{1}] into redis failed!", e, redisKey, expire);
}
return false;
}
public long getExpire(String key) {
String redisKey = this.rebuildKey(key);
try {
return (redisKey, );
} catch (Exception e) {
("getExpire [Key:{0}][handleAutoRenew:{1}] into redis failed!", e, redisKey);
}
return 0l;
}
public String hget(String key, String hKey) {
String redisKey = this.rebuildKey(key);
try {
return (String) ().get(redisKey, hKey);
} catch (Exception e) {
("hget key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);
}
return null;
}
public Map<String, String> hscan(String key, String pattern) {
String redisKey = this.rebuildKey(key);
Cursor<<Object, Object>> cursor = null;
try {
ScanOptions scanOptions = null;
if ((pattern))
scanOptions = ;
else
scanOptions = ().match(pattern).build();
cursor = ().scan(redisKey, scanOptions);
Map<String, String> map = new HashMap<>();
while (()) {
<Object, Object> entry = ();
Object value = ();
(().toString(), null != value ? () : null);
}
return map;
} catch (Exception e) {
("hscan key:[{0}] fail ", e, redisKey);
} finally {
if (null != cursor) {
try {
();
} catch (IOException e) {
("hscan close cursor fail ", e);
}
}
}
return null;
}
public boolean hset(String key, String hKey, String hValue) {
String redisKey = this.rebuildKey(key);
try {
().put(redisKey, hKey, hValue);
return true;
} catch (Exception e) {
("hset key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);
}
return true;
}
public boolean hset(String key, String hKey, String hValue, long timeout) {
String redisKey = this.rebuildKey(key);
try {
().put(redisKey, hKey, hValue);
(redisKey, timeout, );
return true;
} catch (Exception e) {
("hset key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);
}
return true;
}
public boolean hdel(String key, String hKey) {
String redisKey = this.rebuildKey(key);
try {
().delete(redisKey, hKey);
return true;
} catch (Exception e) {
("hdel key:[{0}] hkey:[{1}] fail ", e, redisKey, hKey);
}
return false;
}
public boolean sadd(String key, String... value) {
String redisKey = this.rebuildKey(key);
try {
Long add = ().add(redisKey, value);
return (add) && add > 0;
} catch (Exception e) {
("sadd value:[{0}] to key:[{1}] fail", redisKey, value, e);
}
return false;
}
public Set<String> smembers(String key) {
String redisKey = this.rebuildKey(key);
try {
return ().members(redisKey);
} catch (Exception e) {
("scard values from key:[{0}] fail", redisKey, e);
}
return new HashSet();
}
public boolean szset(String key, String value, double score) {
String redisKey = this.rebuildKey(key);
try {
return ().add(redisKey, value, score);
}catch (Exception e){
("szset value:[{0}] from key:[{1}] fail", value, redisKey, e);
}
return false;
}
public Long szdel(String key, String... value) {
String redisKey = this.rebuildKey(key);
try {
return ().remove(redisKey, value);
}catch (Exception e){
("szdel values:[{0}] from key:[{1}] fail", value, redisKey, e);
}
return 0L;
}
public Set<String> szrange(String key, long start, long end) {
String redisKey = this.rebuildKey(key);
try {
return ().range(redisKey, start, end);
}catch (Exception e){
("szrange start:[{0}] end:[{1}] from key:[{2}] fail", start, end, redisKey, e);
}
return null;
}
public Set<String> szdelAndGet(String key, long start, long end) {
String redisKey = this.rebuildKey(key);
RLock lock = (redisKey+"_Lock");
try {
();
ZSetOperations<String, String> operation = ();
Set<String> set = (redisKey, start, end);
if(null == set || ()==0)return set;
(redisKey, ());
return set;
}catch (Exception e){
("szdelAndGet start:[{0}] end:[{1}] from key:[{2}] fail", start, end, redisKey, e);
}finally {
();
}
return null;
}
public Double szscore(String key, String value) {
String redisKey = this.rebuildKey(key);
try {
return ().score(redisKey, value);
}catch (Exception e){
("szscore value:[{0}] from key:[{1}] fail", value, redisKey, e);
}
return null;
}
public Long lLeftPush(String key, String value) {
String redisKey = this.rebuildKey(key);
try {
return ().leftPush(redisKey, value);
}catch (Exception e){
("lLeftPush value:[{0}] from key:[{1}] fail", value, redisKey, e);
}
return null;
}
public Long lLeftPushAll(String key, Collection<String> values) {
String redisKey = this.rebuildKey(key);
try {
return ().leftPushAll(key, values);
}catch (Exception e){
("lLeftPushAll values:[{0}] from key:[{1}] fail", values, redisKey, e);
}
return null;
}
public String lRightPop(String key, long expire, TimeUnit unit) {
String redisKey = this.rebuildKey(key);
try {
return ().rightPop(key, expire, unit);
}catch (Exception e){
("lRightPop key:[{1}] fail", redisKey, e);
}
return null;
}
public RLock getLock(String key) {
String redisKey = this.rebuildKey(key);
try {
RLock lock = (redisKey);
return lock;
} catch (Exception e) {
("getLock key:[{0}] fail ", e, redisKey);
}
return null;
}
///
// private functions
///
private String rebuildKey(String redisKey) {
return namespace + redisKey;
}
}