Skip to content

Add cached find support#894

Open
premtsd-code wants to merge 45 commits into
mainfrom
feat/cached-find
Open

Add cached find support#894
premtsd-code wants to merge 45 commits into
mainfrom
feat/cached-find

Conversation

@premtsd-code

@premtsd-code premtsd-code commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • extend Database::withCache() with optional generic encode and decode callbacks
  • keep withCache() value-agnostic by default: existing callers continue to cache and return values as-is
  • refresh rejected cache payloads when decode() returns false, so callers can recover from invalid or stale cached values without duplicating purge/refetch logic
  • remove the public Database::cachedFind() wrapper in favor of explicit caller-owned withCache() composition
  • add QueryCache helpers for collection/query-scoped cached values:
    • getQueryCacheKey()
    • getQueryCacheField()
    • restoreQueryCacheDocuments()
    • purgeQueryCache()
  • keep query-cache key/field primitives in Database so collection schema, query state, filters, relationships, auth context, permission mode, and field type remain hashed consistently
  • keep Query::orderRandom() cache bypass as caller policy: callers should skip withCache() for random ordering

Usage

Callers cache query-derived values by composing the Database-owned key helpers, generic cache transform hooks, and optional document restore helper:

$collection = $db->getCollection('wafRules');
$key = $db->getQueryCacheKey($collection->getId(), '_'.$projectSequence);
$field = $db->getQueryCacheField(
    collection: $collection,
    queries: $queries,
    field: 'documents',
    forPermission: Database::PERMISSION_READ,
);

$rules = $db->getAuthorization()->skip(fn () => $db->withCache(
    key: $key,
    hash: $field,
    callback: fn () => $db->find(
        collection: $collection->getId(),
        queries: $queries,
        forPermission: Database::PERMISSION_READ,
    ),
    encode: static fn (array $documents): array => array_map(
        static fn (Document $document): array => $document->getArrayCopy(),
        $documents,
    ),
    decode: fn (mixed $payload): array|false => $db->restoreQueryCacheDocuments(
        collection: $collection,
        payload: $payload,
        forPermission: Database::PERMISSION_READ,
    ),
));

Invalidate cached query fields for the same collection namespace after writes with:

$db->purgeQueryCache('wafRules', '_'.$projectSequence);

For scalar values and arrays made only of scalar/list/map data, callers can omit encode and decode and use withCache() as before.

Cache Shape

Query-cache entries use:

{cacheName}-cache:{hostname}:{namespace}:{tenant}:collection:{collection}:query

The hash field is:

{schemaHash}:{queryStateHash}:{field}

The field can distinguish cached value types such as documents, total, count, or sum under the same query-state hash.

Tests

  • composer format
  • composer lint
  • vendor/bin/phpunit tests/unit/QueryCacheTest.php tests/unit/CacheKeyTest.php

composer check is currently blocked locally by the existing Connection.php PHPStan issue where Swoole\Database\DetectsLostConnections is not discovered by the local analyzer setup.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a generic withCache() cache-aside method to Database, along with public getFindCacheKey() and getFindCacheField() helpers backed by deterministic internal helpers (serializeFindCacheQuery, getFindCacheSchemaHash, getActiveFilterSignatures). Implements public cachedFind() and purgeCachedFind() APIs that wrap find queries in cache-aside logic, skipping cache for random-order queries and re-validating permissions on cache hits. Updates getCacheKeys() to use the centralized filter-signature helper. Tests cover both the cache-aside behavior, find cache-key stability, end-to-end cachedFind lifecycle, and include custom in-memory cache adapters with TTL support.

Changes

Cache-aside helper and find cache-key infrastructure

