Skip to content

Commit e89e288

Browse files
committed
perf: use redis mget for api list
1 parent 6e34b29 commit e89e288

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/index.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ function downloadBatch(array $urls) {
343343
$[if `process.env.API === "true"`]$
344344
if(isset($_REQUEST["ls"])) {
345345
$info = [];
346+
$[end]$
347+
$[if `process.env.API === "true" && process.env.DOWNLOAD_COUNTER === "true"`]$
348+
// Get all counters in one call
349+
$urls = array_map(fn($file) => $file->url, $sorted);
350+
$all_counters = $redis->mget($urls);
351+
$counters_map = array_combine($urls, $all_counters);
352+
$[end]$
353+
$[if `process.env.API === "true"`]$
354+
346355
foreach ($sorted as $file) {
347356
if ($file->name === "..") continue; // skip parent folder
348357
$info[] = [
@@ -351,7 +360,7 @@ function downloadBatch(array $urls) {
351360
"type" => $file->is_dir ? "dir" : "file",
352361
"size" => intval($file->size),
353362
"modified" => $file->modified_date,
354-
"downloads" => ${{`process.env.DOWNLOAD_COUNTER === "true" ? "intval($redis->get($file->url))" : "0"`}}$
363+
"downloads" => ${{`process.env.DOWNLOAD_COUNTER === "true" ? "intval($counters_map[$file->url] ?? 0)" : "0"`}}$
355364
];
356365
}
357366
header("Content-Type: application/json");

0 commit comments

Comments
 (0)