Skip to content

cache: compareAndSwap and saveMany don't refuse '' and '0' like save does #15707

Open

Description

@ArnabChatterjee20k

Found while auditing #15704.

The cache does not apply one rule for which values it stores:

  • Cache::compareAndSwap() (packages/cache/src/Cache.php:240-245) has no data check, unlike save() and saveWithLease(), which refuse '' and '0'.
    • On Memory, compareAndSwap('k', '', $version) calls save(), which refuses it, so it throws Unavailable('Unable to save cache snapshot') (Adapter/Memory.php:33-34). A refused value is reported as an outage.
    • On Redis and Redis\Multiplexing, the CAS script stores '', and load() returns it (Adapter/Redis/Versioned.php:104-118).
  • saveMany() stores '' and '0' field values (Adapter/Redis.php:205-211, Adapter/Redis/Multiplexing.php:278-285, Adapter/RedisCluster.php:146-149), while save() refuses them.

No caller passes '' or '0' through these paths today. utopia-php/database's mutation records are always ['owners' => …].

Fix: return false for '' and '0' in Cache::compareAndSwap(), as saveWithLease() does. Either skip ''/'0' fields in saveMany(), or document that fields are stored as given. Add tests for both.

Activity

  1. ArnabChatterjee20k commented on Nov 17, 2026

    @ArnabChatterjee20k
    MemberAuthor

    Staging run showing where saveMany() storing '0' is visible.

    On staging fra (cloud-fra1-stg), while testing appwrite-labs/cloud#7621 (utopia-php/cache 8.0.0), the list cache cached the total "0" for an empty table through saveMany(), even though save() refuses '0':

    • ListCache::save() writes every field with Cache::saveMany() (src/Appwrite/Utopia/Cache/ListCache.php), so the total of an empty list is stored as "0".
    • The list action skips saving the empty row list (if ($documents !== []) in Databases/Collections/Documents/XList.php), so the rows aren't cached.

    Steps (TablesDB rows list, ttl=600):

    1. List an empty table: total: 0, X-Appwrite-Cache: miss.
    2. Create row e1.
    3. List again: rows: ["e1"] (read fresh), total: 0 (cached), X-Appwrite-Cache: hit.
    PASS list empty ttl=600 rows has e1 (empty never cached)            200  [e1] cache=miss
    NOTE list empty ttl=600 total (cached '0', row writes don't purge)  0 cache=hit
    

    The same happened on the build before #15704 and the build after it, so 8.0.0 didn't cause it. The list behaviour is tracked in #15718. If saveMany() gets the same ''/'0' guard as save(), it would stop caching that "0" total, so #15718's fix has to be decided together with this one.

    Full staging results: https://github.com/appwrite-labs/cloud/pull/7621 (Tests → Staging).

  2. rahulsjha commented on Nov 18, 2026

    @rahulsjha

    Hi @ArnabChatterjee20k, I'd like to work on this. Plan: in Cache::compareAndSwap() (packages/cache/src/Cache.php), return false for '' and '0' before calling the adapter, as saveWithLease() does. Then Memory doesn't report a refused value as Unavailable, and Redis doesn't store it. In saveMany() on Redis, Redis\Multiplexing and RedisCluster, skip '' and '0' fields so it matches save(). Given your staging note on the cached "0" total, skipping looks safer than documenting. Tests: unit tests per adapter for CAS refusing both values, and saveMany() leaving them out while storing the other fields. Could you assign it to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions