Skip to content

Commit f2b0f30

Browse files
committed
Revise flushcache
1 parent 8f60654 commit f2b0f30

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

app/helper/flushCache.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module.exports = ({
22
reverse_proxies,
3-
requestsPerSecond = 1,
3+
requestsPerSecond = 5,
44
maxHostsPerPurge = 10,
55
}) => {
6-
let queue = [];
6+
let queue = new Set(); // Changed to Set to automatically handle duplicates
77
let isProcessing = false;
88
let lastRequestTime = 0;
99
let currentBatchResolvers = [];
1010

1111
async function add(hosts) {
1212
return new Promise((resolve, reject) => {
13-
queue.push(...hosts);
13+
// Add all hosts to the Set (duplicates will be automatically ignored)
14+
hosts.forEach(host => queue.add(host));
1415
currentBatchResolvers.push({ resolve, reject });
1516
process();
1617
});
@@ -21,7 +22,7 @@ module.exports = ({
2122

2223
isProcessing = true;
2324

24-
while (queue.length > 0) {
25+
while (queue.size > 0) {
2526
const now = Date.now();
2627
const timeSinceLastRequest = now - lastRequestTime;
2728
const minimumGap = 1000 / requestsPerSecond;
@@ -32,12 +33,15 @@ module.exports = ({
3233
);
3334
}
3435

35-
const hostsBatch = queue.splice(0, maxHostsPerPurge);
36+
// Convert part of the Set to Array for processing
37+
const hostsBatch = Array.from(queue).slice(0, maxHostsPerPurge);
38+
// Remove processed hosts from the Set
39+
hostsBatch.forEach(host => queue.delete(host));
3640

3741
try {
3842
await flushHosts(hostsBatch);
3943

40-
if (queue.length === 0) {
44+
if (queue.size === 0) {
4145
// Resolve all promises when the entire queue is processed
4246
currentBatchResolvers.forEach(({ resolve }) => resolve());
4347
currentBatchResolvers = [];
@@ -46,7 +50,7 @@ module.exports = ({
4650
// Reject all promises if there's an error
4751
currentBatchResolvers.forEach(({ reject }) => reject(error));
4852
currentBatchResolvers = [];
49-
queue = [];
53+
queue.clear();
5054
}
5155

5256
lastRequestTime = Date.now();
@@ -59,7 +63,6 @@ module.exports = ({
5963
for (const reverse_proxy_url of reverse_proxies) {
6064
try {
6165
const url = `${reverse_proxy_url}/purge?${hosts
62-
.filter((host, index, self) => self.indexOf(host) === index)
6366
.map((host) => `host=${encodeURIComponent(host)}`)
6467
.join("&")}`;
6568

@@ -98,4 +101,4 @@ module.exports = ({
98101

99102
await add(hosts);
100103
};
101-
};
104+
};

0 commit comments

Comments
 (0)