Layer / File(s) Summary
withCache() cache-aside implementation
src/Database/Database.php
Adds withCache(key, callback, hash) implementing cache-aside: loads by key/hash, treats false/missing as a miss, purges rejected entries, invokes the callback on miss, and best-effort saves results that are not false, with warning-level error handling.
Find cache-key/field helpers and filter-signature centralization
src/Database/Database.php
Updates getCacheKeys() to source the filters signature from new getActiveFilterSignatures(). Adds public getFindCacheKey() and getFindCacheField(), and internal serializeFindCacheQuery(), normalizeFindCacheQueryValue(), getFindCacheSchemaHash(), and getActiveFilterSignatures() for deterministic, stable find cache keys.
cachedFind() and purgeCachedFind() public APIs
src/Database/Database.php
Adds cachedFind(collection, queries, namespace, forPermission) wrapping find queries in cache-aside logic, bypassing cache for random-order queries, converting cached payloads back to document instances with permission revalidation and casting. Adds purgeCachedFind(collection, namespace) to purge all cached find entries.
CacheKeyTest: getFindCacheKey and getFindCacheField coverage
tests/unit/CacheKeyTest.php
Adds Document/Query imports and extends createDatabase() helper; adds eight tests asserting correct key format with :find suffix, namespace override, schema-based field derivation, sensitivity to roles/queries/cursors/ambient state/authorization context, and QueryException on invalid query types.
ListCacheTest: withCache() behaviors, cachedFind() lifecycle, and cache adapters
tests/unit/ListCacheTest.php
Adds withCache() tests asserting callback invocation on miss, caching of empty/null values, payload separation by hash, and non-caching of false. Adds comprehensive cachedFind() tests verifying cache persistence/purge, role/permission separation, document recasting, double-decode prevention, random-query bypass, permission revalidation, and nested document rehydration. Implements HashMemoryCache and JsonHashMemoryCache in-memory cache adapters with TTL-aware operations.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Database
  participant Cache as Cache Adapter
  participant Callback

  Client->>Database: cachedFind(collection, queries)
  Database->>Database: getFindCacheKey()
  Database->>Database: getFindCacheField()
  Database->>Cache: load(key, hash)
  alt Cache hit and value exists
    Cache-->>Database: cached document arrays
  else Cache miss or false
    Cache-->>Database: null
    Database->>Callback: invoke callback via withCache()
    Callback->>Database: find() results
  end
  Database->>Database: recreate documents + authorize
  Database-->>Client: documents
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • utopia-php/database#717: The main PR's cachedFind() explicitly bypasses caching for random-order queries, which depends on this PR's introduction of the TYPE_ORDER_RANDOM query type and SQL handling.
  • utopia-php/database#828: The main PR's deterministic find cache-field/key logic and active filter signature handling depends on per-filter signature metadata via getInstanceFilters(), which this PR enables.

Suggested reviewers

  • abnegate

Poem

🐇 Hop, hop through the cache we go,
A key, a hash, a miss—oh no!
The callback runs, the result is stored,
False values tossed, the rest adored.
With stable keys and schema hash,
My finds are cached in quite a flash! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.79% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add cached find support' directly and accurately describes the main change: introducing new caching functionality for database find operations through cachedFind() and purgeCachedFind() methods.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/cached-find

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces a generic withCache() cache-aside helper with optional encode/decode callbacks, alongside getQueryCacheKey(), getQueryCacheField(), restoreQueryCacheDocuments(), and purgeQueryCache() as building blocks for callers who want to cache find() results. The old inline filter-signature loop in getCacheKeys() is also extracted into a private getActiveFilterSignatures() helper.

  • withCache() wraps cached values in a ['value' => …] envelope so empty arrays, null, and zero can be stored and distinguished from a cache miss; a false return from the decode callback or the main callback signals "not cacheable" and triggers a purge-then-refetch.
  • getQueryCacheField() hashes authorization state, queries, relationships, active filter signatures, permission mode, schema, and database name into a stable field key; restoreQueryCacheDocuments() rebuilds Document objects from the stored payload and applies casting to correct type drift introduced by cache serialization.
  • Test coverage (QueryCacheTest) is thorough and well-structured with self-contained in-memory cache fakes.

Confidence Score: 3/5

The read path is correct, but purgeQueryCache() drops all hash fields under the shared key, undoing the per-database read isolation that getQueryCacheField() establishes.

purgeQueryCache() calls cache->purge($key) without a hash argument, which drops every field under the key regardless of which database wrote it. getQueryCacheField() deliberately includes the database name in the field hash for per-database isolation on reads, but that isolation is broken on every purge. Any caller running purgeQueryCache in one database context silently invalidates cached results for all other databases sharing the same namespace/tenant/hostname/collection.

src/Database/Database.php — specifically purgeQueryCache() and getQueryCacheKey()

Important Files Changed

Filename Overview
src/Database/Database.php Adds withCache(), restoreQueryCacheDocuments(), purgeQueryCache(), getQueryCacheKey(), getQueryCacheField(), and supporting private helpers. The read-isolation design (database name in field hash) is correct, but purgeQueryCache() purges the entire cache key without the database scope, potentially evicting cached results from other Database instances sharing the same namespace/tenant/collection.
tests/unit/CacheKeyTest.php Adds tests for getQueryCacheKey() and getQueryCacheField() covering key shape, namespace override, schema hash, auth context changes, cursor document payload inclusion, ambient state, and query validation. The expected schema hash in the test uses ?: '' guards that the production code currently lacks.
tests/unit/QueryCacheTest.php New integration test file with in-memory adapters covering withCache() cache miss/hit, empty/null/false semantics, encode/decode lifecycle, double-decode prevention, JSON serialization type casting, random-order bypass, document-security documented limitation, and nested document rehydration.

Reviews (38): Last reviewed commit: "Rename list cache helpers to query cache" | Re-trigger Greptile

Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Database/Database.php`:
- Around line 8625-8642: The cache hit path in the conditional block starting
with if ($cached !== null && $cached !== false && \is_array($cached)) only wraps
cached arrays into Document instances using createDocumentInstance, but does not
hydrate relationship fields that may have been serialized as arrays. This
results in different document shapes being returned compared to the cache miss
path which uses find() that returns fully processed documents. Either ensure the
cached hit path recursively hydrates relationship fields to match the shape
returned by find(), or add logic to bypass caching for finds that request
relationship-resolved data to avoid returning inconsistent types between cache
hits and misses.
- Around line 9563-9564: The cache key generation logic in the findCached method
is incorrectly dropping the serialized queries when a $key is provided. Change
the ternary expression that sets the queries field to always call
serializeFindCacheQueries($queries) instead of returning null when $key is set.
The caller key should add an additional cache dimension alongside the query
fingerprint, not replace it, so both the serialized queries and the key must be
included in the cache identifier to prevent different queries with the same key
from returning incorrect cached results.
- Around line 9572-9584: The getFindCacheSchemaHash method currently only
includes collection attributes and indexes in the hash calculation, but the
find() authorization check also validates collection permissions and
documentSecurity settings. When collection authorization policies change, cached
results remain valid incorrectly because the schema hash doesn't reflect these
changes. Modify the getFindCacheSchemaHash method to include the collection's
permissions and documentSecurity flag in the hash payload alongside the existing
attributes and indexes. Additionally, replace the MD5 hash algorithm with a
stronger digest function such as SHA256 to address the static analysis flag
regarding weak hashing.

In `@tests/unit/FindCacheTest.php`:
- Around line 140-144: The `HashMemoryCache::save` method incorrectly prevents
caching of empty arrays because the condition `empty($data)` returns true for
empty arrays and rejects them. Modify the validation logic to only reject empty
strings while allowing empty arrays to be cached as valid results. Change the
condition to specifically check if `$data` is an empty string or if `$key` is
empty, rather than using `empty()` which treats empty arrays the same as empty
strings.
- Around line 83-99: The test testFindCachedTriggersFindEventOnCacheHit does not
actually verify a cache hit because both cache hits and cache misses emit
EVENT_DOCUMENT_FIND. To fix this, add data mutation between the two findCached
calls to force a true cache hit scenario. After the first findCached call on
line 92, modify the seeded project data in the database, then verify that the
second findCached call on line 93 returns the stale cached result from the first
call rather than the modified data, which would definitively prove the second
call was a cache hit.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d0eed82b-9eca-489f-a0fd-19748bc2694c

📥 Commits

Reviewing files that changed from the base of the PR and between cfba533 and 1d615f5.

📒 Files selected for processing (4)
  • src/Database/Database.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/FindCacheTest.php

Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread tests/unit/FindCacheTest.php Outdated
Comment thread tests/unit/FindCacheTest.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/Database/Database.php (2)

8611-8617: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Validate queries before serializing the cache key.

findCached() reaches serializeQueriesForFindCache() before find() runs checkQueryTypes(), so malformed query arrays can fail outside the normal QueryException path on cached calls.

Proposed fix
         if ($collectionDocument->isEmpty()) {
             throw new NotFoundException('Collection not found');
         }
 
+        $this->checkQueryTypes($queries);
+
         [$findKey, $findField] = $this->getFindCacheKeys($collectionDocument->getId(), $queries, $key, $forPermission, $collectionDocument);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Database/Database.php` around lines 8611 - 8617, In the findCached()
method, validate the queries before they are serialized for the cache key. Call
checkQueryTypes() on the queries parameter before invoking getFindCacheKeys(),
which calls serializeQueriesForFindCache(). This ensures malformed query arrays
are caught early with proper QueryException handling rather than failing during
serialization.

8617-8639: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Avoid serving document-security caches after permission revocation.

The cache hit path returns serialized documents without re-reading current document permissions. If a cached document later loses $permissions, the same role can still receive it until the find TTL expires. Until permission-changing writes purge affected find variants, bypass caching for documentSecurity collections.

Conservative fix
         if ($collectionDocument->isEmpty()) {
             throw new NotFoundException('Collection not found');
         }
 
+        if ($collectionDocument->getAttribute('documentSecurity', false)) {
+            return $this->find($collectionDocument->getId(), $queries, $forPermission);
+        }
+
         [$findKey, $findField] = $this->getFindCacheKeys($collectionDocument->getId(), $queries, $key, $forPermission, $collectionDocument);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Database/Database.php` around lines 8617 - 8639, The current caching
logic in the find method returns cached documents without re-validating
permissions, which creates a security vulnerability for documentSecurity
collections where permissions may have been revoked. Add a check using the
$collectionDocument to determine if the collection is a documentSecurity
collection, and if so, skip the entire cache loading and cache return logic (the
try-catch block for $this->cache->load() and the conditional check for $cached
!== null) to force a fresh permission check on every query. This ensures that
permission changes are respected immediately rather than serving stale cached
results until the TTL expires.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/Database/Database.php`:
- Around line 8611-8617: In the findCached() method, validate the queries before
they are serialized for the cache key. Call checkQueryTypes() on the queries
parameter before invoking getFindCacheKeys(), which calls
serializeQueriesForFindCache(). This ensures malformed query arrays are caught
early with proper QueryException handling rather than failing during
serialization.
- Around line 8617-8639: The current caching logic in the find method returns
cached documents without re-validating permissions, which creates a security
vulnerability for documentSecurity collections where permissions may have been
revoked. Add a check using the $collectionDocument to determine if the
collection is a documentSecurity collection, and if so, skip the entire cache
loading and cache return logic (the try-catch block for $this->cache->load() and
the conditional check for $cached !== null) to force a fresh permission check on
every query. This ensures that permission changes are respected immediately
rather than serving stale cached results until the TTL expires.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 29c2ae6a-250e-4626-8ae6-59effc86c769

📥 Commits

Reviewing files that changed from the base of the PR and between 6fad393 and 5275b33.

