Learn how to slash REST API response times in your WordPress plugins using object caching. Master cache invalidation and performance measurement strategies.
Previously in this course, we covered building search and filter functionality. While your endpoints are now functional and searchable, they might feel sluggish as your database grows. In this lesson, we’ll move beyond simple query execution and introduce caching to dramatically improve your plugin's performance.
WordPress performance is often bottlenecked by heavy database queries. When a user hits your REST API, WordPress bootstraps, authenticates, and executes SQL. If the data hasn't changed, repeating this work is wasteful.
We use the Object Cache (wp_cache_set and wp_cache_get) to store the results of expensive operations in memory. If you're using a persistent object cache backend like Redis or Memcached, this data persists across page loads, meaning your API can return a response in milliseconds rather than seconds.
To optimize our Knowledge Base plugin, we'll cache the output of our primary GET endpoint. Instead of running a fresh WP_Query every time, we check the cache first.
Here is how you implement this pattern in your REST API callback:
PHPfunction get_knowledge_base_items_cached( $request ) { $cache_key = 'kb_items_list_' . md5( serialize( $request->get_params() ) ); $cached_data = wp_cache_get( $cache_key, 'knowledge_base' ); if ( false !== $cached_data ) { return rest_ensure_response( $cached_data ); } #6A9955">// Perform the heavy lifting $query = new WP_Query( [ 'post_type' => 'kb_item' ] ); $data = array_map( 'prepare_kb_item_for_response', $query->posts ); #6A9955">// Store in cache for 1 hour wp_cache_set( $cache_key, $data, 'knowledge_base', HOUR_IN_SECONDS ); return rest_ensure_response( $data ); }
Caching is easy; invalidation is hard. If you update a knowledge base item but the user still sees the cached version, your UI will be inconsistent. You must hook into the update process to purge the cache.
Use wp_cache_delete or wp_cache_flush to clear the relevant keys whenever data changes:
PHPadd_action( 'save_post_kb_item', 'invalidate_kb_cache' ); function invalidate_kb_cache( $post_id ) { #6A9955">// Clear the group so all cached lists are regenerated wp_cache_flush_group( 'knowledge_base' ); }
Before and after optimization, you need hard data. Use the microtime(true) function to wrap your logic and log the duration to your debug log:
PHP$start = microtime(true); #6A9955">// ... your code ... $end = microtime(true); error_log( 'API Response Time: ' . ($end - $start) . ' seconds' );
For more advanced analysis, check out Optimizing Queries: WordPress Performance and Scaling Techniques to ensure your underlying SQL is lean before you even start caching. You might also want to look into WordPress performance: Preventing Cache Stampedes with Request Coalescing if your site experiences massive traffic spikes.
microtime logging to your existing GET endpoint.wp_cache_get and wp_cache_set.time reported in your debug.log. You should see a significant decrease on the second request.wp_cache is non-persistent by default. If your server doesn't have Redis or Memcached configured, the cache will only last for the duration of the single request. Always ensure your environment is configured for persistence if you want real-world performance gains.For more complex scenarios, refer to WordPress REST API Cache Invalidation: Dependency Graph Strategies to learn how to handle relationships between resources.
We've successfully moved from raw database queries to a cached architecture. By leveraging wp_cache_set and wp_cache_get with cache invalidation hooks, your plugin is now significantly faster and more scalable.
Up next: We will learn about Working with Date and Time in React to handle timestamps correctly in our admin dashboard.
Learn how to use WordPress transients to cache expensive database queries. Boost your plugin's speed and reduce database load with proper invalidation logic.
Read moreLearn how to optimize your WordPress queries for peak performance. Master meta_query parameters, no_found_rows, and object caching to scale your plugins effectively.
Optimizing API Response Times
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web