Last active
December 14, 2015 07:58
-
-
Save wren-hearn/5053954 to your computer and use it in GitHub Desktop.
Incomplete example of how one would cache the default WordPress query so that the database is never hit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_request', 'pre_db_hit' ); | |
function pre_db_hit( $request ) | |
{ | |
global $diy_cache_current_request; | |
$diy_cache_current_request = $request; | |
// use request string to see if expired... | |
if ( $diy_cache_expired ) | |
return $request; // Let query happen | |
return null; // null request shuts down execution of the query | |
} | |
add_filter( 'posts_results', 'post_db_hit' ); | |
function post_db_hit( $post_objects ) | |
{ | |
global $diy_cache_current_request; | |
// use request string to see if expired... | |
if ( $diy_cache_expired ) | |
{ | |
// Store the post objects in the cache | |
// under a key consisting of a hash of | |
// the request string... | |
} else { | |
// Get the post objects from your DIY | |
// cache and return them... | |
return $cached_post_objects; | |
} | |
return $post_objects; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment