/** * Start the output buffer * * @since 2.0.0 * * @param string $output * @return string */ protected function ob($output = '') { // Bail if cancelling if (true === $this->cancel) { return $output; } // PHP5 and objects disappearing before output buffers? wp_object_cache_init(); // $wp_object_cache was clobbered in wp-settings.php so repeat this $this->configure_groups(); // Unlock regeneration wp_cache_delete("{$this->url_key}_genlock", $this->group); // Do not cache blank pages unless they are HTTP redirects $output = trim($output); if (empty($output) && (empty($this->redirect_status) || empty($this->redirect_location))) { return; } // Do not cache 5xx responses if (isset($this->status_code) && intval($this->status_code / 100) == 5) { return $output; } // Variants and keys $this->do_variants($this->vary); $this->generate_keys(); // Construct and save the spider_cache $this->cache = array('output' => $output, 'time' => time(), 'headers' => array(), 'timer' => $this->timer_stop(false, 3), 'status_header' => $this->status_header, 'redirect_status' => $this->redirect_status, 'redirect_location' => $this->redirect_location, 'version' => $this->url_version); // PHP5 and higher ( foreach (headers_list() as $header) { list($k, $v) = array_map('trim', explode(':', $header, 2)); $this->cache['headers'][$k] = $v; } // Set uncached headers if (!empty($this->cache['headers']) && !empty($this->uncached_headers)) { foreach ($this->uncached_headers as $header) { unset($this->cache['headers'][$header]); } } // Set cached headers foreach ($this->cache['headers'] as $header => $values) { // Bail if cookies were set if (strtolower($header) === 'set-cookie') { return $output; } foreach ((array) $values as $value) { if (preg_match('/^Cache-Control:.*max-?age=(\\d+)/i', "{$header}: {$value}", $matches)) { $this->max_age = intval($matches[1]); } } } // Set max-age $this->cache['max_age'] = $this->max_age; // Set cache wp_cache_set($this->key, $this->cache, $this->group, $this->max_age + $this->seconds + 30); // Cache control if (true === $this->cache_control) { // Don't clobber Last-Modified header if already set, e.g. by WP::send_headers() if (!isset($this->cache['headers']['Last-Modified'])) { header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->cache['time']) . ' GMT', true); } if (!isset($this->cache['headers']['Cache-Control'])) { header("Cache-Control: max-age={$this->max_age}, must-revalidate", false); } } $this->do_headers($this->headers); // Add some debug info just before <head if (true === $this->debug) { $this->add_debug_just_cached(); } // Pass output to next ob handler return $this->cache['output']; }
<?php /** * Plugin name: Spider-Cache * Plugin URI: https://wordpress.org/plugins/wp-spider-cache/ * Description: Fully rendered pages stored in & served from Memcached. * Version: 2.1.0 */ // Exit if accessed directly defined('ABSPATH') || exit; // Required files require_once WP_CONTENT_DIR . '/plugins/wp-spider-cache/includes/functions.php'; // Initialize the caches if (!wp_skip_output_cache()) { wp_object_cache_init(); wp_output_cache_init(); }
/** * Clear a cached URL * * @since 2.3.0 * * @param string $url * * @return boolean */ public static function clean_url($url = '') { // Bail if no URL if (empty($url)) { return false; } // Bail if no persistent output cache if (!function_exists('wp_output_cache')) { return false; } // Normalize the URL if (0 === strpos($url, 'https://')) { $url = str_replace('https://', 'http://', $url); } if (0 !== strpos($url, 'http://')) { $url = 'http://' . $url; } $url_key = md5($url); // Get cache objects $output_cache = wp_output_cache_init(); $object_cache = wp_object_cache_init(); // Bail if either cache is missing if (empty($output_cache) || empty($object_cache)) { return; } wp_cache_add("{$url_key}_version", 0, $output_cache->group); $retval = wp_cache_incr("{$url_key}_version", 1, $output_cache->group); $output_cache_no_remote_group_key = array_search($output_cache->group, (array) $object_cache->no_remote_groups); // The *_version key needs to be replicated remotely, otherwise invalidation won't work. // The race condition here should be acceptable. if (false !== $output_cache_no_remote_group_key) { unset($object_cache->no_remote_groups[$output_cache_no_remote_group_key]); $retval = wp_cache_set("{$url_key}_version", $retval, $output_cache->group); $object_cache->no_remote_groups[$output_cache_no_remote_group_key] = $output_cache->group; } return $retval; }
/** * Returns the Object Cache Global. * * @since 2.0.0 * * @global WP_Spider_Cache_Object $wp_object_cache WordPress Object Cache * @return object */ function wp_object_cache() { if (!isset($GLOBALS['wp_object_cache'])) { wp_object_cache_init(); } return $GLOBALS['wp_object_cache']; }