修复图片Key缓存冷启动并关闭异常HTTP响应
This commit is contained in:
@@ -194,10 +194,13 @@ public class GalleryUtil {
|
|||||||
|
|
||||||
public static String getMpvKey(String url){
|
public static String getMpvKey(String url){
|
||||||
String gid = String.valueOf(parseGid(url));
|
String gid = String.valueOf(parseGid(url));
|
||||||
return gid2MpvKey.computeIfAbsent(gid, k -> {
|
String key = gid2MpvKey.get(gid);
|
||||||
|
if (key == null) {
|
||||||
|
// refreshMpvKey writes the cache itself; never call it inside computeIfAbsent.
|
||||||
refreshMpvKey(url);
|
refreshMpvKey(url);
|
||||||
return gid2MpvKey.get(k);
|
key = gid2MpvKey.get(gid);
|
||||||
});
|
}
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void refreshMpvKey(String url) {
|
public static void refreshMpvKey(String url) {
|
||||||
@@ -210,7 +213,7 @@ public class GalleryUtil {
|
|||||||
content = requests(mpvUrl, "get", header, null);
|
content = requests(mpvUrl, "get", header, null);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("刷新mpvKey失败, url: {}", url, e);
|
log.error("刷新mpvKey失败, url: {}", url, e);
|
||||||
gid2MpvKey.put(parseGid(url) + "", null);
|
gid2MpvKey.remove(parseGid(url) + "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Document document = Jsoup.parse(content);
|
Document document = Jsoup.parse(content);
|
||||||
@@ -343,23 +346,22 @@ public class GalleryUtil {
|
|||||||
}
|
}
|
||||||
httpResponse = httpClient.execute(httpPost);
|
httpResponse = httpClient.execute(httpPost);
|
||||||
}
|
}
|
||||||
|
try (httpResponse) {
|
||||||
HttpEntity responseEntity = httpResponse.getEntity();
|
HttpEntity responseEntity = httpResponse.getEntity();
|
||||||
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
if (statusCode == 200 && responseEntity != null) {
|
||||||
if(statusCode == 200){
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()))) {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
|
|
||||||
String str;
|
String str;
|
||||||
while ((str = reader.readLine()) != null)
|
while ((str = reader.readLine()) != null)
|
||||||
stringBuilder.append(str).append("\n");
|
stringBuilder.append(str).append("\n");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn("{}:{}", url, statusCode);
|
log.warn("{}:{}", url, statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
httpResponse.close();
|
|
||||||
|
|
||||||
return stringBuilder.toString();
|
return stringBuilder.toString();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Integer parseGid(String link){
|
public static Integer parseGid(String link){
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.lion.lionwebsite.Util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.io.IOException;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
|
||||||
|
class GalleryKeyCacheTest {
|
||||||
|
@Test void coldCacheAllowsRefreshToPopulateIt() {
|
||||||
|
String url = "https://example.org/g/987654321/key/";
|
||||||
|
GalleryUtil.gid2MpvKey.remove("987654321");
|
||||||
|
try (var methods = mockStatic(GalleryUtil.class)) {
|
||||||
|
methods.when(() -> GalleryUtil.parseGid(url)).thenReturn(987654321);
|
||||||
|
methods.when(() -> GalleryUtil.getMpvKey(url)).thenCallRealMethod();
|
||||||
|
methods.when(() -> GalleryUtil.refreshMpvKey(url)).thenAnswer(call -> {
|
||||||
|
GalleryUtil.gid2MpvKey.put("987654321", "cached-key");
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
assertEquals("cached-key", GalleryUtil.getMpvKey(url));
|
||||||
|
assertEquals("cached-key", GalleryUtil.getMpvKey(url));
|
||||||
|
methods.verify(() -> GalleryUtil.refreshMpvKey(url), times(1));
|
||||||
|
} finally { GalleryUtil.gid2MpvKey.remove("987654321"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void failedRefreshRemovesOldKeyWithoutInsertingNull() {
|
||||||
|
String url = "https://example.org/g/987654321/key/";
|
||||||
|
GalleryUtil.gid2MpvKey.put("987654321", "old-key");
|
||||||
|
try (var methods = mockStatic(GalleryUtil.class)) {
|
||||||
|
methods.when(() -> GalleryUtil.parseGid(url)).thenReturn(987654321);
|
||||||
|
methods.when(() -> GalleryUtil.refreshMpvKey(url)).thenCallRealMethod();
|
||||||
|
methods.when(() -> GalleryUtil.requests(anyString(), anyString(), any(), any()))
|
||||||
|
.thenThrow(new IOException("test failure"));
|
||||||
|
assertDoesNotThrow(() -> GalleryUtil.refreshMpvKey(url));
|
||||||
|
assertFalse(GalleryUtil.gid2MpvKey.containsKey("987654321"));
|
||||||
|
} finally { GalleryUtil.gid2MpvKey.remove("987654321"); }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user