Skip to content

Commit 6156533

Browse files
committed
refactor: cache geo manifest by query string
1 parent d68e784 commit 6156533

5 files changed

Lines changed: 77 additions & 49 deletions

File tree

src/class/GEOResourceManifest.mjs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class GEOResourceManifest {
1313
Console.log("☑️ Download");
1414
const newRequest = { ...request };
1515
newRequest.url = new URL(newRequest.url);
16-
newRequest.url.searchParams.set("country_code", countryCode);
16+
newRequest.url.searchParams.set("country_code", countryCode === "XX" ? "US" : countryCode);
1717
newRequest.url = newRequest.url.toString();
1818
newRequest["binary-mode"] = true;
1919
const response = await fetch(newRequest);
@@ -26,12 +26,16 @@ export default class GEOResourceManifest {
2626
* 读取资源清单缓存。
2727
* Read resource manifest cache.
2828
* @param {object} caches 缓存对象 / Cache object.
29-
* @param {string} countryCode 国家代码 / Country code.
29+
* @param {string} queryString 查询字符串(包含前导问号) / Query string with leading question mark.
3030
* @returns {{eTag?: string, base64?: string}|undefined} 缓存条目 / Cache entry.
3131
*/
32-
static getCache(caches = {}, countryCode = "CN") {
32+
static getCache(caches = {}, queryString = "") {
3333
Console.log("☑️ Get Cache");
34-
const cache = caches?.GeoManifest?.[countryCode];
34+
if (!queryString) {
35+
Console.warn("❎ Get Cache", "Missing query string");
36+
return undefined;
37+
}
38+
const cache = caches?.GeoManifest?.[queryString];
3539
switch (typeof cache?.base64) {
3640
case "string":
3741
if (cache.base64) {
@@ -48,21 +52,25 @@ export default class GEOResourceManifest {
4852
* 写入资源清单缓存。
4953
* Write resource manifest cache.
5054
* @param {object} caches 缓存对象 / Cache object.
51-
* @param {string} countryCode 国家代码 / Country code.
55+
* @param {string} queryString 查询字符串(包含前导问号) / Query string with leading question mark.
5256
* @param {string} eTag 实体标签 / Entity tag.
5357
* @param {Uint8Array|ArrayBuffer} rawBody 原始二进制 / Raw binary body.
5458
* @param {object} env Worker 环境对象 / Worker environment bindings.
5559
* @returns {Promise<boolean>} 是否写入成功 / Whether cache is written.
5660
*/
57-
static async setCache(caches = {}, countryCode = "CN", eTag = "", rawBody = new Uint8Array(), env) {
61+
static async setCache(caches = {}, queryString = "", eTag = "", rawBody = new Uint8Array(), env) {
5862
Console.log("☑️ Set Cache");
63+
if (!queryString) {
64+
Console.warn("❎ Set Cache", "Missing query string");
65+
return false;
66+
}
5967
if (!eTag) {
60-
Console.warn("❎ Set Cache", `Missing eTag: ${countryCode}`);
68+
Console.warn("❎ Set Cache", `Missing eTag: ${queryString}`);
6169
return false;
6270
}
6371
rawBody = rawBody instanceof Uint8Array ? rawBody : new Uint8Array(rawBody ?? []);
6472
if (!rawBody.length) {
65-
Console.warn("❎ Set Cache", `Empty body: ${countryCode}`);
73+
Console.warn("❎ Set Cache", `Empty body: ${queryString}`);
6674
return false;
6775
}
6876
let base64 = "";
@@ -87,15 +95,15 @@ export default class GEOResourceManifest {
8795
}
8896
} catch (error) {
8997
Console.error(error);
90-
Console.warn("❎ Set Cache", `Encode failed: ${countryCode}`);
98+
Console.warn("❎ Set Cache", `Encode failed: ${queryString}`);
9199
return false;
92100
}
93101
if (!base64) {
94-
Console.warn("❎ Set Cache", `Empty base64: ${countryCode}`);
102+
Console.warn("❎ Set Cache", `Empty base64: ${queryString}`);
95103
return false;
96104
}
97105
if (typeof caches.GeoManifest !== "object" || caches.GeoManifest === null) caches.GeoManifest = {};
98-
caches.GeoManifest[countryCode] = { eTag, base64 };
106+
caches.GeoManifest[queryString] = { eTag, base64 };
99107
const result = env?.PersistentStore
100108
? await new Storage({ env: { namespace: env.PersistentStore } }).setItem("@iRingo.Maps.Caches.GeoManifest", caches.GeoManifest)
101109
: PersistentStorage.setItem("@iRingo.Maps.Caches", caches);
@@ -107,14 +115,14 @@ export default class GEOResourceManifest {
107115
* 解码资源清单缓存。
108116
* Decode resource manifest cache.
109117
* @param {object} caches 缓存对象 / Cache object.
110-
* @param {string} countryCode 国家代码 / Country code.
118+
* @param {string} queryString 查询字符串(包含前导问号) / Query string with leading question mark.
111119
* @returns {object|undefined} 解码结果 / Decoded manifest.
112120
*/
113-
static decodeCache(caches = {}, countryCode = "CN") {
121+
static decodeCache(caches = {}, queryString = "") {
114122
Console.log("☑️ Decode Cache");
115-
const cache = this.getCache(caches, countryCode);
123+
const cache = GEOResourceManifest.getCache(caches, queryString);
116124
if (!cache?.base64) {
117-
Console.warn("❎ Decode Cache", `Missing cache: ${countryCode}`);
125+
Console.warn("❎ Decode Cache", `Missing cache: ${queryString}`);
118126
return undefined;
119127
}
120128
try {
@@ -133,15 +141,15 @@ export default class GEOResourceManifest {
133141
throw new Error("Unsupported base64 decoder");
134142
}
135143
if (!rawBody?.length) {
136-
Console.warn("❎ Decode Cache", `Empty body: ${countryCode}`);
144+
Console.warn("❎ Decode Cache", `Empty body: ${queryString}`);
137145
return undefined;
138146
}
139147
const body = GEOResourceManifestDownload.decode(rawBody);
140148
Console.log("✅ Decode Cache");
141149
return body;
142150
} catch (error) {
143151
Console.error(error);
144-
Console.warn("❎ Decode Cache", `Decode failed: ${countryCode}`);
152+
Console.warn("❎ Decode Cache", `Decode failed: ${queryString}`);
145153
return undefined;
146154
}
147155
}

src/process/Request.dev.mjs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,20 @@ export async function Request($request, env) {
145145
switch (url.searchParams.get("country_code")) {
146146
case "CN":
147147
for (const cacheCountryCode of ["XX"]) {
148-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
149-
const remoteCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
148+
const cacheURL = new URL(url.toString());
149+
const requestCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
150+
cacheURL.searchParams.set("country_code", requestCountryCode);
151+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
150152
let response;
151153
if (cache?.eTag) {
152-
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, remoteCountryCode);
154+
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, requestCountryCode);
153155
} else {
154-
response = await GEOResourceManifest.download(request, remoteCountryCode);
156+
response = await GEOResourceManifest.download(request, requestCountryCode);
155157
}
156158
switch (response?.status) {
157159
case 200:
158160
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
159-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
161+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
160162
break;
161163
case 304:
162164
Console.info(`Cache not modified: ${cacheCountryCode}`);
@@ -169,18 +171,20 @@ export async function Request($request, env) {
169171
break;
170172
case "KR":
171173
for (const cacheCountryCode of ["CN", "XX"]) {
172-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
173-
const remoteCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
174+
const cacheURL = new URL(url.toString());
175+
const requestCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
176+
cacheURL.searchParams.set("country_code", requestCountryCode);
177+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
174178
let response;
175179
if (cache?.eTag) {
176-
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, remoteCountryCode);
180+
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, requestCountryCode);
177181
} else {
178-
response = await GEOResourceManifest.download(request, remoteCountryCode);
182+
response = await GEOResourceManifest.download(request, requestCountryCode);
179183
}
180184
switch (response?.status) {
181185
case 200:
182186
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
183-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
187+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
184188
break;
185189
case 304:
186190
Console.info(`Cache not modified: ${cacheCountryCode}`);
@@ -193,7 +197,9 @@ export async function Request($request, env) {
193197
break;
194198
default:
195199
for (const cacheCountryCode of ["CN"]) {
196-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
200+
const cacheURL = new URL(url.toString());
201+
cacheURL.searchParams.set("country_code", cacheCountryCode);
202+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
197203
let response;
198204
if (cache?.eTag) {
199205
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, cacheCountryCode);
@@ -203,7 +209,7 @@ export async function Request($request, env) {
203209
switch (response?.status) {
204210
case 200:
205211
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
206-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
212+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
207213
break;
208214
case 304:
209215
Console.info(`Cache not modified: ${cacheCountryCode}`);

src/process/Request.mjs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,20 @@ export async function Request($request, env) {
134134
switch (url.searchParams.get("country_code")) {
135135
case "CN":
136136
for (const cacheCountryCode of ["XX"]) {
137-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
138-
const remoteCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
137+
const cacheURL = new URL(url.toString());
138+
const requestCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
139+
cacheURL.searchParams.set("country_code", requestCountryCode);
140+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
139141
let response;
140142
if (cache?.eTag) {
141-
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, remoteCountryCode);
143+
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, requestCountryCode);
142144
} else {
143-
response = await GEOResourceManifest.download(request, remoteCountryCode);
145+
response = await GEOResourceManifest.download(request, requestCountryCode);
144146
}
145147
switch (response?.status) {
146148
case 200:
147149
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
148-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
150+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
149151
break;
150152
case 304:
151153
Console.info(`Cache not modified: ${cacheCountryCode}`);
@@ -158,18 +160,20 @@ export async function Request($request, env) {
158160
break;
159161
case "KR":
160162
for (const cacheCountryCode of ["CN", "XX"]) {
161-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
162-
const remoteCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
163+
const cacheURL = new URL(url.toString());
164+
const requestCountryCode = cacheCountryCode === "XX" ? "US" : cacheCountryCode;
165+
cacheURL.searchParams.set("country_code", requestCountryCode);
166+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
163167
let response;
164168
if (cache?.eTag) {
165-
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, remoteCountryCode);
169+
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, requestCountryCode);
166170
} else {
167-
response = await GEOResourceManifest.download(request, remoteCountryCode);
171+
response = await GEOResourceManifest.download(request, requestCountryCode);
168172
}
169173
switch (response?.status) {
170174
case 200:
171175
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
172-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
176+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
173177
break;
174178
case 304:
175179
Console.info(`Cache not modified: ${cacheCountryCode}`);
@@ -182,7 +186,9 @@ export async function Request($request, env) {
182186
break;
183187
default:
184188
for (const cacheCountryCode of ["CN"]) {
185-
const cache = GEOResourceManifest.getCache(Caches, cacheCountryCode);
189+
const cacheURL = new URL(url.toString());
190+
cacheURL.searchParams.set("country_code", cacheCountryCode);
191+
const cache = GEOResourceManifest.getCache(Caches, cacheURL.search);
186192
let response;
187193
if (cache?.eTag) {
188194
response = await GEOResourceManifest.download({ ...request, headers: { ...request.headers, "If-None-Match": cache.eTag } }, cacheCountryCode);
@@ -192,7 +198,7 @@ export async function Request($request, env) {
192198
switch (response?.status) {
193199
case 200:
194200
if (!response?.eTag || !response?.body?.length) Console.warn(`Skip cache update: ${cacheCountryCode}`);
195-
else await GEOResourceManifest.setCache(Caches, cacheCountryCode, response.eTag, response.body, env);
201+
else await GEOResourceManifest.setCache(Caches, cacheURL.search, response.eTag, response.body, env);
196202
break;
197203
case 304:
198204
Console.info(`Cache not modified: ${cacheCountryCode}`);

0 commit comments

Comments
 (0)