Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 2 weeks ago

#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)

37464.diff​ (6.0 KB ) - added by spacedmonkey 10 years ago.
37464.1.diff​ (6.1 KB ) - added by spacedmonkey 10 years ago.
37464.2.diff​ (6.3 KB ) - added by desrosj 10 years ago.

Download all attachments as: .zip

Change History (22)

@spacedmonkey
10 years ago

#1 follow-up: @DrewAPicture
10 years ago

  • Keywords has-patch 4.7-early added
  • Milestone Awaiting Review → Future Release

@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.

This ticket was mentioned in ​Slack in #core by helen. ​View the logs.


10 years ago

#3 in reply to: ↑ 1 @SergeyBiryukov
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 @boonebgorges
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 @jorbin
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

#7 @chriscct7
10 years ago

  • Keywords needs-unit-tests added

#8 @chriscct7
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

@desrosj
10 years ago

#10 @desrosj
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

#13 @jorbin
10 years ago

  • Owner set to jorbin
  • Status new → assigned

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

#16 @jorbin
10 years ago

  • Resolution → fixed
  • Status assigned → closed

In 38849:

Cache API: introduce wp_cache_get_last_changed to improve DRY

One thing fairly common to the cache groups is a block of code to look to see when the cache was last changed, and if there isn't one, to set it for the current microtime(). It appears in 8 different places in core. This adds a new helper wp_cache_get_last_changed to DRY things up a bit.

Since wp-includes/cache.php isn't guaranteed to be loaded, this new function is in wp-includes/functions.php

Props spacedmonkey, desrosj.
Fixes #37464.

#17 @johnjamesjacoby
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 @tillkruess
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), but wp_cache_get_last_changed() itself was only exercised incidentally via assertNotFalse() 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 to wp_cache_set_last_changed()), confirmed the relevant test(s) failed, then reverted.
  • npm run test:php -- --filter Tests_Functions_wpCacheGetLastChanged and npm run test:php -- --group 37464 pass on trunk.
  • composer lint is 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.

Note: See TracTickets for help on using tickets.