|
@@ -5,6 +5,8 @@ import com.yonge.cooleshow.common.constant.SysConfigConstant;
|
|
|
import com.yonge.toolset.base.exception.BizException;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.redisson.api.RBucket;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -30,6 +32,8 @@ public class WordContext {
|
|
|
|
|
|
@Autowired
|
|
|
private SysConfigService sysConfigService;
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
|
|
/**
|
|
|
* 敏感词字典
|
|
@@ -49,16 +53,39 @@ public class WordContext {
|
|
|
*/
|
|
|
private String whiteList;
|
|
|
|
|
|
+ //上传更新时间戳
|
|
|
+ private Long updateTime;
|
|
|
+
|
|
|
+ public boolean isInit(){
|
|
|
+ RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
|
|
|
+ boolean exists = bucket.isExists();
|
|
|
+ if(!exists){
|
|
|
+ return true;
|
|
|
+ }else {
|
|
|
+ Long lastModified = bucket.get();
|
|
|
+ if(!Objects.equals(updateTime,lastModified)){
|
|
|
+ updateTime = lastModified;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
+ initKeyWord1();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initKeyWord1() {
|
|
|
try {
|
|
|
- this.blackList = FileUtils.readFileToString(new File("/root/blacklist.txt"), "UTF-8");
|
|
|
+ RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
|
|
|
+ File file = new File(SysConfigConstant.BLACK_LIST_FILE_PATH);
|
|
|
+ this.blackList = FileUtils.readFileToString(file, "UTF-8");
|
|
|
+ bucket.set(file.lastModified());
|
|
|
+ initKeyWord();
|
|
|
} catch (IOException e) {
|
|
|
log.error("读取黑名单词库失败", e);
|
|
|
}
|
|
|
-// this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
|
|
|
-// this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
|
|
|
- initKeyWord();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -131,65 +158,11 @@ public class WordContext {
|
|
|
*/
|
|
|
public void mapInit() {
|
|
|
init = false;
|
|
|
- this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
|
|
|
- this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
|
|
|
wordMap.clear();
|
|
|
- initKeyWord();
|
|
|
- /*Map nowMap;
|
|
|
- for (String key : wordList) {
|
|
|
- List<Map> cacheList = new ArrayList<>();
|
|
|
- nowMap = wordMap;
|
|
|
- for (int i = 0; i < key.length(); i++) {
|
|
|
- char keyChar = key.charAt(i);
|
|
|
-
|
|
|
- Object map = nowMap.get(keyChar);
|
|
|
- if (map != null) {
|
|
|
- nowMap = (Map) map;
|
|
|
- cacheList.add(nowMap);
|
|
|
- } else {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (i == key.length() - 1) {
|
|
|
- char[] keys = key.toCharArray();
|
|
|
- boolean cleanable = false;
|
|
|
- char lastChar = 0;
|
|
|
- for (int j = cacheList.size() - 1; j >= 0; j--) {
|
|
|
- Map cacheMap = cacheList.get(j);
|
|
|
- if (j == cacheList.size() - 1) {
|
|
|
- if (String.valueOf(WordType.BLACK.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
|
|
- if (wordType == WordType.WHITE) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- if (String.valueOf(WordType.WHITE.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
|
|
- if (wordType == WordType.BLACK) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- cacheMap.remove("isWhiteWord");
|
|
|
- cacheMap.remove("isEnd");
|
|
|
- if (cacheMap.size() == 0) {
|
|
|
- cleanable = true;
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- if (cleanable) {
|
|
|
- Object isEnd = cacheMap.get("isEnd");
|
|
|
- if (String.valueOf(EndType.IS_END.ordinal()).equals(isEnd)) {
|
|
|
- cleanable = false;
|
|
|
- }
|
|
|
- cacheMap.remove(lastChar);
|
|
|
- }
|
|
|
- lastChar = keys[j];
|
|
|
- }
|
|
|
-
|
|
|
- if (cleanable) {
|
|
|
- wordMap.remove(lastChar);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }*/
|
|
|
+ initKeyWord1();
|
|
|
+// this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
|
|
|
+// this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
|
|
|
+// initKeyWord();
|
|
|
}
|
|
|
|
|
|
/**
|