Skip to content

Cache API: Respect the expiration time in the default object cache. - #16447

Open
divyesh-addweb wants to merge 2 commits into
WordPress:trunkfrom
divyesh-addweb:fix/55847-object-cache-expire
Open

divyesh-addweb wants to merge 2 commits into
WordPress:trunkfrom
divyesh-addweb:fix/55847-object-cache-expire

Conversation

@divyesh-addweb

@divyesh-addweb divyesh-addweb commented Dec 3, 2026 •

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/55847

Problem

The default WP_Object_Cache ignores the $expire argument. In long-running requests, such as cron jobs, a value remains readable after it should have expired.

Solution

  • Store expiration timestamps only for keys set with $expire > 0.
  • Treat expired keys as missing in _exists() and remove them.
  • Setting a key again without an expiration clears the old one.
  • Clear expirations in delete(), flush(), flush_group() and reset().
  • incr() and decr() keep the existing expiration.

time() is only called for keys that have an expiration. External object cache drop-ins replace this class, so they are not affected.

Behavior changes

  • Expired keys are cache misses.
  • On an expired key, replace(), delete(), incr() and decr() return false, and add() succeeds.
  • Expiration has whole-second granularity: a value lasts at least the requested time, and up to about one second longer.

Testing

Adds 5 tests to tests/phpunit/tests/cache.php.

  • --group cache: passes on single site and multisite, on trunk at fd51bac.
  • Full PHPUnit suite (single site, before updating to fd51bac): no errors or failures; 86 PHPUnit deprecation warnings, 49 skipped.
  • WP-CLI: a value set with a 1-second expiration and read after 2 seconds was returned before the change and is false after it.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Writing the patch, unit tests and running the tests.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Keys stored with an expiration now expire during long-running requests.

See #55847.
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props divyeshpatel01, ravipatel.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@divyesh-addweb

Copy link
Copy Markdown
Author

@propsbot refresh

@rvpatel

rvpatel commented Dec 3, 2026

Copy link
Copy Markdown

Test Report

Environment: wordpress-develop Docker, PHP 8.3.35, default WP_Object_Cache (no drop-in), trunk 9fe0c05 + this PR (merges cleanly).

Steps: one wp eval-file process sets keys, sleep( 3 ), then reads them back.

After 3 seconds Before (trunk) After (this PR)
wp_cache_get() of a key set with $expire = 2 ❌ 'A', $found = true ✅ false, $found = false
wp_cache_incr() on that expired key ❌ 6 ✅ false
wp_cache_add() on that expired key ❌ false, old value kept ✅ true, new value stored
wp_cache_get_multiple() including it ❌ returned ✅ false for that key
no expiration / set again without expiration / 3600 / -5 kept kept ✅

Tests: tests/phpunit/tests/cache.php passes on single site (41 tests) and multisite (41 tests); --group cache passes (96 tests). With only the src/ change reverted, cache.php has 5 errors, so the new tests cover the change.

Works as described 👍

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.

2 participants