📒 Files selected for processing (5)
  • src/Database/Database.php
  • tests/e2e/Adapter/Base.php
  • tests/e2e/Adapter/RedisTest.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • tests/unit/FindCacheTest.php
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/e2e/Adapter/Base.php
  • tests/e2e/Adapter/RedisTest.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Database/Database.php`:
- Line 8659: The condition on line 8659 checks `if ($touchOnHit &&
!$hasExpiredDocuments)`, but the `!$hasExpiredDocuments` part is redundant
because the code already guarantees this value through an early return statement
in the preceding lines (8646-8657). Remove the `&& !$hasExpiredDocuments` clause
from the condition, leaving only `if ($touchOnHit)`, to eliminate the
always-true condition that is causing the PHPStan error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 647e586e-bf40-4233-b042-568a6b0b5436

📥 Commits

Reviewing files that changed from the base of the PR and between 5275b33 and 7cd9d19.

📒 Files selected for processing (4)
  • src/Database/Database.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/FindCacheTest.php
💤 Files with no reviewable changes (1)
  • tests/unit/CacheKeyTest.php
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • tests/unit/FindCacheTest.php

Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated
Comment thread src/Database/Database.php Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
src/Database/Database.php (1)

9567-9572: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Strengthen find-cache fingerprint to prevent stale authorization reuse.

getFindCacheField()/getFindCacheSchemaHash() still use MD5 and the schema hash excludes collection policy fields. Since cache hits bypass find() checks, a collection policy change ($permissions / documentSecurity) can keep an old cache field valid for the same role hash until TTL expiry.

Suggested patch
-        return \sprintf(
-            '%s:%s:%s:%s',
-            $this->getFindCacheSchemaHash($collection),
-            \md5(\json_encode($roles) ?: ''),
-            \md5(\json_encode($queryPayload) ?: ''),
-            $field,
-        );
+        return \sprintf(
+            '%s:%s:%s:%s',
+            $this->getFindCacheSchemaHash($collection),
+            \hash('sha256', \json_encode($roles) ?: ''),
+            \hash('sha256', \json_encode($queryPayload) ?: ''),
+            $field,
+        );
@@
-        return \md5(
-            \json_encode($collection->getAttribute('attributes', []))
-            . \json_encode($collection->getAttribute('indexes', []))
-        );
+        return \hash('sha256', \json_encode([
+            'attributes' => $collection->getAttribute('attributes', []),
+            'indexes' => $collection->getAttribute('indexes', []),
+            'permissions' => $collection->getAttribute('$permissions', []),
+            'documentSecurity' => $collection->getAttribute('documentSecurity', false),
+        ]) ?: '');

Also applies to: 9621-9630

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Database/Database.php` around lines 9567 - 9572, The find-cache
fingerprint in getFindCacheField() currently uses weak MD5 hashing and the
schema hash from getFindCacheSchemaHash() excludes collection policy fields
(permissions and documentSecurity). Replace the MD5 hash algorithm with a
stronger alternative like SHA-256 for both the roles and queryPayload hashes in
the cache key. Additionally, update the schema hash calculation to include
collection policy fields so that changes to permissions or documentSecurity
invalidate the cache and prevent stale authorization reuse. Apply these changes
consistently across both getFindCacheField() and getFindCacheSchemaHash()
methods.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@src/Database/Database.php`:
- Around line 9567-9572: The find-cache fingerprint in getFindCacheField()
currently uses weak MD5 hashing and the schema hash from
getFindCacheSchemaHash() excludes collection policy fields (permissions and
documentSecurity). Replace the MD5 hash algorithm with a stronger alternative
like SHA-256 for both the roles and queryPayload hashes in the cache key.
Additionally, update the schema hash calculation to include collection policy
fields so that changes to permissions or documentSecurity invalidate the cache
and prevent stale authorization reuse. Apply these changes consistently across
both getFindCacheField() and getFindCacheSchemaHash() methods.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1171458a-1aba-44ff-b664-ab2433babf95

📥 Commits

Reviewing files that changed from the base of the PR and between 2eb8271 and 7478a8b.

📒 Files selected for processing (3)
  • src/Database/Database.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/ListCacheTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit/CacheKeyTest.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unit/ListCacheTest.php (1)

157-157: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Make hash-field assertion order-insensitive to avoid flaky tests.

$cache->list('key') ordering may vary by adapter implementation; asserting exact sequence can create nondeterministic failures. Prefer canonicalizing comparison or sorting before assert.

Suggested test adjustment
-        $this->assertSame(['first-field', 'second-field'], $cache->list('key'));
+        $this->assertEqualsCanonicalizing(['first-field', 'second-field'], $cache->list('key'));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/ListCacheTest.php` at line 157, The assertSame assertion on the
$cache->list('key') call is comparing arrays in strict order, which can cause
flaky tests since different cache adapter implementations may return hash fields
in varying orders. Modify the assertion to be order-insensitive by either
sorting both the expected and actual arrays before comparison using sort(), or
use an assertion method like assertEqualsCanonicalizing that performs
order-insensitive comparison. This ensures the test validates the correct fields
are returned regardless of the order in which they appear.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Database/Database.php`:
- Around line 8588-8603: The cache key hash calculation in the withCache call is
not including the $forPermission parameter, which means different permission
levels (read, update, delete) will share the same cache entry. Update the
getFindCacheField method call to include $forPermission as an additional
parameter in the hash calculation so that the full authorization context is
properly fingerprinted. This ensures that a cached result for read permissions
cannot be incorrectly reused for update or delete permission requests with the
same query and roles.

In `@tests/unit/ListCacheTest.php`:
- Around line 219-221: The test for cachedFind method (lines 219-221 and similar
occurrences at 228-230 and 234-239) assumes deterministic ordering of results
without explicitly specifying a sort order, which can cause instability across
different database adapters. Either add an explicit order clause to the queries
parameter passed to cachedFind (for example, sorting by ID) to ensure consistent
ordering, or refactor the assertions to compare results canonically without
relying on index position (such as extracting and comparing IDs in a set-based
manner rather than checking first[0]).

---

Outside diff comments:
In `@tests/unit/ListCacheTest.php`:
- Line 157: The assertSame assertion on the $cache->list('key') call is
comparing arrays in strict order, which can cause flaky tests since different
cache adapter implementations may return hash fields in varying orders. Modify
the assertion to be order-insensitive by either sorting both the expected and
actual arrays before comparison using sort(), or use an assertion method like
assertEqualsCanonicalizing that performs order-insensitive comparison. This
ensures the test validates the correct fields are returned regardless of the
order in which they appear.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 45e5f395-111c-4d05-82c3-18f6f0cc29eb

📥 Commits

Reviewing files that changed from the base of the PR and between 7478a8b and a761a16.

📒 Files selected for processing (2)
  • src/Database/Database.php
  • tests/unit/ListCacheTest.php

Comment thread src/Database/Database.php Outdated
Comment thread tests/unit/ListCacheTest.php Outdated
Comment thread src/Database/Database.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unit/ListCacheTest.php (1)

515-559: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

testCachedFindFiltersTtlExpiredDocumentsOnCacheHit no longer validates the cache-hit path.

At Line 550, the document is already expired before the first cachedFind(), so it is filtered out on miss and cached as []. The second call then just reads [], which doesn’t verify hit-time TTL filtering logic. Seed the cache with an expired payload (or otherwise force expiry between miss and hit) to exercise the intended path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/ListCacheTest.php` around lines 515 - 559, The test
`testCachedFindFiltersTtlExpiredDocumentsOnCacheHit` is not properly validating
the cache-hit filtering path because the document expires before the first
`cachedFind()` call, resulting in an empty cached result that the second call
just retrieves. To test the intended behavior of TTL filtering on cache hits,
modify the test so that the document is not expired when the first
`cachedFind()` call is made (so it gets cached with actual data), then force the
document to expire before the second `cachedFind()` call (either by advancing
time, creating a new document with a later expiration, or directly seeding the
cache with an expired payload). This ensures the second call exercises the logic
that filters expired documents from a cached result.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@tests/unit/ListCacheTest.php`:
- Around line 515-559: The test
`testCachedFindFiltersTtlExpiredDocumentsOnCacheHit` is not properly validating
the cache-hit filtering path because the document expires before the first
`cachedFind()` call, resulting in an empty cached result that the second call
just retrieves. To test the intended behavior of TTL filtering on cache hits,
modify the test so that the document is not expired when the first
`cachedFind()` call is made (so it gets cached with actual data), then force the
document to expire before the second `cachedFind()` call (either by advancing
time, creating a new document with a later expiration, or directly seeding the
cache with an expired payload). This ensures the second call exercises the logic
that filters expired documents from a cached result.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 57a03272-7eb5-437b-afa1-ef6c4c796370

📥 Commits

Reviewing files that changed from the base of the PR and between 6f70767 and 9ac837b.

📒 Files selected for processing (3)
  • src/Database/Database.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/ListCacheTest.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Database/Database.php`:
- Around line 8608-8615: The cachedFind method caches document payloads without
filtering TTL-expired documents, causing expired documents to remain visible
until the cache entry expires. Filter out TTL-expired documents in two places
within cachedFind: first, in the callback function shown (after the find call
returns documents but before they are array-mapped and stored in cache), and
second, in the cache hit scenario (around lines 8635-8647, before returning the
cached payload). Use the isTtlExpired method to identify and exclude expired
documents from both the cache miss result being saved and the cache hit payload
being returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cc6fd113-7a02-40de-8fe9-c1c00576e805

📥 Commits

Reviewing files that changed from the base of the PR and between 9ac837b and ad49dba.

📒 Files selected for processing (3)
  • src/Database/Database.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/ListCacheTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit/CacheKeyTest.php

Comment thread src/Database/Database.php Outdated
@utopia-php utopia-php deleted a comment from coderabbitai Bot Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant