Skip to content

Commit ad6a208

Browse files
authored
uc query result support disk cache (#592)
1 parent 90fabe4 commit ad6a208

File tree

12 files changed

+316
-39
lines changed

12 files changed

+316
-39
lines changed

src/main/java/com/qiniu/common/Constants.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.qiniu.common;
22

3+
import java.io.File;
34
import java.nio.charset.Charset;
45

56
/**
@@ -47,7 +48,28 @@ public final class Constants {
4748
*/
4849
public static final int CONNECTION_POOL_MAX_IDLE_MINUTES = 5;
4950

51+
public static final String CACHE_DIR = getCacheDir();
52+
5053
private Constants() {
5154
}
55+
56+
private static String getCacheDir() {
57+
String tmpDir = System.getProperty("java.io.tmpdir");
58+
if (tmpDir == null || tmpDir.isEmpty()) {
59+
return null;
60+
}
61+
62+
String qiniuDir = tmpDir + "com.qiniu.java-sdk";
63+
File dir = new File(qiniuDir);
64+
if (!dir.exists()) {
65+
return dir.mkdirs() ? qiniuDir : null;
66+
}
67+
68+
if (dir.isDirectory()) {
69+
return qiniuDir;
70+
}
71+
72+
return null;
73+
}
5274
}
5375

src/main/java/com/qiniu/http/Response.java

+11
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,18 @@ public static Response create(okhttp3.Response response, String address, double
105105
error = e.getMessage();
106106
}
107107
}
108+
} else if (response.body() != null) {
109+
// 处理其他 body 非预期情况
110+
if (response.code() >= 300) {
111+
try {
112+
body = response.body().bytes();
113+
error = new String(body, Constants.UTF_8);
114+
} catch (Exception e) {
115+
error = e.getMessage();
116+
}
117+
}
108118
}
119+
109120
return new Response(response, code, reqId, response.header("X-Log"), via(response),
110121
address, duration, error, body);
111122
}

src/main/java/com/qiniu/storage/Api.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,12 @@ public StringMap getHeader() throws QiniuException {
612612
header.put(key, this.header.get(key));
613613
}
614614

615-
if (header.keySet().contains("Content-Type")) {
615+
if (body == null || body.contentType == null
616+
|| header.keySet().contains("Content-Type")) {
616617
return header;
617618
}
618619

619-
if (body != null) {
620-
header.put("Content-Type", body.contentType.toString());
621-
}
620+
header.put("Content-Type", body.contentType.toString());
622621

623622
return header;
624623
}

src/main/java/com/qiniu/storage/AutoRegion.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import com.qiniu.common.QiniuException;
44
import com.qiniu.http.Client;
55
import com.qiniu.http.Response;
6-
import com.qiniu.util.StringUtils;
7-
import com.qiniu.util.UrlUtils;
6+
import com.qiniu.util.*;
87

