Skip to content

Add cache_calls metric for persistent object cache integration#219

Draft
Copilot wants to merge 5 commits into
mainfrom
copilot/integrate-persistent-object-caches
Draft

Add cache_calls metric for persistent object cache integration#219
Copilot wants to merge 5 commits into
mainfrom
copilot/integrate-persistent-object-caches

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

Multiple persistent object cache plugins expose backend call counts, but wp profile had no way to surface this data.

Changes

  • src/Logger.php — adds $cache_calls public property; adds get_object_cache_calls() static helper that reads from whichever backend is active; start() captures the offset, stop() computes and stores the delta. Gracefully no-ops (shows 0) when no supported backend is present.
  • src/Profiler.php — adds $tick_cache_calls_offset for tick-based (function-level) profiling; mirrors the same offset/delta pattern in handle_function_tick() via the shared helper.
  • src/Command.php — adds cache_calls to the $metrics fields for stage, hook, eval, and eval-file subcommands.
  • features/profile-eval.feature — two new scenarios: one asserting cache_calls = 0 with the default cache; one using a minimal object-cache.php drop-in that mimics the cache_calls array format to assert the count is correct.

Supported backends

The get_object_cache_calls() helper checks each source in order:

Plugin Property / Method
WP Redis $wp_object_cache->redis_calls (array of command → count)
Redis Object Cache $wp_object_cache->cache_calls (array of command → count)
Object Cache Pro $wp_object_cache->metrics()->storeReads + storeWrites

Example output

$ wp profile eval 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' \
    --fields=cache_hits,cache_misses,cache_calls
+------------+--------------+-------------+
| cache_hits | cache_misses | cache_calls |
+------------+--------------+-------------+
| 2          | 0            | 3           |
+------------+--------------+-------------+

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 58.13953% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Profiler.php 0.00% 14 Missing ⚠️
src/Logger.php 84.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI and others added 2 commits April 29, 2026 20:37
Copilot AI changed the title [WIP] Add integration for persistent object caches Add redis_calls metric for persistent object cache integration Apr 29, 2026
Copilot AI requested a review from swissspidy April 29, 2026 20:39
Comment thread src/Profiler.php Outdated
Comment thread src/Profiler.php Outdated
@swissspidy
Copy link
Copy Markdown
Member

@copilot address the review comments

#219 (comment)

#219 (comment)

…ct cache backends

Agent-Logs-Url: https://github.com/wp-cli/profile-command/sessions/96870b9d-2d37-4ac3-b192-b5d30a51d5fe

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 19, 2026

@copilot address the review comments

#219 (comment)
...

Done in 20c95b9. Two changes made:

  1. Renamed the metric from redis_calls to cache_calls across all files (Logger, Profiler, Command, and feature tests).

  2. Added a Logger::get_object_cache_calls() static helper that checks all three backends in order:

    • WP Redis: $wp_object_cache->redis_calls (array)
    • Redis Object Cache: $wp_object_cache->cache_calls (array)
    • Object Cache Pro: $wp_object_cache->metrics()->storeReads + storeWrites

    Falls back to null (not tracked) when none of these are present, so the default WP object cache shows 0.

Copilot AI changed the title Add redis_calls metric for persistent object cache integration Add cache_calls metric for persistent object cache integration May 19, 2026
@swissspidy
Copy link
Copy Markdown
Member

@copilot fix failing tests. apparently get_object_cache_calls() is being passed null as an argument

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 19, 2026

@copilot fix failing tests. apparently get_object_cache_calls() is being passed null as an argument

Fixed in e13fa72. Added an is_object() guard at the top of get_object_cache_calls() — PHP 8 made method_exists(null, ...) throw a TypeError, so the function now returns null early when passed a non-object (including null).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrate with persistent object caches

3 participants