Skip to content

Clear a key's expiry when set() is called without a TTL - #7

Merged
loevgaard merged 1 commit into
masterfrom
null-ttl-clears-expiry
Nov 23, 2026
Merged

loevgaard merged 1 commit into
masterfrom
null-ttl-clears-expiry

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Summary

A null TTL now means the key never expires. set() replaces the value and the expiry together, as Redis SET and PSR-16 set() do. Array access goes through set(), so it behaves the same.

 set(key, value, ttl)
   reject reserved key / ttl <= 0
   store value (changed if new or different)
   if ttl given
     expiry = now + ttl (changed if it moved)
+  else if key has an expiry
+    remove expiry (changed)

Before this change, a value set on a key whose expiry had passed was gone at once. A value set on a key with a live expiry kept that expiry, even though the call didn't ask for one.

The $ttl docblock and the README's Expiring entries and Tracking changes sections describe the new rule.

Closes #4

Evidence

  • Before (master, using the repro from set() without a TTL keeps the key's old expiry #4):

    set('a', 2) on a key whose expiry has passed   → has('a') === false
    set('b', 1, 60); set('b', 2)                    → toArray() keeps __expires.b = now + 60
    $m['c'] = 2 on a key whose expiry has passed    → isset($m['c']) === false
    set('d', 1) on an expired 'd' that holds 1      → hasChanged() === false
    

    After (this branch, same script):

    has('a') === true
    toArray() === ['b' => 2]
    isset($m['c']) === true
    hasChanged() === true
    
  • Tests: 7 new data-provider cases fail against master's src/ and pass on this branch:

    • it_clears_the_expiry_when_setting_without_a_ttl: set() on a key whose expiry has passed, on a key with a live expiry, and on a key that only had an expiry restored from stored data; plus offsetSet() on a passed and on a live expiry. Each checks the value is still there long after the old expiry.
    • it_changes_when_setting_the_same_value_without_a_ttl_clears_an_expiry: one case with a live expiry, one with an expiry that has passed.

    The old it_does_not_change_when_setting_the_same_value_without_a_ttl test now covers only keys with no expiry. PHPStan, ECS, PHPUnit (74 tests) and Infection (MSI 96%, no escaped mutants in Metadata) all pass.

Merge Danger

Door: two-way

Reverting the commit restores the old behaviour. Data stored in the meantime keeps working, because the serialized shape is unchanged: a key without an entry in __expires was already a valid state.

Blast Radius: consumers

The method signatures don't change, so the Roave BC check passes. Behaviour does change, though, and that check can't see it:

  • Callers who relied on set($key, $value) keeping a key's expiry now get a key that never expires. They need to pass the TTL again.
  • Setting a key to the value it already has, without a TTL, now counts as a change (hasChanged() returns true) if the key had an expiry. A store that saves on hasChanged() will write that row once.

This should be noted in the 1.2.0 release notes, next to change tracking (#5). setono/client-bundle's src/ never sets a key without a TTL to keep its expiry.

A null TTL now means the key never expires: set() replaces the value and
the expiry together, as Redis SET and PSR-16 set() do. Before, set()
without a TTL kept whatever expiry the key had, so a value set on a key
whose expiry had passed was gone at once, and a value set on a key with a
live expiry inherited it. Array access goes through set() and behaves the
same.

Removing an expiry counts as a change, so setting a key to the value it
already has without a TTL makes hasChanged() true when the key had an
expiry.

This changes behaviour for callers that relied on set() without a TTL
keeping an expiry: they now have to pass the TTL again.

Closes #4
@loevgaard
loevgaard merged commit a4337ed into master Nov 23, 2026
27 checks passed
@loevgaard
loevgaard deleted the null-ttl-clears-expiry branch November 23, 2026 10:48
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.

set() without a TTL keeps the key's old expiry

1 participant