[Add] 增加redisAddr启动参数以通过参数指定redis缓存库地址;

[Update] 补充Javadoc;
[Change] 根据性能分析结果调整imageCacheExecutor线程池参数;
[Change] 调整CQGlobal内线程池参数;
[Change] 将illustPages缓存纳入Redis缓存库;
[Change] 根据性能分析结果将SpringBoot的HttpThreads降至1;
This commit is contained in:
LamGC 2020-03-31 11:04:08 +08:00
parent e4e7771d4d
commit 3333f4c5c5
4 changed files with 59 additions and 11 deletions

View File

@ -1,13 +1,27 @@
package net.lamgc.cgj; package net.lamgc.cgj;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.lz1998.cq.CQGlobal; import net.lz1998.cq.CQGlobal;
import net.lz1998.cq.EnableCQ; import net.lz1998.cq.EnableCQ;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@EnableCQ @EnableCQ
public class CQConfig { public class CQConfig {
public static void init() { public static void init() {
CQGlobal.pluginList.add(CQPluginMain.class); CQGlobal.pluginList.add(CQPluginMain.class);
CQGlobal.executor = new ThreadPoolExecutor(
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
Runtime.getRuntime().availableProcessors(),
25, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(512),
new ThreadFactoryBuilder()
.setNameFormat("Plugin-ProcessThread-%d")
.build()
);
} }
} }

View File

@ -1,6 +1,8 @@
package net.lamgc.cgj; package net.lamgc.cgj;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.*; import com.google.gson.*;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import net.lamgc.cgj.cache.*; import net.lamgc.cgj.cache.*;
@ -23,9 +25,7 @@ import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CQProcess { public class CQProcess {
@ -39,7 +39,7 @@ public class CQProcess {
.serializeNulls() .serializeNulls()
.create(); .create();
private final static URI redisServerUri = URI.create("redis://192.168.1.17"); private final static URI redisServerUri = URI.create("redis://" + System.getProperty("cgj.redisAddress"));
private final static Hashtable<String, File> imageCache = new Hashtable<>(); private final static Hashtable<String, File> imageCache = new Hashtable<>();
@ -49,15 +49,28 @@ public class CQProcess {
private final static JsonRedisCacheStore searchBodyCache = new JsonRedisCacheStore(redisServerUri, "searchBody", gson); private final static JsonRedisCacheStore searchBodyCache = new JsonRedisCacheStore(redisServerUri, "searchBody", gson);
private final static CacheStore<List<String>> pagesCache = new LocalHashCacheStore<>(); private final static CacheStore<List<String>> pagesCache = new RedisPoolCacheStore<List<String>>(redisServerUri, "imagePages") {
@Override
protected String parse(List<String> dataObj) {
return gson.toJson(dataObj);
}
@Override
protected List<String> analysis(String dataStr) {
return gson.fromJson(dataStr, new TypeToken<List<String>>(){}.getType());
}
};
private final static JsonRedisCacheStore rankingCache = new JsonRedisCacheStore(redisServerUri, "ranking", gson); private final static JsonRedisCacheStore rankingCache = new JsonRedisCacheStore(redisServerUri, "ranking", gson);
private final static EventExecutor imageCacheExecutor = new EventExecutor(new ThreadPoolExecutor( private final static EventExecutor imageCacheExecutor = new EventExecutor(new ThreadPoolExecutor(
1, Runtime.getRuntime().availableProcessors() >= 2 ? 2 : 1,
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F), (int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
15L, TimeUnit.SECONDS, 5L, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(30), new LinkedBlockingQueue<>(128),
new ThreadFactoryBuilder()
.setNameFormat("imageCacheThread-%d")
.build(),
new ThreadPoolExecutor.DiscardOldestPolicy() new ThreadPoolExecutor.DiscardOldestPolicy()
)); ));
@ -164,6 +177,7 @@ public class CQProcess {
@Argument(name = "contentOption", force = false) String contentOption, @Argument(name = "contentOption", force = false) String contentOption,
@Argument(name = "page", force = false, defaultValue = "1") int pagesIndex @Argument(name = "page", force = false, defaultValue = "1") int pagesIndex
) throws IOException { ) throws IOException {
log.info("正在执行搜索...");
PixivSearchBuilder searchBuilder = new PixivSearchBuilder(Strings.isNullOrEmpty(content) ? "" : content); PixivSearchBuilder searchBuilder = new PixivSearchBuilder(Strings.isNullOrEmpty(content) ? "" : content);
if (type != null) { if (type != null) {
try { try {
@ -402,6 +416,7 @@ public class CQProcess {
if (index == pageIndex) { if (index == pageIndex) {
try { try {
//TODO: 这里可以尝试改成直接提交所有所需的图片给这里然后再获取结果
imageCacheExecutor.executorSync(new ImageCacheObject(imageCache, illustId, link, currentImageFile)); imageCacheExecutor.executorSync(new ImageCacheObject(imageCache, illustId, link, currentImageFile));
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.warn("图片下载遭到中断!", e); log.warn("图片下载遭到中断!", e);
@ -522,7 +537,17 @@ public class CQProcess {
return imageStoreDir; return imageStoreDir;
} }
private static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range) throws IOException { /**
* 获取排行榜
* @param contentType 排行榜类型
* @param mode 排行榜模式
* @param queryDate 查询时间
* @param start 开始排名, 从1开始
* @param range 取范围
* @return 成功返回有值List, 失败且无异常返回空
* @throws IOException 获取异常时抛出
*/
public static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range) throws IOException {
String date = new SimpleDateFormat("yyyyMMdd").format(queryDate); String date = new SimpleDateFormat("yyyyMMdd").format(queryDate);
//int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode(); //int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode();
String requestSign = buildSyncKey("Ranking.", contentType.name(), ".", mode.name(), ".", date); String requestSign = buildSyncKey("Ranking.", contentType.name(), ".", mode.name(), ".", date);

View File

@ -69,7 +69,16 @@ public class Main {
log.info("cqRootDir: {}", argsProp.getValue("cqRootDir")); log.info("cqRootDir: {}", argsProp.getValue("cqRootDir"));
System.setProperty("cgj.cqRootDir", argsProp.getValue("cqRootDir")); System.setProperty("cgj.cqRootDir", argsProp.getValue("cqRootDir"));
} else { } else {
log.info("未设置cqRootDir"); log.info("未设置cqRootDir, 当前运行目录将作为酷Q机器人所在目录.");
System.setProperty("cgj.cqRootDir", "./");
}
if(argsProp.containsKey("redisAddr")) {
log.info("redisAddress: {}", argsProp.getValue("redisAddr"));
System.setProperty("cgj.redisAddress", argsProp.getValue("redisAddr"));
} else {
log.info("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)");
System.setProperty("cgj.redisAddress", "127.0.0.1");
} }
File cookieStoreFile = new File("cookies.store"); File cookieStoreFile = new File("cookies.store");

View File

@ -1,2 +1,2 @@
server.port=8081 server.port=8081
server.tomcat.max-threads=10 server.tomcat.max-threads=1