#37464 closed enhancement (fixed)
Last changed helper function
| Reported by: | spacedmonkey | Owned by: | jorbin |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.7 |
| Component: | Cache API | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | performance |
Description
The last changed value is used to invalidate many caches in core. Getting the code to last changed value is repeated through the core.
Attachments (3)
Change History (22)
#1
follow-up:
↓ 3
@
10 years ago
- Keywords has-patch 4.7-early added
- Milestone Awaiting Review → Future Release
This ticket was mentioned in Slack in #core by helen. View the logs.
10 years ago
#3
in reply to: ↑ 1
@
10 years ago
- Milestone Future Release → 4.7
Replying to DrewAPicture:
Also, we should follow current naming conventions and add
wp_get_to the front of the function name.
Would wp_cache_get_last_changed() be a more descriptive name? 37464.1.diff seems good otherwise.
#4
@
10 years ago
The fact that the incrementor is tied to the "last change" seems like an implementation detail that shouldn't matter to people building cache keys. The important fact is that it's an incrementor. What do people think about wp_get_cache_incrementor()?
#5
@
10 years ago
I think wp_cache_get_last_changed() sounds like the best name. It is an implementation detail, but I think this function is clearly tied to implementation and is not overly generalized.
This ticket was mentioned in Slack in #core by chriscct7. View the logs.
10 years ago
#8
@
10 years ago
- Keywords needs-refresh added; 4.7-early removed
Docbloc for new function is also incomplete
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
10 years ago
#10
@
10 years ago
- Keywords needs-refresh removed
Updated patch changes the function name to wp_cache_get_last_changed() and updates the Docblock.
Would this function be something that needs to be pluggable for when other caching mechanisms are in use? If so, then maybe this should go in cache.php instead of function.php.
This ticket was mentioned in Slack in #core by desrosj. View the logs.
10 years ago
This ticket was mentioned in Slack in #core by desrosj. View the logs.
10 years ago
This ticket was mentioned in Slack in #core by desrosj. View the logs.
10 years ago
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
10 years ago
#17
@
10 years ago
Is it too late in 4.7 to consider a wp_cache_set_last_changed()?
wp_cache_set( 'last_changed', microtime(), $foo ); is also used 11 times in core.
#18
@
4 years ago
I’ve opened a pull request for this: https://core.trac.wordpress.org/ticket/37464
This ticket was mentioned in PR #13961 on WordPress/wordpress-develop by @irozum.
2 weeks ago
#19
- Keywords has-unit-tests added; needs-unit-tests removed
This is a follow-up to [38849], which introduced wp_cache_get_last_changed() in wp-includes/functions.php (#37464), adding direct unit test coverage for it.
Why these tests:
- The sibling function
wp_cache_set_last_changed()already has dedicated coverage (tests/phpunit/tests/functions/wpCacheSetLastChanged.php, #59737), butwp_cache_get_last_changed()itself was only exercised incidentally viaassertNotFalse()assertions inside unrelated meta-cache tests (tests/phpunit/tests/post/meta.php,tests/phpunit/tests/term/meta.php,tests/phpunit/tests/comment/metaCache.php). None of those actually verified its read-or-initialize contract.
Cases covered in the new tests/phpunit/tests/functions/wpCacheGetLastChanged.php:
- Sets and returns a new value when nothing is cached yet for the group.
- Returns the existing cached value unchanged when one is already set.
- Returns the same value across repeated calls, confirming it doesn't silently regenerate the timestamp on every call.
Verification:
- For each test, I temporarily broke the corresponding branch of
wp_cache_get_last_changed()(forcing the cached-value check to always fail, and separately making the fallback return a fixed sentinel instead of delegating towp_cache_set_last_changed()), confirmed the relevant test(s) failed, then reverted. npm run test:php -- --filter Tests_Functions_wpCacheGetLastChangedandnpm run test:php -- --group 37464pass on trunk.composer lintis clean on the new file.
This PR is test-only — no production code under src/ is changed.
Use of AI Tools
AI assistance: Yes
Tool: Claude Code
Model: Claude Sonnet 5
Used for: Picking the ticket, writing the test cases, verifying each test actually fails against a deliberately broken implementation and passes against trunk, and drafting this PR description. I reviewed the diff and verification results before opening this PR.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
@spacedmonkey: Thanks for the patch. Looks like 37464.diff is missing inline documentation. Also, we should follow current naming conventions and add
wp_get_to the front of the function name.