Repository: appwrite/appwrite
File: src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php (PHP)

Code before:
                        }, $cachedDocuments);
                        $documentsCacheHit = true;
                    } else {
                        $documents = $dbForProject->find($collectionTableId, $queries);

                        // Convert Document objects to arrays for caching
                        $documentsArray = \array_map(function ($doc) {
                            return $doc->getArrayCopy();
                        }, $documents);
                        $dbForProject->getCache()->save($documentsCacheKey, $documentsArray);
                    }

                    if ($includeTotal) {
                        $cachedTotal = $dbForProject->getCache()->load($totalCacheKey, $ttl);
                        if ($cachedTotal !== null && $cachedTotal !== false) {
                            $total = $cachedTotal;
                            $totalDocumentsCacheHit = true;
                        } else {
                            $total = $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT);
                            $dbForProject->getCache()->save($totalCacheKey, $total);
                        }
                    } else {
                        $total = 0;
                    }

                    $response->addHeader('X-Appwrite-Cache', $documentsCacheHit ? 'hit' : 'miss')

                } else {
                    // has selects, allow relationship on documents
                    $documents = $dbForProject->find($collectionTableId, $queries);
                    $total = $includeTotal ? $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT) : 0;
                }

            } else {
                // has no selects, disable relationship loading on documents
                /* @type Document[] $documents */
                $documents = $dbForProject->skipRelationships(fn () => $dbForProject->find($collectionTableId, $queries));
                $total = $includeTotal ? $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT) : 0;
            }
        } catch (OrderException $e) {
            $documents = $this->isCollectionsAPI() ? 'documents' : 'rows';
            $attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
            $message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
            throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
        } catch (QueryException $e) {
            throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
        }

        $operations = 0;
        $collectionsCache = [];
        foreach ($documents as $document) {

Review comment (performance):
```suggestion
                    $response->addHeader('X-Appwrite-Cache', $documentsCacheHit ? 'hit' : 'miss');
```

Code after:

                        // Convert Document objects to arrays for caching
                        $documentsArray = \array_map(function ($doc) {
                            return $doc->getArrayCopy();
                        }, $documents);
                        $dbForProject->getCache()->save($documentsCacheKey, $documentsArray);
                    }

                    if ($includeTotal) {
                        $cachedTotal = $dbForProject->getCache()->load($totalCacheKey, $ttl);
                        if ($cachedTotal !== null && $cachedTotal !== false) {
                            $total = $cachedTotal;
                            $totalDocumentsCacheHit = true;
                        } else {
                            $total = $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT);
                            $dbForProject->getCache()->save($totalCacheKey, $total);
                        }
                    } else {
                        $total = 0;
                    }

                    $response->addHeader('X-Appwrite-Cache', $documentsCacheHit ? 'hit' : 'miss');

                } else {
                    // has selects, allow relationship on documents
                    $documents = $dbForProject->find($collectionTableId, $queries);
                    $total = $includeTotal ? $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT) : 0;
                }

            } else {
                // has no selects, disable relationship loading on documents
                /* @type Document[] $documents */
                $documents = $dbForProject->skipRelationships(fn () => $dbForProject->find($collectionTableId, $queries));
                $total = $includeTotal ? $dbForProject->count($collectionTableId, $queries, APP_LIMIT_COUNT) : 0;
            }
        } catch (OrderException $e) {
            $documents = $this->isCollectionsAPI() ? 'documents' : 'rows';
            $attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
            $message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
            throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
        } catch (QueryException $e) {
            throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
        }

        $operations = 0;
        $collectionsCache = [];
        foreach ($documents as $document) {
            $this->processDocument(
                database: $database,
                collection: $collection,
                document: $document,