[wp-trac] [WordPress Trac] #62476: Need the ability to clear WP_Query cache partially and ability to set expiry for queries.

WordPress Trac noreply at wordpress.org
Tue Nov 19 11:38:05 UTC 2024


#62476: Need the ability to clear WP_Query cache partially and ability to set
expiry for queries.
-----------------------------------------+-----------------------------
 Reporter:  thekt12                      |      Owner:  (none)
     Type:  enhancement                  |     Status:  new
 Priority:  normal                       |  Milestone:  Awaiting Review
Component:  Database                     |    Version:
 Severity:  normal                       |   Keywords:
  Focuses:  performance, sustainability  |
-----------------------------------------+-----------------------------
 > **Background**
 During the audit of an enterprise client's website, it was observed that
 the core caching mechanism in WordPress had been disabled, and a custom
 caching mechanism was built around WP_Query. This custom implementation
 was introduced to enable selective cache flushing for specific queries
 after data synchronization from a third-party system.

 The primary motivation for this change was to avoid the "cache stampede"
 issue that occurred when flushing the entire WP_Query cache on high-
 traffic days. However, the custom solution, while addressing this specific
 need, lacked the robustness and performance benefits of WordPress's core
 caching mechanism.

 > **Current Limitation in WP_Query Caching**
 At present, WordPress does not provide a way to selectively remove cached
 entries for WP_Query. The current implementation of core allows either
 full caching or none at all. This limitation necessitated the custom
 implementation, but it also highlighted opportunities for improving the
 core functionality of WP_Query caching.

 > **Proposed Improvements**
 1. Selective Cache Flushing via Salted Keys
 Introduce an action after cache key generation code that takes
 `$cache_key` and `$args` as parameters.
 [https://github.com/WordPress/wordpress-
 develop/blob/11e4c7c5dd8c0366e0f911995881a6ca9a2323e5/src/wp-includes
 /class-wp-query.php#L3189 $cache_key   = $this->generate_cache_key( $q,
 $new_request );]

 {{{
 $cache_key   = $this->generate_cache_key( $q, $new_request );
 do_action( 'wp_query_cache_key_generated', $cache_key, $q );
 // $q is query args, and the developer can pass a salt, which can be used
 to identify a group of queries.
 }}}

 Developers can utilize this action to maintain a record of cache entries
 associated with a specific set, enabling targeted cache clearing when
 needed.

 2. Ability to set custom groups via query args
 While the exact implementation needs further exploration, introducing the
 ability to assign a custom group name to specific queries via query
 arguments would be highly beneficial. This feature would enable developers
 to group related queries under a custom group name, allowing for targeted
 cache invalidation. Additionally, it would facilitate better segregation
 and management of cached entries.

 > Additionally add support for `cache_expiry` in query args.
 Add support for a `cache_expiry` argument within WP_Query. This feature
 would allow developers to set a custom expiration time for cache entries.
 For example, data that frequently updates from the backend could have a
 shorter expiry time, reducing the need for manual cache-clearing
 mechanisms.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62476>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list