1:将下发配置使用的springdataredis改为jedis方式下发
2:注释configredisserviceimpl实现的接口防止spring加载时bean冲突 3:还原pom文件到上个版本 4:添加读取每类业务下有多少表的方法
This commit is contained in:
35
pom.xml
35
pom.xml
@@ -39,32 +39,11 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/*.properties</include>
|
||||
<include>**/*.xml</include>
|
||||
<include>**/*.xsd</include>
|
||||
<include>**/*.yml</include>
|
||||
</includes>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*.properties</include>
|
||||
<include>**/*.xml</include>
|
||||
</includes>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
|
||||
</resources>
|
||||
|
||||
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -628,7 +607,13 @@
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jdk.tools</groupId>
|
||||
<artifactId>jdk.tools</artifactId>
|
||||
<version>1.7</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
|
||||
845
src/main/java/com/nis/util/JedisUtils.java
Normal file
845
src/main/java/com/nis/util/JedisUtils.java
Normal file
@@ -0,0 +1,845 @@
|
||||
package com.nis.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.nis.web.service.SpringContextHolder;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
public class JedisUtils {
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
|
||||
|
||||
private static JedisPool jedisPool = SpringContextHolder.getBean(JedisPool.class);
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static String get(String key, int redisDb) {
|
||||
String value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
|
||||
if (jedis.exists(key)) {
|
||||
value = jedis.get(key);
|
||||
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
|
||||
logger.debug("get {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("get {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Object getObject(String key, int redisDb) {
|
||||
Object value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
value = toObject(jedis.get(getBytesKey(key)));
|
||||
logger.debug("getObject {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getObject {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>可以作为获取唯一id的方法</b><br/>
|
||||
* 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
|
||||
*
|
||||
* @param String
|
||||
* key
|
||||
* @param long number 要减去的值
|
||||
* @return long 相加后的值
|
||||
* */
|
||||
public static long incrBy(String key, long number,int redisDb) {
|
||||
Jedis jedis = getResource(redisDb);
|
||||
long len = jedis.incrBy(key, number);
|
||||
returnResource(jedis);
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static String set(String key, String value, int cacheSeconds, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.set(key, value);
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("set {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("set {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static String setObject(String key, Object value, int cacheSeconds, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.set(getBytesKey(key), toBytes(value));
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setObject {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setObject {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取List缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static List<String> getList(String key, int redisDb) {
|
||||
List<String> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
value = jedis.lrange(key, 0, -1);
|
||||
logger.debug("getList {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getList {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取List缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static List<Object> getObjectList(String key, int redisDb) {
|
||||
List<Object> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
List<byte[]> list = jedis.lrange(getBytesKey(key), 0, -1);
|
||||
value = Lists.newArrayList();
|
||||
for (byte[] bs : list) {
|
||||
value.add(toObject(bs));
|
||||
}
|
||||
logger.debug("getObjectList {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getObjectList {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置List缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static long setList(String key, List<String> value, int cacheSeconds, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
jedis.del(key);
|
||||
}
|
||||
result = jedis.rpush(key, (String[]) value.toArray());
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setList {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setList {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置List缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static long setObjectList(String key, List<Object> value, int cacheSeconds, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
jedis.del(key);
|
||||
}
|
||||
List<byte[]> list = Lists.newArrayList();
|
||||
for (Object o : value) {
|
||||
list.add(toBytes(o));
|
||||
}
|
||||
result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setObjectList {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setObjectList {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向List缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long listAdd(String key, int redisDb, String... value) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.rpush(key, value);
|
||||
logger.debug("listAdd {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("listAdd {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向List缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long listObjectAdd(String key, int redisDb, Object... value) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
List<byte[]> list = Lists.newArrayList();
|
||||
for (Object o : value) {
|
||||
list.add(toBytes(o));
|
||||
}
|
||||
result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
|
||||
logger.debug("listObjectAdd {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("listObjectAdd {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Set<String> getSet(String key, int redisDb) {
|
||||
Set<String> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
value = jedis.smembers(key);
|
||||
logger.debug("getSet {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getSet {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Set<Object> getObjectSet(String key, int redisDb) {
|
||||
Set<Object> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
value = Sets.newHashSet();
|
||||
Set<byte[]> set = jedis.smembers(getBytesKey(key));
|
||||
for (byte[] bs : set) {
|
||||
value.add(toObject(bs));
|
||||
}
|
||||
logger.debug("getObjectSet {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getObjectSet {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Set缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static long setSet(String key, Set<String> value, int cacheSeconds, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
jedis.del(key);
|
||||
}
|
||||
result = jedis.sadd(key, (String[]) value.toArray());
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setSet {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setSet {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Set缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static long setObjectSet(String key, Set<Object> value, int cacheSeconds, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
jedis.del(key);
|
||||
}
|
||||
Set<byte[]> set = Sets.newHashSet();
|
||||
for (Object o : value) {
|
||||
set.add(toBytes(o));
|
||||
}
|
||||
result = jedis.sadd(getBytesKey(key), (byte[][]) set.toArray());
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setObjectSet {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setObjectSet {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向Set缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long setSetAdd(String key, int redisDb, String... value) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.sadd(key, value);
|
||||
logger.debug("setSetAdd {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setSetAdd {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向Set缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long setSetObjectAdd(String key, int redisDb, Object... value) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
Set<byte[]> set = Sets.newHashSet();
|
||||
for (Object o : value) {
|
||||
set.add(toBytes(o));
|
||||
}
|
||||
result = jedis.rpush(getBytesKey(key), (byte[][]) set.toArray());
|
||||
logger.debug("setSetObjectAdd {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setSetObjectAdd {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Map缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Map<String, String> getMap(String key, int redisDb) {
|
||||
Map<String, String> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
value = jedis.hgetAll(key);
|
||||
logger.debug("getMap {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getMap {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Map缓存
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public static Map<String, Object> getObjectMap(String key, int redisDb) {
|
||||
Map<String, Object> value = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
value = Maps.newHashMap();
|
||||
Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));
|
||||
for (Map.Entry<byte[], byte[]> e : map.entrySet()) {
|
||||
value.put(StringUtils.toString(e.getKey()), toObject(e.getValue()));
|
||||
}
|
||||
logger.debug("getObjectMap {} = {}", key, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("getObjectMap {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Map缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static String setMap(String key, Map<String, String> value, int cacheSeconds, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
jedis.del(key);
|
||||
}
|
||||
result = jedis.hmset(key, value);
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setMap {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setMap {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Map缓存
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param cacheSeconds 超时时间,0为不超时
|
||||
* @return
|
||||
*/
|
||||
public static String setObjectMap(String key, Map<String, Object> value, int cacheSeconds, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
jedis.del(key);
|
||||
}
|
||||
Map<byte[], byte[]> map = Maps.newHashMap();
|
||||
for (Map.Entry<String, Object> e : value.entrySet()) {
|
||||
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
|
||||
}
|
||||
result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
|
||||
if (cacheSeconds != 0) {
|
||||
jedis.expire(key, cacheSeconds);
|
||||
}
|
||||
logger.debug("setObjectMap {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("setObjectMap {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向Map缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static String mapPut(String key, Map<String, String> value, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.hmset(key, value);
|
||||
logger.debug("mapPut {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapPut {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向Map缓存中添加值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static String mapObjectPut(String key, Map<String, Object> value, int redisDb) {
|
||||
String result = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
Map<byte[], byte[]> map = Maps.newHashMap();
|
||||
for (Map.Entry<String, Object> e : value.entrySet()) {
|
||||
map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
|
||||
}
|
||||
result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
|
||||
logger.debug("mapObjectPut {} = {}", key, value);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapObjectPut {} = {}", key, value, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除Map缓存中的值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long mapRemove(String key, String mapKey, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.hdel(key, mapKey);
|
||||
logger.debug("mapRemove {} {}", key, mapKey);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapRemove {} {}", key, mapKey, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除Map缓存中的值
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static long mapObjectRemove(String key, String mapKey, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
|
||||
logger.debug("mapObjectRemove {} {}", key, mapKey);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapObjectRemove {} {}", key, mapKey, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断Map缓存中的Key是否存在
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static boolean mapExists(String key, String mapKey, int redisDb) {
|
||||
boolean result = false;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.hexists(key, mapKey);
|
||||
logger.debug("mapExists {} {}", key, mapKey);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapExists {} {}", key, mapKey, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断Map缓存中的Key是否存在
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static boolean mapObjectExists(String key, String mapKey, int redisDb) {
|
||||
boolean result = false;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
|
||||
logger.debug("mapObjectExists {} {}", key, mapKey);
|
||||
} catch (Exception e) {
|
||||
logger.warn("mapObjectExists {} {}", key, mapKey, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public static long del(String key, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(key)) {
|
||||
result = jedis.del(key);
|
||||
logger.debug("del {}", key);
|
||||
} else {
|
||||
logger.debug("del {} not exists", key);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("del {}", key, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public static long delObject(String key, int redisDb) {
|
||||
long result = 0;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
if (jedis.exists(getBytesKey(key))) {
|
||||
result = jedis.del(getBytesKey(key));
|
||||
logger.debug("delObject {}", key);
|
||||
} else {
|
||||
logger.debug("delObject {} not exists", key);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("delObject {}", key, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存是否存在
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public static boolean exists(String key, int redisDb) {
|
||||
boolean result = false;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.exists(key);
|
||||
logger.debug("exists {}", key);
|
||||
} catch (Exception e) {
|
||||
logger.warn("exists {}", key, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存是否存在
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public static boolean existsObject(String key, int redisDb) {
|
||||
boolean result = false;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = getResource(redisDb);
|
||||
result = jedis.exists(getBytesKey(key));
|
||||
logger.debug("existsObject {}", key);
|
||||
} catch (Exception e) {
|
||||
logger.warn("existsObject {}", key, e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资源
|
||||
* @return
|
||||
* @throws JedisException
|
||||
*/
|
||||
public static Jedis getResource(int redisDb) throws JedisException {
|
||||
Jedis jedis = null;
|
||||
if (jedisPool == null) {
|
||||
throw new RuntimeException("后台错误:redis连接池为空,请联系管理员检查程序");
|
||||
}
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
jedis.select(redisDb);
|
||||
} catch (JedisException e) {
|
||||
returnBrokenResource(jedis);
|
||||
throw new RuntimeException("后台错误:获取redis连接失败,请联系管理员检查程序", e);
|
||||
}
|
||||
return jedis;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 归还资源
|
||||
* @param jedis
|
||||
* @param isBroken
|
||||
*/
|
||||
public static void returnBrokenResource(Jedis jedis) {
|
||||
if (jedis != null) {
|
||||
// jedisPool.returnBrokenResource(jedis);
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
* @param jedis
|
||||
* @param isBroken
|
||||
*/
|
||||
public static void returnResource(Jedis jedis) {
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取byte[]类型Key
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getBytesKey(Object object) {
|
||||
if (object instanceof String) {
|
||||
return StringUtils.getBytes((String) object);
|
||||
} else {
|
||||
return ObjectUtils.serialize(object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Object转换byte[]类型
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static byte[] toBytes(Object object) {
|
||||
return ObjectUtils.serialize(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* byte[]型转换Object
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Object toObject(byte[] bytes) {
|
||||
return ObjectUtils.unserialize(bytes);
|
||||
}
|
||||
|
||||
}
|
||||
1422
src/main/java/com/nis/util/JedisUtils2.java
Normal file
1422
src/main/java/com/nis/util/JedisUtils2.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,11 +2,16 @@ package com.nis.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.nis.domain.MaatXmlExpr;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -140,6 +145,92 @@ public class ServiceAndRDBIndexReal {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
getUnMaatTable();
|
||||
}
|
||||
|
||||
public static void getUnMaatTable() {
|
||||
Map<Integer, String> typeTable = new HashMap<Integer, String>();
|
||||
|
||||
String unMaatService = Configurations.getStringProperty("unMaatService", "");
|
||||
if (unMaatService != null && !unMaatService.trim().equals("")) {
|
||||
String[] split = unMaatService.split(";");
|
||||
for (String str : split) {
|
||||
String[] serviceAction = str.split(":");
|
||||
String serviceType = Configurations.getStringProperty(serviceAction[0], "");
|
||||
if (serviceType != null && !serviceType.trim().equals("")) {
|
||||
String[] typeArrs = serviceType.split(";");
|
||||
for (String typeStr : typeArrs) {
|
||||
String[] typeArr = typeStr.split(":");
|
||||
String tableNameArr[] = typeArr[1].split(",");
|
||||
for (String tableName : tableNameArr) {
|
||||
if (!typeTable.containsKey(Integer.parseInt(serviceAction[0]))) {
|
||||
typeTable.put(Integer.parseInt(serviceAction[0]), tableName.toUpperCase());
|
||||
} else {
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
unMaatSercieNameMap.put(Integer.parseInt(serviceAction[0]), tableName.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Integer service : typeTable.keySet()) {
|
||||
List<MaatXmlExpr> expressionList = ReadMaatXmlUtil.getMaatConfigByService(service).getExpressionList();
|
||||
for (MaatXmlExpr maatXmlExpr : expressionList) {
|
||||
if (maatXmlExpr.getValueExpression() != null) {
|
||||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||||
int a=1;
|
||||
for (int i = 0; i < valSplit.length; i++) {
|
||||
if (valSplit[i].toLowerCase().contains("is_valid")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||||
System.out.println(service+"--"+typeTable.get(service) + "---" + a);
|
||||
}else if (valSplit[i].toLowerCase().contains("[")){
|
||||
a++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void getMaatTable() {
|
||||
Map<Integer, Set<String>> typeMap = new HashMap<Integer, Set<String>>();
|
||||
String service = Configurations.getStringProperty("service", "");
|
||||
if (service != null && !service.trim().equals("")) {
|
||||
String[] split = service.split(";");
|
||||
for (String str : split) {
|
||||
String[] serviceAction = str.split(":");
|
||||
serviceActionMap.put(Integer.valueOf(serviceAction[0]), Integer.valueOf(serviceAction[1]));
|
||||
String type = Configurations.getStringProperty(serviceAction[0], "");
|
||||
if (type != null && !type.trim().equals("")) {
|
||||
|
||||
String[] typeArrs = type.split(";");
|
||||
for (String typeStr : typeArrs) {
|
||||
String[] typeArr = typeStr.split(":");
|
||||
int tableType = Integer.parseInt(typeArr[0]);
|
||||
String tableNameArr[] = typeArr[1].split(",");
|
||||
for (String tableName : tableNameArr) {
|
||||
if (typeMap.containsKey(tableType)) {
|
||||
typeMap.get(tableType).add(tableName.toUpperCase());
|
||||
} else {
|
||||
Set<String> list = new HashSet<String>();
|
||||
list.add(tableName.toUpperCase());
|
||||
typeMap.put(tableType, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
for (Integer type : typeMap.keySet()) {
|
||||
System.out.println(type + "----" + typeMap.get(type));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据业务类型获取这个
|
||||
* @param service
|
||||
@@ -183,7 +274,8 @@ public class ServiceAndRDBIndexReal {
|
||||
return tableList.get(index);
|
||||
} else {
|
||||
logger.error("未从业务类型和表对应关系中,找到业务类型:{},配置类型:{}表名:{}对应的真实表名", service, type, tableName);
|
||||
throw new RuntimeException("后台错误:未从业务类型和表对应关系中,找到业务类型:"+service+",配置类型:"+type+"表名:"+tableName+"对应的真实表名");
|
||||
throw new RuntimeException(
|
||||
"后台错误:未从业务类型和表对应关系中,找到业务类型:" + service + ",配置类型:" + type + "表名:" + tableName + "对应的真实表名");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -209,7 +301,6 @@ public class ServiceAndRDBIndexReal {
|
||||
return serviceActionMap.get(service);
|
||||
}
|
||||
|
||||
|
||||
public static Map<Integer, Map<Integer, List<String>>> getSercieNameMap() {
|
||||
return sercieNameMap;
|
||||
}
|
||||
@@ -230,10 +321,10 @@ public class ServiceAndRDBIndexReal {
|
||||
return maatToValveMap;
|
||||
}
|
||||
|
||||
public static void setMaatToValveMap(
|
||||
Map<Integer, Map<String, String[]>> maatToValveMap) {
|
||||
public static void setMaatToValveMap(Map<Integer, Map<String, String[]>> maatToValveMap) {
|
||||
ServiceAndRDBIndexReal.maatToValveMap = maatToValveMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param valveDBIndex the valveDBIndex to set
|
||||
*/
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -26,7 +26,7 @@ import com.nis.web.dao.impl.BaseRedisDao;
|
||||
import com.nis.web.service.SpringContextHolder;
|
||||
|
||||
@Service()
|
||||
public class ConfigRedisServiceimpl implements ConfigRedisService {
|
||||
public class ConfigRedisServiceimpl {// implements ConfigRedisService
|
||||
private static Logger logger = LoggerFactory.getLogger(ConfigRedisServiceimpl.class);
|
||||
|
||||
@Transactional
|
||||
@@ -452,7 +452,6 @@ public class ConfigRedisServiceimpl implements ConfigRedisService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean saveMaatConfig(Map<Integer, List<MaatConfig>> configMap) {
|
||||
if (configMap != null && configMap.size() > 0) {
|
||||
@@ -734,7 +733,6 @@ public class ConfigRedisServiceimpl implements ConfigRedisService {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean delUnMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
||||
if (idMap != null && idMap.size() > 0) {
|
||||
@@ -881,7 +879,6 @@ public class ConfigRedisServiceimpl implements ConfigRedisService {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean delMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap) {
|
||||
if (idMap != null && idMap.size() > 0) {
|
||||
@@ -1176,8 +1173,9 @@ public class ConfigRedisServiceimpl implements ConfigRedisService {
|
||||
redisTemplate.rename(effectiveRuleKey.toUpperCase(), effectiveRuleKey
|
||||
.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
|
||||
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是:{}",
|
||||
redisStatisticsRealDBIndex, effectiveRuleKey.toUpperCase(), effectiveRuleKey
|
||||
.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
|
||||
redisStatisticsRealDBIndex, effectiveRuleKey.toUpperCase(),
|
||||
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE",
|
||||
"OBSOLETE_RULE"));
|
||||
|
||||
String zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "DEL,");
|
||||
redisTemplate.boundZSetOps("MAAT_UPDATE_STATUS").add(zset, maatVersion);
|
||||
|
||||
@@ -25,6 +25,15 @@
|
||||
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
|
||||
<property name="testOnReturn" value="${redis.testOnReturn}" />
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
|
||||
<constructor-arg name="poolConfig" ref="poolConfig" />
|
||||
<constructor-arg name="host" value="${redis.host}" />
|
||||
<constructor-arg name="port" value="${redis.port}" type="int" />
|
||||
</bean>
|
||||
|
||||
|
||||
<!-- 默认使用数据库0,设置connectionFactory为非单例模式,方便选择redis数据库 -->
|
||||
<bean id="connectionFactory0"
|
||||
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
|
||||
|
||||
Reference in New Issue
Block a user