记得上下班打卡 | git大法好,push需谨慎

Commit 614c05f8 authored by 张国柄's avatar 张国柄

~config:redis.key+prefix;

parent 06142cfc
......@@ -18,6 +18,11 @@ import java.util.concurrent.TimeUnit;
* @date 2020/8/26 13:11
*/
public abstract class AbstractRedisUtil {
/**
* 补全KEY前缀
*/
public abstract String fillingKey(String key);
/**
* 获取redis初始化信息
*/
......@@ -48,6 +53,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean expire(String key, long time) {
key = this.fillingKey(key);
if (time > 0) {
......@@ -56,7 +62,6 @@ public abstract class AbstractRedisUtil {
}
return true;
}
......@@ -68,9 +73,9 @@ public abstract class AbstractRedisUtil {
*/
public long getExpire(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).getExpire(key, TimeUnit.SECONDS);
}
......@@ -82,9 +87,9 @@ public abstract class AbstractRedisUtil {
*/
public boolean hasKey(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).hasKey(key);
}
/**
......@@ -93,6 +98,8 @@ public abstract class AbstractRedisUtil {
* @param prefix Key前缀
*/
public void delKeysByPrefix(String prefix) {
prefix = this.fillingKey(prefix);
if (null != prefix && prefix.trim().length() > 0) {
for (Integer key : this.getRedisConfig().redisTemplateMap.keySet()) {
Set<String> keys = this.getRedisConfig().getRedisTemplateByDb(key).keys(prefix.concat("*"));
......@@ -118,13 +125,15 @@ public abstract class AbstractRedisUtil {
if (key != null && key.length > 0) {
if (key.length == 1) {
String keyStr = this.fillingKey(key[0]);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key[0])).delete(key[0]);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(keyStr)).delete(keyStr);
} else {
// redisTemplate.delete(CollectionUtils.arrayToList(key));
for (String keyStr : key) {
keyStr = this.fillingKey(keyStr);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(keyStr)).delete(keyStr);
}
}
......@@ -150,6 +159,7 @@ public abstract class AbstractRedisUtil {
hashMap = hashMapIntegerArrayList();
//构建 需要删除的Redis HashMapDBKey
for (String keyStr : key) {
keyStr = this.fillingKey(keyStr);
Integer dbPosition = this.getIndex(keyStr);
ArrayList<String> dbArray;
if (hashMap.containsKey(dbPosition)) {
......@@ -179,9 +189,13 @@ public abstract class AbstractRedisUtil {
*/
public Object get(String key) {
if (null == key) {
return null;
}
return key == null ? null : this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().get(key);
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().get(key);
}
......@@ -194,13 +208,11 @@ public abstract class AbstractRedisUtil {
*/
public boolean set(String key, Object value) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().set(key, value);
return true;
}
......@@ -214,7 +226,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean set(String key, Object value, long time) {
key = this.fillingKey(key);
if (time > 0) {
RedisTemplate<String, Object> redisTemplate = this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key));
......@@ -227,11 +239,8 @@ public abstract class AbstractRedisUtil {
}
return true;
}
/**
* 递增
*
......@@ -239,7 +248,6 @@ public abstract class AbstractRedisUtil {
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
......@@ -248,8 +256,9 @@ public abstract class AbstractRedisUtil {
}
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().increment(key, delta);
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().increment(key, delta);
}
......@@ -269,8 +278,9 @@ public abstract class AbstractRedisUtil {
}
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().increment(key, -delta);
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForValue().increment(key, -delta);
}
......@@ -283,12 +293,15 @@ public abstract class AbstractRedisUtil {
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().get(key, item);
}
public Object hkeys(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().keys(key);
}
......@@ -301,9 +314,9 @@ public abstract class AbstractRedisUtil {
*/
public Map<Object, Object> hmget(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().entries(key);
}
......@@ -316,13 +329,11 @@ public abstract class AbstractRedisUtil {
*/
public boolean hmset(String key, Map<String, Object> map) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().putAll(key, map);
return true;
}
......@@ -336,7 +347,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().putAll(key, map);
......@@ -345,10 +356,7 @@ public abstract class AbstractRedisUtil {
expire(key, time);
}
return true;
}
......@@ -362,13 +370,11 @@ public abstract class AbstractRedisUtil {
*/
public boolean hset(String key, String item, Object value) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().put(key, item, value);
return true;
}
......@@ -383,7 +389,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean hset(String key, String item, Object value, long time) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().put(key, item, value);
......@@ -394,8 +400,6 @@ public abstract class AbstractRedisUtil {
}
return true;
}
......@@ -407,9 +411,9 @@ public abstract class AbstractRedisUtil {
*/
public void hdel(String key, Object... item) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().delete(key, item);
}
......@@ -422,9 +426,9 @@ public abstract class AbstractRedisUtil {
*/
public boolean hHasKey(String key, String item) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().hasKey(key, item);
}
......@@ -438,9 +442,9 @@ public abstract class AbstractRedisUtil {
*/
public double hincr(String key, String item, double by) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().increment(key, item, by);
}
......@@ -454,9 +458,9 @@ public abstract class AbstractRedisUtil {
*/
public double hdecr(String key, String item, double by) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForHash().increment(key, item, -by);
}
......@@ -470,11 +474,9 @@ public abstract class AbstractRedisUtil {
*/
public Set<Object> sGet(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().members(key);
}
......@@ -487,11 +489,9 @@ public abstract class AbstractRedisUtil {
*/
public boolean sHasKey(String key, Object value) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().isMember(key, value);
}
......@@ -504,11 +504,9 @@ public abstract class AbstractRedisUtil {
*/
public long sSet(String key, Object... values) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().add(key, values);
}
......@@ -522,7 +520,7 @@ public abstract class AbstractRedisUtil {
*/
public long sSetAndTime(String key, long time, Object... values) {
key = this.fillingKey(key);
Long count = this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().add(key, values);
......@@ -531,8 +529,6 @@ public abstract class AbstractRedisUtil {
expire(key, time);
return count;
}
......@@ -542,13 +538,10 @@ public abstract class AbstractRedisUtil {
* @param key 键
* @return
*/
public long sGetSetSize(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().size(key);
}
......@@ -559,15 +552,12 @@ public abstract class AbstractRedisUtil {
* @param values 值 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
key = this.fillingKey(key);
Long count = this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForSet().remove(key, values);
return count;
}
// ===============================list=================================
......@@ -583,11 +573,9 @@ public abstract class AbstractRedisUtil {
*/
public List<Object> lGet(String key, long start, long end) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().range(key, start, end);
}
......@@ -599,11 +587,9 @@ public abstract class AbstractRedisUtil {
*/
public long lGetListSize(String key) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().size(key);
}
......@@ -616,11 +602,9 @@ public abstract class AbstractRedisUtil {
*/
public Object lGetIndex(String key, long index) {
key = this.fillingKey(key);
return this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().index(key, index);
}
......@@ -632,13 +616,11 @@ public abstract class AbstractRedisUtil {
* @return
*/
public boolean lSet(String key, Object value) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().rightPush(key, value);
return true;
}
......@@ -652,7 +634,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean lSet(String key, Object value, long time) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().rightPush(key, value);
......@@ -661,8 +643,6 @@ public abstract class AbstractRedisUtil {
expire(key, time);
return true;
}
......@@ -674,13 +654,11 @@ public abstract class AbstractRedisUtil {
* @return
*/
public boolean lSet(String key, List<Object> value) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().rightPushAll(key, value);
return true;
}
......@@ -694,7 +672,7 @@ public abstract class AbstractRedisUtil {
*/
public boolean lSet(String key, List<Object> value, long time) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().rightPushAll(key, value);
......@@ -703,8 +681,6 @@ public abstract class AbstractRedisUtil {
expire(key, time);
return true;
}
......@@ -718,13 +694,11 @@ public abstract class AbstractRedisUtil {
*/
public boolean lUpdateIndex(String key, long index, Object value) {
key = this.fillingKey(key);
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().set(key, index, value);
return true;
}
......@@ -736,15 +710,12 @@ public abstract class AbstractRedisUtil {
* @param value 值
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
key = this.fillingKey(key);
Long remove = this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).opsForList().remove(key, count, value);
return remove;
}
public RedisTemplate<String, Object> getRedisTemplateByDb(int db) {
......@@ -753,15 +724,20 @@ public abstract class AbstractRedisUtil {
public Object getDB15RedisHGet(String redisKey, String item) {
redisKey = this.fillingKey(redisKey);
return this.getRedisConfig().getRedisTemplateByDb(15).opsForHash().get(redisKey, item);
}
public Object getDB15RedisGet(String redisKey) {
redisKey = this.fillingKey(redisKey);
return this.getRedisConfig().getRedisTemplateByDb(15).opsForValue().get(redisKey);
}
public boolean getDB15RedisHasKey(String redisKey, String item) {
redisKey = this.fillingKey(redisKey);
return this.getRedisConfig().getRedisTemplateByDb(15).opsForSet().isMember(redisKey, item);
}
......@@ -773,6 +749,8 @@ public abstract class AbstractRedisUtil {
* @return
*/
public boolean lock(String key, int value, long releaseTime) {
key = this.fillingKey(key);
// 尝试获取锁
RedisTemplate<String, Object> redisTemplate = this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key));
Boolean boo = redisTemplate.opsForValue().setIfAbsent(key, value, releaseTime, TimeUnit.SECONDS);
......@@ -785,6 +763,7 @@ public abstract class AbstractRedisUtil {
** @param key
*/
public void uLock(String key) {
key = this.fillingKey(key);
// 删除key即可释放锁
this.getRedisConfig().getRedisTemplateByDb(this.getIndex(key)).delete(key);
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisAdamConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,10 +21,17 @@ import org.springframework.stereotype.Component;
public final class RedisAdamUtil extends AbstractRedisUtil{
@Autowired
private RedisAdamConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisAdamUtil.totalDbs===",redisConfig.totalDbs);
log.info("redisAdamUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisCandyConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -19,10 +20,17 @@ import org.springframework.stereotype.Component;
public final class RedisCandyUtil extends AbstractRedisUtil{
@Autowired
private RedisCandyConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisCandyUtil.totalDbs===",redisConfig.totalDbs);
log.info("redisCandyUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisDragonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,15 +21,20 @@ import org.springframework.stereotype.Component;
public final class RedisDragonUtil extends AbstractRedisUtil{
@Autowired
private RedisDragonConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisDragonUtil.totalDbs===",redisConfig.totalDbs);
log.info("redisDragonUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
@Override
AbstractRedisConfig getRedisConfig() {
return this.redisConfig;
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisGalaxyConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,10 +21,17 @@ import org.springframework.stereotype.Component;
public final class RedisGalaxyUtil extends AbstractRedisUtil{
@Autowired
private RedisGalaxyConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("RedisGalaxyUtil.totalDbs===",redisConfig.totalDbs);
log.info("RedisGalaxyUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
package com.liquidnet.common.cache.redis.util;
import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisAdamConfig;
import com.liquidnet.common.cache.redis.config.RedisGoblinConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -21,10 +21,17 @@ import org.springframework.stereotype.Component;
public final class RedisGoblinUtil extends AbstractRedisUtil {
@Autowired
private RedisGoblinConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisAdamUtil.totalDbs===", redisConfig.totalDbs);
log.info("redisAdamUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisKylinConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,15 +21,20 @@ import org.springframework.stereotype.Component;
public final class RedisKylinUtil extends AbstractRedisUtil{
@Autowired
private RedisKylinConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisKylinUtil.totalDbs===",redisConfig.totalDbs);
log.info("redisKylinUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
@Override
AbstractRedisConfig getRedisConfig() {
return this.redisConfig;
......
......@@ -21,9 +21,14 @@ public final class RedisQueueUtil extends AbstractRedisUtil{
@Autowired
private RedisQueueConfig redisConfig;
@Override
public String fillingKey(String key) {
return key;
}
@Override
public int getDbs() {
log.info("RedisQueueUtil.totalDbs===",redisConfig.totalDbs);
log.info("RedisQueueUtil.totalDbs:{}===", redisConfig.totalDbs);
return redisConfig.totalDbs;
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisSweetConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,10 +21,17 @@ import org.springframework.stereotype.Component;
public final class RedisSweetUtil extends AbstractRedisUtil{
@Autowired
private RedisSweetConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
log.info("redisSweetUtil.totalDbs===",redisConfig.totalDbs);
log.info("redisSweetUtil.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
......@@ -4,6 +4,7 @@ import com.liquidnet.common.cache.redis.config.AbstractRedisConfig;
import com.liquidnet.common.cache.redis.config.RedisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
......@@ -20,10 +21,17 @@ import org.springframework.stereotype.Component;
public final class RedisUtil extends AbstractRedisUtil{
@Autowired
private RedisConfig redisConfig;
@Value("${spring.profiles.active:prod}")
private String prefix;
@Override
public String fillingKey(String key) {
return prefix.equals("prod") ? key : prefix.concat(":").concat(key);
}
@Override
public int getDbs() {
// log.info("redisConfig.totalDbs===",redisConfig.totalDbs);
log.info("redisConfig.totalDbs:{},prefix:{}===", redisConfig.totalDbs, prefix);
return redisConfig.totalDbs;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment