| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | Plugin Name: Advanced Post Caching
|
|---|
| 5 | Description: Cache post queries.
|
|---|
| 6 | Version: 0.2
|
|---|
| 7 | Author: Automattic
|
|---|
| 8 | Author URI: http://automattic.com/
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | class Advanced_Post_Cache {
|
|---|
| 12 | var $CACHE_GROUP_PREFIX = 'advanced_post_cache_';
|
|---|
| 13 |
|
|---|
| 14 | // Flag for temp (within one page load) turning invalidations on and off
|
|---|
| 15 | // @see dont_clear_advanced_post_cache()
|
|---|
| 16 | // @see do_clear_advanced_post_cache()
|
|---|
| 17 | // Used to prevent invalidation during new comment
|
|---|
| 18 | var $do_flush_cache = true;
|
|---|
| 19 |
|
|---|
| 20 | // Flag for preventing multiple invalidations in a row: clean_post_cache() calls itself recursively for post children.
|
|---|
| 21 | var $need_to_flush_cache = true; // Currently disabled
|
|---|
| 22 |
|
|---|
| 23 | /* Per cache-clear data */
|
|---|
| 24 | var $cache_incr = 0; // Increments the cache group (advanced_post_cache_0, advanced_post_cache_1, ...)
|
|---|
| 25 | var $cache_group = ''; // CACHE_GROUP_PREFIX . $cache_incr
|
|---|
| 26 |
|
|---|
| 27 | /* Per query data */
|
|---|
| 28 | var $cache_key = ''; // md5 of current SQL query
|
|---|
| 29 | var $all_post_ids = false; // IDs of all posts current SQL query returns
|
|---|
| 30 | var $cached_post_ids = array(); // subset of $all_post_ids whose posts are currently in cache
|
|---|
| 31 | var $cached_posts = array();
|
|---|
| 32 | var $found_posts = false; // The result of the FOUND_ROWS() query
|
|---|
| 33 | var $cache_func = 'wp_cache_add'; // Turns to set if there seems to be inconsistencies
|
|---|
| 34 |
|
|---|
| 35 | function __construct() {
|
|---|
| 36 | wp_cache_add_group_prefix_map( $this->CACHE_GROUP_PREFIX, 'advanced_post_cache' );
|
|---|
| 37 |
|
|---|
| 38 | $this->cache_incr = wp_cache_get( 'advanced_post_cache', 'cache_incrementors' ); // Get and construct current cache group name
|
|---|
| 39 | if ( !is_numeric( $this->cache_incr ) ) {
|
|---|
| 40 | $now = time();
|
|---|
| 41 | wp_cache_set( 'advanced_post_cache', $now, 'cache_incrementors' );
|
|---|
| 42 | $this->cache_incr = $now;
|
|---|
| 43 | }
|
|---|
| 44 | $this->cache_group = $this->CACHE_GROUP_PREFIX . $this->cache_incr;
|
|---|
| 45 |
|
|---|
| 46 | add_filter( 'posts_request', array( &$this, 'posts_request' ) ); // Short circuits if cached
|
|---|
| 47 | add_filter( 'posts_results', array( &$this, 'posts_results' ) ); // Collates if cached, primes cache if not
|
|---|
| 48 |
|
|---|
| 49 | add_filter( 'post_limits_request', array( &$this, 'post_limits_request' ), 999, 2 ); // Checks to see if we need to worry about found_posts
|
|---|
| 50 |
|
|---|
| 51 | add_filter( 'found_posts_query', array( &$this, 'found_posts_query' ) ); // Short circuits if cached
|
|---|
| 52 | add_filter( 'found_posts', array( &$this, 'found_posts' ) ); // Reads from cache if cached, primes cache if not
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /* Advanced Post Cache API */
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Flushes the cache by incrementing the cache group
|
|---|
| 59 | */
|
|---|
| 60 | function flush_cache() {
|
|---|
| 61 | // Cache flushes have been disabled
|
|---|
| 62 | if ( !$this->do_flush_cache )
|
|---|
| 63 | return;
|
|---|
| 64 |
|
|---|
| 65 | // Bail on post preview
|
|---|
| 66 | if ( is_admin() && isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] )
|
|---|
| 67 | return;
|
|---|
| 68 |
|
|---|
| 69 | // Bail on autosave
|
|---|
| 70 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
|
|---|
| 71 | return;
|
|---|
| 72 |
|
|---|
| 73 | // We already flushed once this page load, and have not put anything into the cache since.
|
|---|
| 74 | // OTHER processes may have put something into the cache! In theory, this could cause stale caches.
|
|---|
| 75 | // We do this since clean_post_cache() (which fires the action this method attaches to) is called RECURSIVELY for all descendants.
|
|---|
| 76 | // if ( !$this->need_to_flush_cache )
|
|---|
| 77 | // return;
|
|---|
| 78 |
|
|---|
| 79 | $this->cache_incr = wp_cache_incr( 'advanced_post_cache', 1, 'cache_incrementors' );
|
|---|
| 80 | if ( 10 < strlen( $this->cache_incr ) ) {
|
|---|
| 81 | wp_cache_set( 'advanced_post_cache', 0, 'cache_incrementors' );
|
|---|
| 82 | $this->cache_incr = 0;
|
|---|
| 83 | }
|
|---|
| 84 | $this->cache_group = $this->CACHE_GROUP_PREFIX . $this->cache_incr;
|
|---|
| 85 | $this->need_to_flush_cache = false;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | /* Cache Reading/Priming Functions */
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Determines (by hash of SQL) if query is cached.
|
|---|
| 93 | * If cached: Return query of needed post IDs.
|
|---|
| 94 | * Otherwise: Returns query unchanged.
|
|---|
| 95 | */
|
|---|
| 96 | function posts_request( $sql ) {
|
|---|
| 97 | global $wpdb;
|
|---|
| 98 |
|
|---|
| 99 | $this->cache_key = md5( $sql ); // init
|
|---|
| 100 | $this->all_post_ids = wp_cache_get( $this->cache_key, $this->cache_group );
|
|---|
| 101 | if ( 'NA' !== $this->found_posts )
|
|---|
| 102 | $this->found_posts = wp_cache_get( "{$this->cache_key}_found", $this->cache_group );
|
|---|
| 103 |
|
|---|
| 104 | if ( $this->all_post_ids xor $this->found_posts )
|
|---|
| 105 | $this->cache_func = 'wp_cache_set';
|
|---|
| 106 | else
|
|---|
| 107 | $this->cache_func = 'wp_cache_add';
|
|---|
| 108 |
|
|---|
| 109 | $this->cached_post_ids = array(); // re-init
|
|---|
| 110 | $this->cached_posts = array(); // re-init
|
|---|
| 111 |
|
|---|
| 112 | // Query is cached
|
|---|
| 113 | if ( $this->found_posts && is_array( $this->all_post_ids ) ) {
|
|---|
| 114 | $this->cached_posts = array_filter( wp_cache_get_multi( array( 'posts' => $this->all_post_ids ) ) );
|
|---|
| 115 | foreach ( $this->cached_posts as $post ) {
|
|---|
| 116 | if ( !empty( $post ) )
|
|---|
| 117 | $this->cached_post_ids[] = $post->ID;
|
|---|
| 118 | }
|
|---|
| 119 | $uncached_post_ids = array_diff( $this->all_post_ids, $this->cached_post_ids );
|
|---|
| 120 |
|
|---|
| 121 | if ( $uncached_post_ids )
|
|---|
| 122 | return "SELECT * FROM $wpdb->posts WHERE ID IN(" . join( ',', array_map( 'absint', $uncached_post_ids ) ) . ")";
|
|---|
| 123 | return '';
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | return $sql;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * If cached: Collates posts returned by SQL query with posts that are already cached. Orders correctly.
|
|---|
| 131 | * Otherwise: Primes cache with data for current posts WP_Query.
|
|---|
| 132 | */
|
|---|
| 133 | function posts_results( $posts ) {
|
|---|
| 134 | if ( $this->found_posts && is_array( $this->all_post_ids ) ) { // is cached
|
|---|
| 135 | $collated_posts = array();
|
|---|
| 136 | foreach ( $this->cached_posts as $post )
|
|---|
| 137 | $posts[] = $post;
|
|---|
| 138 |
|
|---|
| 139 | foreach ( $posts as $post ) {
|
|---|
| 140 | $loc = array_search( $post->ID, $this->all_post_ids );
|
|---|
| 141 | if ( is_numeric( $loc ) && -1 < $loc )
|
|---|
| 142 | $collated_posts[$loc] = $post;
|
|---|
| 143 | }
|
|---|
| 144 | ksort( $collated_posts );
|
|---|
| 145 | return array_values( $collated_posts );
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | $post_ids = array();
|
|---|
| 149 | foreach ( $posts as $post )
|
|---|
| 150 | $post_ids[] = $post->ID;
|
|---|
| 151 |
|
|---|
| 152 | if ( !$post_ids )
|
|---|
| 153 | return array();
|
|---|
| 154 |
|
|---|
| 155 | call_user_func( $this->cache_func, $this->cache_key, $post_ids, $this->cache_group );
|
|---|
| 156 | $this->need_to_flush_cache = true;
|
|---|
| 157 |
|
|---|
| 158 | return $posts;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * If $limits is empty, WP_Query never calls the found_rows stuff, so we set $this->found_rows to 'NA'
|
|---|
| 163 | */
|
|---|
| 164 | function post_limits_request( $limits, &$query ) {
|
|---|
| 165 | if ( empty( $limits ) || ( isset( $query->query_vars['no_found_rows'] ) && $query->query_vars['no_found_rows'] ) )
|
|---|
| 166 | $this->found_posts = 'NA';
|
|---|
| 167 | else
|
|---|
| 168 | $this->found_posts = false; // re-init
|
|---|
| 169 | return $limits;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * If cached: Blanks SELECT FOUND_ROWS() query. This data is already stored in cache.
|
|---|
| 174 | * Otherwise: Returns query unchanged.
|
|---|
| 175 | */
|
|---|
| 176 | function found_posts_query( $sql ) {
|
|---|
| 177 | if ( $this->found_posts && is_array( $this->all_post_ids ) ) // is cached
|
|---|
| 178 | return '';
|
|---|
| 179 | return $sql;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * If cached: Returns cached result of FOUND_ROWS() query.
|
|---|
| 184 | * Otherwise: Returs result unchanged
|
|---|
| 185 | */
|
|---|
| 186 | function found_posts( $found_posts ) {
|
|---|
| 187 | if ( $this->found_posts && is_array( $this->all_post_ids ) ) // is cached
|
|---|
| 188 | return (int) $this->found_posts;
|
|---|
| 189 |
|
|---|
| 190 | call_user_func( $this->cache_func, "{$this->cache_key}_found", (int) $found_posts, $this->cache_group );
|
|---|
| 191 | $this->need_to_flush_cache = true;
|
|---|
| 192 |
|
|---|
| 193 | return $found_posts;
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | global $advanced_post_cache_object;
|
|---|
| 198 | $advanced_post_cache_object = new Advanced_Post_Cache;
|
|---|
| 199 |
|
|---|
| 200 | function clear_advanced_post_cache() {
|
|---|
| 201 | global $advanced_post_cache_object;
|
|---|
| 202 | $advanced_post_cache_object->flush_cache();
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | function do_clear_advanced_post_cache() {
|
|---|
| 206 | $GLOBALS['advanced_post_cache_object']->do_flush_cache = true;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | function dont_clear_advanced_post_cache() {
|
|---|
| 210 | $GLOBALS['advanced_post_cache_object']->do_flush_cache = false;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | add_action( 'clean_term_cache', 'clear_advanced_post_cache' );
|
|---|
| 214 | add_action( 'clean_post_cache', 'clear_advanced_post_cache' );
|
|---|
| 215 |
|
|---|
| 216 | // Don't clear Advanced Post Cache for a new comment - temp core hack
|
|---|
| 217 | // http://core.trac.wordpress.org/ticket/15565
|
|---|
| 218 | add_action( 'wp_updating_comment_count', 'dont_clear_advanced_post_cache' );
|
|---|
| 219 | add_action( 'wp_update_comment_count' , 'do_clear_advanced_post_cache' );
|
|---|