perf(storage): stream cached preview responses

This commit is contained in:
Torsten Dittmann
2026-05-08 15:55:20 +04:00
parent 9100e06bbe
commit caa73a6ad4
2 changed files with 21 additions and 9 deletions
+15 -3
View File
@@ -636,8 +636,9 @@ Http::init()
$key = $request->cacheIdentifier();
Span::add('storage.cache.key', $key);
$cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key));
$cachePath = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId();
$cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
new Filesystem($cachePath, true)
);
$timestamp = 60 * 60 * 24 * 180; // Temporarily increase the TTL to 180 day to ensure files in the cache are still fetched.
$data = $cache->load($key, $timestamp);
@@ -703,7 +704,7 @@ Http::init()
'accessedAt' => DateTime::now(),
])));
// Refresh the filesystem file's mtime so TTL-based expiry in cache->load() stays valid
$cache->save($key, $data);
\touch($cachePath . DIRECTORY_SEPARATOR . $key);
}
$response
@@ -713,7 +714,18 @@ Http::init()
$storageCacheOperationsCounter->add(1, ['result' => 'hit']);
if (! $isImageTransformation || ! $isDisabled) {
Span::add('storage.cache.hit', true);
$response->send($data);
try {
while (! \feof($data)) {
$chunk = \fread($data, MAX_OUTPUT_CHUNK_SIZE);
if ($chunk === false) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to read cached response stream');
}
$response->chunk($chunk, \feof($data));
}
} finally {
\fclose($data);
}
}
} else {
$storageCacheOperationsCounter->add(1, ['result' => 'miss']);
Generated
+6 -6
View File
@@ -3606,16 +3606,16 @@
},
{
"name": "utopia-php/cache",
"version": "1.0.1",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/cache.git",
"reference": "05ceba981436a4022553f7aaa2a05fa049d0f71c"
"reference": "d36f9050c39c02e09a7763389c9e71258e74af1f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/05ceba981436a4022553f7aaa2a05fa049d0f71c",
"reference": "05ceba981436a4022553f7aaa2a05fa049d0f71c",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/d36f9050c39c02e09a7763389c9e71258e74af1f",
"reference": "d36f9050c39c02e09a7763389c9e71258e74af1f",
"shasum": ""
},
"require": {
@@ -3652,9 +3652,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/cache/issues",
"source": "https://github.com/utopia-php/cache/tree/1.0.1"
"source": "https://github.com/utopia-php/cache/tree/1.0.2"
},
"time": "2026-03-12T03:39:09+00:00"
"time": "2026-05-08T11:40:20+00:00"
},
{
"name": "utopia-php/cli",