98
import java.util.ArrayList;
109
import java.util.Arrays;
@@ -27,7 +26,9 @@ class AutoRegion extends Region {
2726
/**
2827
* 全局空间信息缓存,此缓存绑定了 token、bucket,全局有效。
2928
*/
30-
private static Map<String, UCRet> globalRegionCache = new ConcurrentHashMap<>();
29+
private static final Cache<UCRet> globalRegionCache = new Cache.Builder<>(UCRet.class)
30+
.setVersion("v1")
31+
.builder();
3132

3233
/**
3334
* 定义HTTP请求管理相关方法
@@ -75,8 +76,8 @@ private AutoRegion() {
7576
* 通过 API 接口查询上传域名
7677
*/
7778
private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuException {
78-
String cacheKey = index.accessKey + index.bucket;
79-
UCRet ret = globalRegionCache.get(cacheKey);
79+
String cacheKey = getCacheId(index);
80+
UCRet ret = globalRegionCache.cacheForKey(cacheKey);
8081
if (ret != null && ret.isValid()) {
8182
return ret;
8283
}
@@ -94,7 +95,7 @@ private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuE
9495
ret = r.jsonToObject(UCRet.class);
9596
if (ret != null) {
9697
ret.setupDeadline();
97-
globalRegionCache.put(cacheKey, ret);
98+
globalRegionCache.cache(cacheKey, ret);
9899
}
99100
return ret;
100101
}
@@ -122,7 +123,7 @@ static Region regionGroup(UCRet ret) {
122123
*/
123124
private Region queryRegionInfo(String accessKey, String bucket) throws QiniuException {
124125
RegionIndex index = new RegionIndex(accessKey, bucket);
125-
String cacheKey = index.accessKey + "::" + index.bucket;
126+
String cacheKey = getCacheId(index);
126127
Region region = regions.get(cacheKey);
127128

128129
Exception ex = null;
@@ -311,6 +312,22 @@ String[] getUcHostArray() throws QiniuException {
311312
return getUcHosts(null).toArray(new String[0]);
312313
}
313314

315+
private String getCacheId(RegionIndex index) {
316+
StringBuilder builder = new StringBuilder()
317+
.append(index.accessKey)
318+
.append("-")
319+
.append(index.bucket);
320+
321+
if (ucServers != null && !ucServers.isEmpty()) {
322+
for (String host : ucServers) {
323+
if (host != null && !host.isEmpty()) {
324+
builder.append(host);
325+
}
326+
}
327+
}
328+
329+
return UrlSafeBase64.encodeToString(builder.toString());
330+
}
314331

315332
public Object clone() {
316333
AutoRegion newRegion = new AutoRegion();

src/main/java/com/qiniu/storage/ConfigHelper.java

+25-14
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,44 @@ public String tryChangeUpHost(String upToken, String lastUsedHost) throws QiniuE
3939

4040
private String upHost(String upToken, String lastUsedHost, boolean changeHost, boolean mustReturnUpHost)
4141
throws QiniuException {
42-
return getScheme()
43-
+ getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
42+
String host = getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
43+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
44+
return host;
4445
}
4546

4647
public String ioHost(String ak, String bucket) throws QiniuException {
4748
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
48-
return getScheme() + config.region.getIovipHost(regionReqInfo);
49+
String host = config.region.getIovipHost(regionReqInfo);
50+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
51+
return host;
4952
}
5053

5154
public String ioSrcHost(String ak, String bucket) throws QiniuException {
5255
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
53-
return config.region.getIoSrcHost(regionReqInfo);
56+
String host = config.region.getIoSrcHost(regionReqInfo);
57+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
58+
return host;
5459
}
5560

5661
public String apiHost(String ak, String bucket) throws QiniuException {
5762
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
58-
return getScheme() + config.region.getApiHost(regionReqInfo);
63+
String host = config.region.getApiHost(regionReqInfo);
64+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
65+
return host;
5966
}
6067

6168
public String rsHost(String ak, String bucket) throws QiniuException {
6269
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
63-
return getScheme() + config.region.getRsHost(regionReqInfo);
70+
String host = config.region.getRsHost(regionReqInfo);
71+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
72+
return host;
6473
}
6574

6675
public String rsfHost(String ak, String bucket) throws QiniuException {
6776
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
68-
return getScheme() + config.region.getRsfHost(regionReqInfo);
77+
String host = config.region.getRsfHost(regionReqInfo);
78+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
79+
return host;
6980
}
7081

7182
public String rsHost() {
@@ -78,7 +89,8 @@ public String rsHost() {
7889
if (host == null || host.length() == 0) {
7990
host = Configuration.defaultRsHost;
8091
}
81-
return getScheme() + host;
92+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
93+
return host;
8294
}
8395

8496
public String apiHost() {
@@ -91,7 +103,8 @@ public String apiHost() {
91103
if (host == null || host.length() == 0) {
92104
host = Configuration.defaultApiHost;
93105
}
94-
return getScheme() + host;
106+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
107+
return host;
95108
}
96109

97110
public String ucHost() {
@@ -104,7 +117,9 @@ public String ucHost() {
104117
if (host == null || host.length() == 0) {
105118
host = Configuration.defaultUcHost;
106119
}
107-
return getScheme() + host;
120+
121+
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
122+
return host;
108123
}
109124

110125
List<String> ucHostsWithoutScheme() {
@@ -151,10 +166,6 @@ List<String> upHostsWithoutScheme() {
151166
return new ArrayList<>(hosts);
152167
}
153168

154-
private String getScheme() {
155-
return config.useHttpsDomains ? "https://" : "http://";
156-
}
157-
158169
private void makeSureRegion() {
159170
if (config.region == null) {
160171
if (config.zone != null) {

src/main/java/com/qiniu/storage/DownloadUrl.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@ protected void didBuildUrl() throws QiniuException {
204204
}
205205

206206
private String getUrlPrefix() throws QiniuException {
207-
if (useHttps) {
208-
return "https://" + domain;
209-
} else {
210-
return "http://" + domain;
211-
}
207+
return UrlUtils.setHostScheme(domain, useHttps);
212208
}
213209

214210
/**

src/main/java/com/qiniu/storage/UpHostHelper.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
4747
regionHostsLRU.put(regionKey, regionHost);
4848
}
4949

50-
String host = regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
51-
return host;
50+
return regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
5251
}
5352

5453
private String failedUpHost(String regionKey) {

0 commit comments

Comments
 (0)