From 248166f05686c9874dcc2bd7ea277734a572747f Mon Sep 17 00:00:00 2001 From: Amedeo Date: Sun, 22 Sep 2019 19:33:37 +0200 Subject: [PATCH] FIRST COMMIT - start cache plug-in --- public/content/advanced-cache.php | 651 +++++++++++++++--------------- public/content/object-cache.php | 581 +++++++++++++------------- public/testDB.php | 8 + 3 files changed, 625 insertions(+), 615 deletions(-) create mode 100644 public/testDB.php diff --git a/public/content/advanced-cache.php b/public/content/advanced-cache.php index 83f24eefc..c0997a08b 100644 --- a/public/content/advanced-cache.php +++ b/public/content/advanced-cache.php @@ -1,379 +1,380 @@ cancel = true; -} + if ( is_object($batcache) ) + $batcache->cancel = true; + } -class batcache { - // This is the base configuration. You can edit these variables or move them into your wp-config.php file. - var $max_age = 300; // Expire batcache items aged this many seconds (zero to disable batcache) - - var $remote = 0; // Zero disables sending buffers to remote datacenters (req/sec is never sent) - - var $times = 2; // Only batcache a page after it is accessed this many times... (two or more) - var $seconds = 120; // ...in this many seconds (zero to ignore this and use batcache immediately) - - var $group = 'batcache'; // Name of memcached group. You can simulate a cache flush by changing this. - - var $unique = array(); // If you conditionally serve different content, put the variable values here. - - var $headers = array(); // Add headers here. These will be sent with every response from the cache. + class batcache { + // This is the base configuration. You can edit these variables or move them into your wp-config.php file. + var $max_age = 300; // Expire batcache items aged this many seconds (zero to disable batcache) + + var $remote = 0; // Zero disables sending buffers to remote datacenters (req/sec is never sent) + + var $times = 2; // Only batcache a page after it is accessed this many times... (two or more) + var $seconds = 120; // ...in this many seconds (zero to ignore this and use batcache immediately) + + var $group = 'batcache'; // Name of memcached group. You can simulate a cache flush by changing this. + + var $unique = array(); // If you conditionally serve different content, put the variable values here. + + var $headers = array(); // Add headers here. These will be sent with every response from the cache. - var $cache_redirects = false; // Set true to enable redirect caching. - var $redirect_status = false; // This is set to the response code during a redirect. - var $redirect_location = false; // This is set to the redirect location. + var $cache_redirects = false; // Set true to enable redirect caching. + var $redirect_status = false; // This is set to the response code during a redirect. + var $redirect_location = false; // This is set to the redirect location. - var $uncached_headers = array('transfer-encoding'); // These headers will never be cached. Apply strtolower. + var $uncached_headers = array('transfer-encoding'); // These headers will never be cached. Apply strtolower. - var $debug = true; // Set false to hide the batcache info + var $debug = true; // Set false to hide the batcache info - var $cache_control = true; // Set false to disable Last-Modified and Cache-Control headers + var $cache_control = true; // Set false to disable Last-Modified and Cache-Control headers - var $cancel = false; // Change this to cancel the output buffer. Use batcache_cancel(); + var $cancel = false; // Change this to cancel the output buffer. Use batcache_cancel(); - var $genlock; // Used internally - var $do; // Used internally + var $genlock; // Used internally + var $do; // Used internally - function batcache( $settings ) { - if ( is_array( $settings ) ) foreach ( $settings as $k => $v ) - $this->$k = $v; - } + function batcache( $settings ) { + if ( is_array( $settings ) ) foreach ( $settings as $k => $v ) + $this->$k = $v; + } - function is_ssl() { - if ( isset($_SERVER['HTTPS']) ) { - if ( 'on' == strtolower($_SERVER['HTTPS']) ) - return true; - if ( '1' == $_SERVER['HTTPS'] ) + function is_ssl() { + if ( isset($_SERVER['HTTPS']) ) { + if ( 'on' == strtolower($_SERVER['HTTPS']) ) + return true; + if ( '1' == $_SERVER['HTTPS'] ) + return true; + } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { return true; - } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { - return true; + } + return false; } - return false; - } - function status_header( $status_header ) { - $this->status_header = $status_header; + function status_header( $status_header ) { + $this->status_header = $status_header; - return $status_header; - } - - function redirect_status( $status, $location ) { - if ( $this->cache_redirects ) { - $this->redirect_status = $status; - $this->redirect_location = $location; + return $status_header; } - return $status; - } + function redirect_status( $status, $location ) { + if ( $this->cache_redirects ) { + $this->redirect_status = $status; + $this->redirect_location = $location; + } - function configure_groups() { - // Configure the memcached client - if ( ! $this->remote ) - if ( function_exists('wp_cache_add_no_remote_groups') ) - wp_cache_add_no_remote_groups(array($this->group)); - if ( function_exists('wp_cache_add_global_groups') ) - wp_cache_add_global_groups(array($this->group)); - } + return $status; + } - // Defined here because timer_stop() calls number_format_i18n() - function timer_stop($display = 0, $precision = 3) { - global $timestart, $timeend; - $mtime = microtime(); - $mtime = explode(' ',$mtime); - $mtime = $mtime[1] + $mtime[0]; - $timeend = $mtime; - $timetotal = $timeend-$timestart; - $r = number_format($timetotal, $precision); - if ( $display ) - echo $r; - return $r; - } + function configure_groups() { + // Configure the memcached client + if ( ! $this->remote ) + if ( function_exists('wp_cache_add_no_remote_groups') ) + wp_cache_add_no_remote_groups(array($this->group)); + if ( function_exists('wp_cache_add_global_groups') ) + wp_cache_add_global_groups(array($this->group)); + } - function ob($output) { - if ( $this->cancel !== false ) - return $output; + // Defined here because timer_stop() calls number_format_i18n() + function timer_stop($display = 0, $precision = 3) { + global $timestart, $timeend; + $mtime = microtime(); + $mtime = explode(' ',$mtime); + $mtime = $mtime[1] + $mtime[0]; + $timeend = $mtime; + $timetotal = $timeend-$timestart; + $r = number_format($timetotal, $precision); + if ( $display ) + echo $r; + return $r; + } - // PHP5 and objects disappearing before output buffers? - wp_cache_init(); - - // Remember, $wp_object_cache was clobbered in wp-settings.php so we have to repeat this. - $this->configure_groups(); - - // Do not batcache blank pages unless they are HTTP redirects - $output = trim($output); - if ( $output === '' && (!$this->redirect_status || !$this->redirect_location) ) - return; - - // Construct and save the batcache - $cache = array( - 'output' => $output, - 'time' => time(), - '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 - ); - - if ( function_exists( 'headers_list' ) ) { - foreach ( headers_list() as $header ) { - list($k, $v) = array_map('trim', explode(':', $header, 2)); - $cache['headers'][$k] = $v; + function ob($output) { + if ( $this->cancel !== false ) + return $output; + + // PHP5 and objects disappearing before output buffers? + wp_cache_init(); + + // Remember, $wp_object_cache was clobbered in wp-settings.php so we have to repeat this. + $this->configure_groups(); + + // Do not batcache blank pages unless they are HTTP redirects + $output = trim($output); + if ( $output === '' && (!$this->redirect_status || !$this->redirect_location) ) + return; + + // Construct and save the batcache + $cache = array( + 'output' => $output, + 'time' => time(), + '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 + ); + + if ( function_exists( 'headers_list' ) ) { + foreach ( headers_list() as $header ) { + list($k, $v) = array_map('trim', explode(':', $header, 2)); + $cache['headers'][$k] = $v; + } + } elseif ( function_exists( 'apache_response_headers' ) ) { + $cache['headers'] = apache_response_headers(); } - } elseif ( function_exists( 'apache_response_headers' ) ) { - $cache['headers'] = apache_response_headers(); - } - if ( $cache['headers'] && !empty( $this->uncached_headers ) ) { - foreach ( $cache['headers'] as $header => $value ) { - if ( in_array( strtolower( $header ), $this->uncached_headers ) ) - unset( $cache['headers'][$header] ); + if ( $cache['headers'] && !empty( $this->uncached_headers ) ) { + foreach ( $cache['headers'] as $header => $value ) { + if ( in_array( strtolower( $header ), $this->uncached_headers ) ) + unset( $cache['headers'][$header] ); + } } - } - wp_cache_set($this->key, $cache, $this->group, $this->max_age + $this->seconds + 30); + wp_cache_set($this->key, $cache, $this->group, $this->max_age + $this->seconds + 30); - // Unlock regeneration - wp_cache_delete("{$this->url_key}_genlock", $this->group); + // Unlock regeneration + wp_cache_delete("{$this->url_key}_genlock", $this->group); - if ( $this->cache_control ) { - header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $cache['time'] ) . ' GMT', true ); - header("Cache-Control: max-age=$this->max_age, must-revalidate", false); - } + if ( $this->cache_control ) { + header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $cache['time'] ) . ' GMT', true ); + header("Cache-Control: max-age=$this->max_age, must-revalidate", false); + } - if ( !empty($this->headers) ) foreach ( $this->headers as $k => $v ) { - if ( is_array( $v ) ) - header("{$v[0]}: {$v[1]}", false); - else - header("$k: $v", true); - } + if ( !empty($this->headers) ) foreach ( $this->headers as $k => $v ) { + if ( is_array( $v ) ) + header("{$v[0]}: {$v[1]}", false); + else + header("$k: $v", true); + } - // Add some debug info just before - if ( $this->debug ) { - $tag = "\n"; - if ( false !== $tag_position = strpos($output, '') ) { + // Add some debug info just before + if ( $this->debug ) { $tag = "\n"; - $output = substr($output, 0, $tag_position) . $tag . substr($output, $tag_position); + if ( false !== $tag_position = strpos($output, '') ) { + $tag = "\n"; + $output = substr($output, 0, $tag_position) . $tag . substr($output, $tag_position); + } } - } - // Pass output to next ob handler - return $output; + // Pass output to next ob handler + return $output; + } } -} -global $batcache; -// Pass in the global variable which may be an array of settings to override defaults. -$batcache = new batcache($batcache); - -if ( ! defined( 'WP_CONTENT_DIR' ) ) - return; - -// Never batcache interactive scripts or API endpoints. -if ( in_array( - basename( $_SERVER['SCRIPT_FILENAME'] ), - array( - 'wp-app.php', - 'xmlrpc.php', - 'ms-files.php', - ) ) ) - return; - -// Never batcache WP javascript generators -if ( strstr( $_SERVER['SCRIPT_FILENAME'], 'wp-includes/js' ) ) - return; - -// Never batcache when POST data is present. -if ( ! empty( $GLOBALS['HTTP_RAW_POST_DATA'] ) || ! empty( $_POST ) ) - return; - -// Never batcache when cookies indicate a cache-exempt visitor. -if ( is_array( $_COOKIE) && ! empty( $_COOKIE ) ) - foreach ( array_keys( $_COOKIE ) as $batcache->cookie ) - if ( $batcache->cookie != 'wordpress_test_cookie' && ( substr( $batcache->cookie, 0, 2 ) == 'wp' || substr( $batcache->cookie, 0, 9 ) == 'wordpress' || substr( $batcache->cookie, 0, 14 ) == 'comment_author' ) ) - return; - -if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) ) - return; - -wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here. - -if ( ! is_object( $wp_object_cache ) ) - return; - -// Now that the defaults are set, you might want to use different settings under certain conditions. - -/* Example: if your documents have a mobile variant (a different document served by the same URL) you must tell batcache about the variance. Otherwise you might accidentally cache the mobile version and serve it to desktop users, or vice versa. -$batcache->unique['mobile'] = is_mobile_user_agent(); -*/ - -/* Example: never batcache for this host -if ( $_SERVER['HTTP_HOST'] == 'do-not-batcache-me.com' ) - return; -*/ - -/* Example: batcache everything on this host regardless of traffic level -if ( $_SERVER['HTTP_HOST'] == 'always-batcache-me.com' ) { - $batcache->max_age = 600; // Cache for 10 minutes - $batcache->seconds = $batcache->times = 0; // No need to wait till n number of people have accessed the page, cache instantly -} -*/ - -/* Example: If you sometimes serve variants dynamically (e.g. referrer search term highlighting) you probably don't want to batcache those variants. Remember this code is run very early in wp-settings.php so plugins are not yet loaded. You will get a fatal error if you try to call an undefined function. Either include your plugin now or define a test function in this file. -if ( include_once( 'plugins/searchterm-highlighter.php') && referrer_has_search_terms() ) - return; -*/ - -// Disabled -if ( $batcache->max_age < 1 ) - return; - -// Make sure we can increment. If not, turn off the traffic sensor. -if ( ! method_exists( $GLOBALS['wp_object_cache'], 'incr' ) ) - $batcache->times = 0; - -// Necessary to prevent clients using cached version after login cookies set. If this is a problem, comment it out and remove all Last-Modified headers. -header('Vary: Cookie', false); - -// Things that define a unique page. -if ( isset( $_SERVER['QUERY_STRING'] ) ) - parse_str($_SERVER['QUERY_STRING'], $batcache->query); -$batcache->keys = array( - 'host' => $_SERVER['HTTP_HOST'], - 'method' => $_SERVER['REQUEST_METHOD'], - 'path' => ( $batcache->pos = strpos($_SERVER['REQUEST_URI'], '?') ) ? substr($_SERVER['REQUEST_URI'], 0, $batcache->pos) : $_SERVER['REQUEST_URI'], - 'query' => $batcache->query, - 'extra' => $batcache->unique -); - -if ( $batcache->is_ssl() ) - $batcache->keys['ssl'] = true; - -$batcache->configure_groups(); - -// Generate the batcache key -$batcache->key = md5(serialize($batcache->keys)); - -// Generate the traffic threshold measurement key -$batcache->req_key = $batcache->key . '_req'; - -// Get the batcache -$batcache->cache = wp_cache_get($batcache->key, $batcache->group); - -// Are we only caching frequently-requested pages? -if ( $batcache->seconds < 1 || $batcache->times < 2 ) { - $batcache->do = true; -} else { - // No batcache item found, or ready to sample traffic again at the end of the batcache life? - if ( !is_array($batcache->cache) || time() >= $batcache->cache['time'] + $batcache->max_age - $batcache->seconds ) { - wp_cache_add($batcache->req_key, 0, $batcache->group); - $batcache->requests = wp_cache_incr($batcache->req_key, 1, $batcache->group); - - if ( $batcache->requests >= $batcache->times ) - $batcache->do = true; - else - $batcache->do = false; + global $batcache; + // Pass in the global variable which may be an array of settings to override defaults. + $batcache = new batcache($batcache); + + if ( ! defined( 'WP_CONTENT_DIR' ) ) + return; + + // Never batcache interactive scripts or API endpoints. + if ( in_array( + basename( $_SERVER['SCRIPT_FILENAME'] ), + array( + 'wp-app.php', + 'xmlrpc.php', + 'ms-files.php', + ) ) ) + return; + + // Never batcache WP javascript generators + if ( strstr( $_SERVER['SCRIPT_FILENAME'], 'wp-includes/js' ) ) + return; + + // Never batcache when POST data is present. + if ( ! empty( $GLOBALS['HTTP_RAW_POST_DATA'] ) || ! empty( $_POST ) ) + return; + + // Never batcache when cookies indicate a cache-exempt visitor. + if ( is_array( $_COOKIE) && ! empty( $_COOKIE ) ) + foreach ( array_keys( $_COOKIE ) as $batcache->cookie ) + if ( $batcache->cookie != 'wordpress_test_cookie' && ( substr( $batcache->cookie, 0, 2 ) == 'wp' || substr( $batcache->cookie, 0, 9 ) == 'wordpress' || substr( $batcache->cookie, 0, 14 ) == 'comment_author' ) ) + return; + + if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) ) + return; + + wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here. + + if ( ! is_object( $wp_object_cache ) ) + return; + + // Now that the defaults are set, you might want to use different settings under certain conditions. + + /* Example: if your documents have a mobile variant (a different document served by the same URL) you must tell batcache about the variance. Otherwise you might accidentally cache the mobile version and serve it to desktop users, or vice versa. + $batcache->unique['mobile'] = is_mobile_user_agent(); + */ + + /* Example: never batcache for this host + if ( $_SERVER['HTTP_HOST'] == 'do-not-batcache-me.com' ) + return; + */ + + /* Example: batcache everything on this host regardless of traffic level + if ( $_SERVER['HTTP_HOST'] == 'always-batcache-me.com' ) { + $batcache->max_age = 600; // Cache for 10 minutes + $batcache->seconds = $batcache->times = 0; // No need to wait till n number of people have accessed the page, cache instantly } -} - -// Recreate the permalink from the URL -$batcache->permalink = 'http://' . $batcache->keys['host'] . $batcache->keys['path'] . ( isset($batcache->keys['query']['p']) ? "?p=" . $batcache->keys['query']['p'] : '' ); -$batcache->url_key = md5($batcache->permalink); -$batcache->url_version = (int) wp_cache_get("{$batcache->url_key}_version", $batcache->group); - -// If the document has been updated and we are the first to notice, regenerate it. -if ( $batcache->do !== false && isset($batcache->cache['version']) && $batcache->cache['version'] < $batcache->url_version ) - $batcache->genlock = wp_cache_add("{$batcache->url_key}_genlock", 1, $batcache->group); - -// Did we find a batcached page that hasn't expired? -if ( isset($batcache->cache['time']) && ! $batcache->genlock && time() < $batcache->cache['time'] + $batcache->max_age ) { - // Issue redirect if cached and enabled - if ( $batcache->cache['redirect_status'] && $batcache->cache['redirect_location'] && $batcache->cache_redirects ) { - $status = $batcache->cache['redirect_status']; - $location = $batcache->cache['redirect_location']; - // From vars.php - $is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer') !== false); - if ( $is_IIS ) { - header("Refresh: 0;url=$location"); - } else { - if ( php_sapi_name() != 'cgi-fcgi' ) { - $texts = array( - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Found', - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 306 => 'Reserved', - 307 => 'Temporary Redirect', - ); - $protocol = $_SERVER["SERVER_PROTOCOL"]; - if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) - $protocol = 'HTTP/1.0'; - if ( isset($texts[$status]) ) - header("$protocol $status " . $texts[$status]); - else - header("$protocol 302 Found"); - } - header("Location: $location"); + */ + + /* Example: If you sometimes serve variants dynamically (e.g. referrer search term highlighting) you probably don't want to batcache those variants. Remember this code is run very early in wp-settings.php so plugins are not yet loaded. You will get a fatal error if you try to call an undefined function. Either include your plugin now or define a test function in this file. + if ( include_once( 'plugins/searchterm-highlighter.php') && referrer_has_search_terms() ) + return; + */ + + // Disabled + if ( $batcache->max_age < 1 ) + return; + + // Make sure we can increment. If not, turn off the traffic sensor. + if ( ! method_exists( $GLOBALS['wp_object_cache'], 'incr' ) ) + $batcache->times = 0; + + // Necessary to prevent clients using cached version after login cookies set. If this is a problem, comment it out and remove all Last-Modified headers. + header('Vary: Cookie', false); + + // Things that define a unique page. + if ( isset( $_SERVER['QUERY_STRING'] ) ) + parse_str($_SERVER['QUERY_STRING'], $batcache->query); + $batcache->keys = array( + 'host' => $_SERVER['HTTP_HOST'], + 'method' => $_SERVER['REQUEST_METHOD'], + 'path' => ( $batcache->pos = strpos($_SERVER['REQUEST_URI'], '?') ) ? substr($_SERVER['REQUEST_URI'], 0, $batcache->pos) : $_SERVER['REQUEST_URI'], + 'query' => $batcache->query, + 'extra' => $batcache->unique + ); + + if ( $batcache->is_ssl() ) + $batcache->keys['ssl'] = true; + + $batcache->configure_groups(); + + // Generate the batcache key + $batcache->key = md5(serialize($batcache->keys)); + + // Generate the traffic threshold measurement key + $batcache->req_key = $batcache->key . '_req'; + + // Get the batcache + $batcache->cache = wp_cache_get($batcache->key, $batcache->group); + + // Are we only caching frequently-requested pages? + if ( $batcache->seconds < 1 || $batcache->times < 2 ) { + $batcache->do = true; + } else { + // No batcache item found, or ready to sample traffic again at the end of the batcache life? + if ( !is_array($batcache->cache) || time() >= $batcache->cache['time'] + $batcache->max_age - $batcache->seconds ) { + wp_cache_add($batcache->req_key, 0, $batcache->group); + $batcache->requests = wp_cache_incr($batcache->req_key, 1, $batcache->group); + + if ( $batcache->requests >= $batcache->times ) + $batcache->do = true; + else + $batcache->do = false; } - exit; } - // Issue "304 Not Modified" only if the dates match exactly. - if ( $batcache->cache_control && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ) { - $since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']); - if ( isset($batcache->cache['headers']['Last-Modified']) ) - $batcache->cache['time'] = strtotime( $batcache->cache['headers']['Last-Modified'] ); - if ( $batcache->cache['time'] == $since ) { - header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304); + // Recreate the permalink from the URL + $batcache->permalink = 'http://' . $batcache->keys['host'] . $batcache->keys['path'] . ( isset($batcache->keys['query']['p']) ? "?p=" . $batcache->keys['query']['p'] : '' ); + $batcache->url_key = md5($batcache->permalink); + $batcache->url_version = (int) wp_cache_get("{$batcache->url_key}_version", $batcache->group); + + // If the document has been updated and we are the first to notice, regenerate it. + if ( $batcache->do !== false && isset($batcache->cache['version']) && $batcache->cache['version'] < $batcache->url_version ) + $batcache->genlock = wp_cache_add("{$batcache->url_key}_genlock", 1, $batcache->group); + + // Did we find a batcached page that hasn't expired? + if ( isset($batcache->cache['time']) && ! $batcache->genlock && time() < $batcache->cache['time'] + $batcache->max_age ) { + // Issue redirect if cached and enabled + if ( $batcache->cache['redirect_status'] && $batcache->cache['redirect_location'] && $batcache->cache_redirects ) { + $status = $batcache->cache['redirect_status']; + $location = $batcache->cache['redirect_location']; + // From vars.php + $is_IIS = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer') !== false); + if ( $is_IIS ) { + header("Refresh: 0;url=$location"); + } else { + if ( php_sapi_name() != 'cgi-fcgi' ) { + $texts = array( + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 306 => 'Reserved', + 307 => 'Temporary Redirect', + ); + $protocol = $_SERVER["SERVER_PROTOCOL"]; + if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) + $protocol = 'HTTP/1.0'; + if ( isset($texts[$status]) ) + header("$protocol $status " . $texts[$status]); + else + header("$protocol 302 Found"); + } + header("Location: $location"); + } exit; } - } - // Use the batcache save time for Last-Modified so we can issue "304 Not Modified" - if ( $batcache->cache_control ) { - header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $batcache->cache['time'] ) . ' GMT', true ); - header('Cache-Control: max-age=' . ($batcache->max_age - time() + $batcache->cache['time']) . ', must-revalidate', true); - } + // Issue "304 Not Modified" only if the dates match exactly. + if ( $batcache->cache_control && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ) { + $since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']); + if ( isset($batcache->cache['headers']['Last-Modified']) ) + $batcache->cache['time'] = strtotime( $batcache->cache['headers']['Last-Modified'] ); + if ( $batcache->cache['time'] == $since ) { + header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304); + exit; + } + } - // Add some debug info just before - if ( $batcache->debug ) { - if ( false !== $tag_position = strpos($batcache->cache['output'], '') ) { - $tag = "\n"; - $batcache->cache['output'] = substr($batcache->cache['output'], 0, $tag_position) . $tag . substr($batcache->cache['output'], $tag_position); + // Use the batcache save time for Last-Modified so we can issue "304 Not Modified" + if ( $batcache->cache_control ) { + header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $batcache->cache['time'] ) . ' GMT', true ); + header('Cache-Control: max-age=' . ($batcache->max_age - time() + $batcache->cache['time']) . ', must-revalidate', true); } - } - if ( !empty($batcache->cache['headers']) ) foreach ( $batcache->cache['headers'] as $k => $v ) - header("$k: $v", true); + // Add some debug info just before + if ( $batcache->debug ) { + if ( false !== $tag_position = strpos($batcache->cache['output'], '') ) { + $tag = "\n"; + $batcache->cache['output'] = substr($batcache->cache['output'], 0, $tag_position) . $tag . substr($batcache->cache['output'], $tag_position); + } + } - if ( !empty($batcache->headers) ) foreach ( $batcache->headers as $k => $v ) { - if ( is_array( $v ) ) - header("{$v[0]}: {$v[1]}", false); - else + if ( !empty($batcache->cache['headers']) ) foreach ( $batcache->cache['headers'] as $k => $v ) header("$k: $v", true); - } - if ( !empty($batcache->cache['status_header']) ) - header($batcache->cache['status_header'], true); + if ( !empty($batcache->headers) ) foreach ( $batcache->headers as $k => $v ) { + if ( is_array( $v ) ) + header("{$v[0]}: {$v[1]}", false); + else + header("$k: $v", true); + } - // Have you ever heard a death rattle before? - die($batcache->cache['output']); -} + if ( !empty($batcache->cache['status_header']) ) + header($batcache->cache['status_header'], true); -// Didn't meet the minimum condition? -if ( !$batcache->do && !$batcache->genlock ) - return; + // Have you ever heard a death rattle before? + die($batcache->cache['output']); + } -$wp_filter['status_header'][10]['batcache'] = array( 'function' => array(&$batcache, 'status_header'), 'accepted_args' => 1 ); -$wp_filter['wp_redirect_status'][10]['batcache'] = array( 'function' => array(&$batcache, 'redirect_status'), 'accepted_args' => 2 ); + // Didn't meet the minimum condition? + if ( !$batcache->do && !$batcache->genlock ) + return; -ob_start(array(&$batcache, 'ob')); + $wp_filter['status_header'][10]['batcache'] = array( 'function' => array(&$batcache, 'status_header'), 'accepted_args' => 1 ); + $wp_filter['wp_redirect_status'][10]['batcache'] = array( 'function' => array(&$batcache, 'redirect_status'), 'accepted_args' => 2 ); -// It is safer to omit the final PHP closing tag. + ob_start(array(&$batcache, 'ob')); +} +// It is safer to omit the final PHP closing tag. diff --git a/public/content/object-cache.php b/public/content/object-cache.php index c4486c2c7..186334087 100644 --- a/public/content/object-cache.php +++ b/public/content/object-cache.php @@ -1,5 +1,4 @@ add($key, $data, $group, $expire); -} + return $wp_object_cache->add($key, $data, $group, $expire); + } -function wp_cache_incr($key, $n = 1, $group = '') { - global $wp_object_cache; + function wp_cache_incr($key, $n = 1, $group = '') { + global $wp_object_cache; - return $wp_object_cache->incr($key, $n, $group); -} + return $wp_object_cache->incr($key, $n, $group); + } -function wp_cache_decr($key, $n = 1, $group = '') { - global $wp_object_cache; + function wp_cache_decr($key, $n = 1, $group = '') { + global $wp_object_cache; - return $wp_object_cache->decr($key, $n, $group); -} + return $wp_object_cache->decr($key, $n, $group); + } -function wp_cache_close() { - global $wp_object_cache; + function wp_cache_close() { + global $wp_object_cache; - return $wp_object_cache->close(); -} + return $wp_object_cache->close(); + } -function wp_cache_delete($key, $group = '') { - global $wp_object_cache; + function wp_cache_delete($key, $group = '') { + global $wp_object_cache; - return $wp_object_cache->delete($key, $group); -} + return $wp_object_cache->delete($key, $group); + } -function wp_cache_flush() { - global $wp_object_cache; + function wp_cache_flush() { + global $wp_object_cache; - return $wp_object_cache->flush(); -} + return $wp_object_cache->flush(); + } -function wp_cache_get($key, $group = '', $force = false) { - global $wp_object_cache; + function wp_cache_get($key, $group = '', $force = false) { + global $wp_object_cache; - return $wp_object_cache->get($key, $group, $force); -} + return $wp_object_cache->get($key, $group, $force); + } -function wp_cache_init() { - global $wp_object_cache; + function wp_cache_init() { + global $wp_object_cache; - $wp_object_cache = new WP_Object_Cache(); -} + $wp_object_cache = new WP_Object_Cache(); + } -function wp_cache_replace($key, $data, $group = '', $expire = 0) { - global $wp_object_cache; + function wp_cache_replace($key, $data, $group = '', $expire = 0) { + global $wp_object_cache; - return $wp_object_cache->replace($key, $data, $group, $expire); -} + return $wp_object_cache->replace($key, $data, $group, $expire); + } -function wp_cache_set($key, $data, $group = '', $expire = 0) { - global $wp_object_cache; + function wp_cache_set($key, $data, $group = '', $expire = 0) { + global $wp_object_cache; - if ( defined('WP_INSTALLING') == false ) - return $wp_object_cache->set($key, $data, $group, $expire); - else - return $wp_object_cache->delete($key, $group); -} + if ( defined('WP_INSTALLING') == false ) + return $wp_object_cache->set($key, $data, $group, $expire); + else + return $wp_object_cache->delete($key, $group); + } -function wp_cache_add_global_groups( $groups ) { - global $wp_object_cache; + function wp_cache_add_global_groups( $groups ) { + global $wp_object_cache; - $wp_object_cache->add_global_groups($groups); -} + $wp_object_cache->add_global_groups($groups); + } -function wp_cache_add_non_persistent_groups( $groups ) { - global $wp_object_cache; + function wp_cache_add_non_persistent_groups( $groups ) { + global $wp_object_cache; - $wp_object_cache->add_non_persistent_groups($groups); -} + $wp_object_cache->add_non_persistent_groups($groups); + } -class WP_Object_Cache { - var $global_groups = array(); + class WP_Object_Cache { + var $global_groups = array(); - var $no_mc_groups = array(); + var $no_mc_groups = array(); - var $cache = array(); - var $mc = array(); - var $stats = array(); - var $group_ops = array(); + var $cache = array(); + var $mc = array(); + var $stats = array(); + var $group_ops = array(); - var $cache_enabled = true; - var $default_expiration = 0; + var $cache_enabled = true; + var $default_expiration = 0; - function add($id, $data, $group = 'default', $expire = 0) { - $key = $this->key($id, $group); + function add($id, $data, $group = 'default', $expire = 0) { + $key = $this->key($id, $group); - if ( is_object( $data ) ) - $data = clone $data; + if ( is_object( $data ) ) + $data = clone $data; - if ( in_array($group, $this->no_mc_groups) ) { - $this->cache[$key] = $data; - return true; - } elseif ( isset($this->cache[$key]) && $this->cache[$key] !== false ) { - return false; - } + if ( in_array($group, $this->no_mc_groups) ) { + $this->cache[$key] = $data; + return true; + } elseif ( isset($this->cache[$key]) && $this->cache[$key] !== false ) { + return false; + } - $mc =& $this->get_mc($group); - $expire = ($expire == 0) ? $this->default_expiration : $expire; - $result = $mc->add($key, $data, false, $expire); + $mc =& $this->get_mc($group); + $expire = ($expire == 0) ? $this->default_expiration : $expire; + $result = $mc->add($key, $data, false, $expire); - if ( false !== $result ) { - @ ++$this->stats['add']; - $this->group_ops[$group][] = "add $id"; - $this->cache[$key] = $data; + if ( false !== $result ) { + @ ++$this->stats['add']; + $this->group_ops[$group][] = "add $id"; + $this->cache[$key] = $data; + } + + return $result; } - return $result; - } + function add_global_groups($groups) { + if ( ! is_array($groups) ) + $groups = (array) $groups; - function add_global_groups($groups) { - if ( ! is_array($groups) ) - $groups = (array) $groups; + $this->global_groups = array_merge($this->global_groups, $groups); + $this->global_groups = array_unique($this->global_groups); + } - $this->global_groups = array_merge($this->global_groups, $groups); - $this->global_groups = array_unique($this->global_groups); - } + function add_non_persistent_groups($groups) { + if ( ! is_array($groups) ) + $groups = (array) $groups; - function add_non_persistent_groups($groups) { - if ( ! is_array($groups) ) - $groups = (array) $groups; + $this->no_mc_groups = array_merge($this->no_mc_groups, $groups); + $this->no_mc_groups = array_unique($this->no_mc_groups); + } - $this->no_mc_groups = array_merge($this->no_mc_groups, $groups); - $this->no_mc_groups = array_unique($this->no_mc_groups); - } + function incr($id, $n = 1, $group = 'default' ) { + $key = $this->key($id, $group); + $mc =& $this->get_mc($group); + $this->cache[ $key ] = $mc->increment( $key, $n ); + return $this->cache[ $key ]; + } - function incr($id, $n = 1, $group = 'default' ) { - $key = $this->key($id, $group); - $mc =& $this->get_mc($group); - $this->cache[ $key ] = $mc->increment( $key, $n ); - return $this->cache[ $key ]; - } + function decr($id, $n = 1, $group = 'default' ) { + $key = $this->key($id, $group); + $mc =& $this->get_mc($group); + $this->cache[ $key ] = $mc->decrement( $key, $n ); + return $this->cache[ $key ]; + } - function decr($id, $n = 1, $group = 'default' ) { - $key = $this->key($id, $group); - $mc =& $this->get_mc($group); - $this->cache[ $key ] = $mc->decrement( $key, $n ); - return $this->cache[ $key ]; - } + function close() { + // Not implemented in MemcacheSASL + } - function close() { - // Not implemented in MemcacheSASL - } + function delete($id, $group = 'default') { + $key = $this->key($id, $group); - function delete($id, $group = 'default') { - $key = $this->key($id, $group); + if ( in_array($group, $this->no_mc_groups) ) { + unset($this->cache[$key]); + return true; + } - if ( in_array($group, $this->no_mc_groups) ) { - unset($this->cache[$key]); - return true; - } + $mc =& $this->get_mc($group); - $mc =& $this->get_mc($group); + $result = $mc->delete($key); - $result = $mc->delete($key); + @ ++$this->stats['delete']; + $this->group_ops[$group][] = "delete $id"; - @ ++$this->stats['delete']; - $this->group_ops[$group][] = "delete $id"; + if ( false !== $result ) + unset($this->cache[$key]); - if ( false !== $result ) - unset($this->cache[$key]); + return $result; + } - return $result; - } + function flush() { + // Not implemented in MemcacheSASL + } - function flush() { - // Not implemented in MemcacheSASL - } + function get($id, $group = 'default', $force = false) { + $key = $this->key($id, $group); + $mc =& $this->get_mc($group); - function get($id, $group = 'default', $force = false) { - $key = $this->key($id, $group); - $mc =& $this->get_mc($group); + if ( isset($this->cache[$key]) && ( !$force || in_array($group, $this->no_mc_groups) ) ) { + if ( is_object( $this->cache[$key] ) ) + $value = clone $this->cache[$key]; + else + $value = $this->cache[$key]; + } else if ( in_array($group, $this->no_mc_groups) ) { + $this->cache[$key] = $value = false; + } else { + $value = $mc->get($key); + if ( NULL === $value ) + $value = false; + $this->cache[$key] = $value; + } - if ( isset($this->cache[$key]) && ( !$force || in_array($group, $this->no_mc_groups) ) ) { - if ( is_object( $this->cache[$key] ) ) - $value = clone $this->cache[$key]; - else - $value = $this->cache[$key]; - } else if ( in_array($group, $this->no_mc_groups) ) { - $this->cache[$key] = $value = false; - } else { - $value = $mc->get($key); - if ( NULL === $value ) - $value = false; - $this->cache[$key] = $value; - } + @ ++$this->stats['get']; + $this->group_ops[$group][] = "get $id"; - @ ++$this->stats['get']; - $this->group_ops[$group][] = "get $id"; + if ( 'checkthedatabaseplease' === $value ) { + unset( $this->cache[$key] ); + $value = false; + } - if ( 'checkthedatabaseplease' === $value ) { - unset( $this->cache[$key] ); - $value = false; + return $value; } - return $value; - } - - function get_multi( $groups ) { - /* - format: $get['group-name'] = array( 'key1', 'key2' ); - */ - $return = array(); - foreach ( $groups as $group => $ids ) { - $mc =& $this->get_mc($group); - foreach ( $ids as $id ) { - $key = $this->key($id, $group); - if ( isset($this->cache[$key]) ) { - if ( is_object( $this->cache[$key] ) ) - $return[$key] = clone $this->cache[$key]; - else - $return[$key] = $this->cache[$key]; - continue; - } else if ( in_array($group, $this->no_mc_groups) ) { - $return[$key] = false; - continue; - } else { - $return[$key] = $mc->get($key); + function get_multi( $groups ) { + /* + format: $get['group-name'] = array( 'key1', 'key2' ); + */ + $return = array(); + foreach ( $groups as $group => $ids ) { + $mc =& $this->get_mc($group); + foreach ( $ids as $id ) { + $key = $this->key($id, $group); + if ( isset($this->cache[$key]) ) { + if ( is_object( $this->cache[$key] ) ) + $return[$key] = clone $this->cache[$key]; + else + $return[$key] = $this->cache[$key]; + continue; + } else if ( in_array($group, $this->no_mc_groups) ) { + $return[$key] = false; + continue; + } else { + $return[$key] = $mc->get($key); + } + } + if ( $to_get ) { + $vals = $mc->get_multi( $to_get ); + $return = array_merge( $return, $vals ); } } - if ( $to_get ) { - $vals = $mc->get_multi( $to_get ); - $return = array_merge( $return, $vals ); - } + @ ++$this->stats['get_multi']; + $this->group_ops[$group][] = "get_multi $id"; + $this->cache = array_merge( $this->cache, $return ); + return $return; } - @ ++$this->stats['get_multi']; - $this->group_ops[$group][] = "get_multi $id"; - $this->cache = array_merge( $this->cache, $return ); - return $return; - } - function key($key, $group) { - if ( empty($group) ) - $group = 'default'; + function key($key, $group) { + if ( empty($group) ) + $group = 'default'; - if ( false !== array_search($group, $this->global_groups) ) - $prefix = $this->global_prefix; - else - $prefix = $this->blog_prefix; - - return preg_replace('/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key"); - } + if ( false !== array_search($group, $this->global_groups) ) + $prefix = $this->global_prefix; + else + $prefix = $this->blog_prefix; - function replace($id, $data, $group = 'default', $expire = 0) { - $key = $this->key($id, $group); - $expire = ($expire == 0) ? $this->default_expiration : $expire; - $mc =& $this->get_mc($group); + return preg_replace('/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key"); + } - if ( is_object( $data ) ) - $data = clone $data; + function replace($id, $data, $group = 'default', $expire = 0) { + $key = $this->key($id, $group); + $expire = ($expire == 0) ? $this->default_expiration : $expire; + $mc =& $this->get_mc($group); - $result = $mc->replace($key, $data, false, $expire); - if ( false !== $result ) - $this->cache[$key] = $data; - return $result; - } + if ( is_object( $data ) ) + $data = clone $data; - function set($id, $data, $group = 'default', $expire = 0) { - $key = $this->key($id, $group); - if ( isset($this->cache[$key]) && ('checkthedatabaseplease' === $this->cache[$key]) ) - return false; + $result = $mc->replace($key, $data, false, $expire); + if ( false !== $result ) + $this->cache[$key] = $data; + return $result; + } - if ( is_object($data) ) - $data = clone $data; + function set($id, $data, $group = 'default', $expire = 0) { + $key = $this->key($id, $group); + if ( isset($this->cache[$key]) && ('checkthedatabaseplease' === $this->cache[$key]) ) + return false; - $this->cache[$key] = $data; + if ( is_object($data) ) + $data = clone $data; - if ( in_array($group, $this->no_mc_groups) ) - return true; + $this->cache[$key] = $data; - $expire = ($expire == 0) ? $this->default_expiration : $expire; - $mc =& $this->get_mc($group); - $result = $mc->set($key, $data, false, $expire); + if ( in_array($group, $this->no_mc_groups) ) + return true; - return $result; - } + $expire = ($expire == 0) ? $this->default_expiration : $expire; + $mc =& $this->get_mc($group); + $result = $mc->set($key, $data, false, $expire); - function colorize_debug_line($line) { - $colors = array( - 'get' => 'green', - 'set' => 'purple', - 'add' => 'blue', - 'delete' => 'red'); + return $result; + } - $cmd = substr($line, 0, strpos($line, ' ')); + function colorize_debug_line($line) { + $colors = array( + 'get' => 'green', + 'set' => 'purple', + 'add' => 'blue', + 'delete' => 'red'); - $cmd2 = "$cmd"; + $cmd = substr($line, 0, strpos($line, ' ')); - return $cmd2 . substr($line, strlen($cmd)) . "\n"; - } + $cmd2 = "$cmd"; - function stats() { - echo "

\n"; - foreach ( $this->stats as $stat => $n ) { - echo "$stat $n"; - echo "
\n"; + return $cmd2 . substr($line, strlen($cmd)) . "\n"; } - echo "

\n"; - echo "

Memcachier:

"; - foreach ( $this->group_ops as $group => $ops ) { - if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) { - $ops = array_slice( $ops, 0, 500 ); - echo "Too many to show! Show them anyway.\n"; - } - echo "

$group commands

"; - echo "
\n";
-			$lines = array();
-			foreach ( $ops as $op ) {
-				$lines[] = $this->colorize_debug_line($op); 
+
+		function stats() {
+			echo "

\n"; + foreach ( $this->stats as $stat => $n ) { + echo "$stat $n"; + echo "
\n"; + } + echo "

\n"; + echo "

Memcachier:

"; + foreach ( $this->group_ops as $group => $ops ) { + if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) { + $ops = array_slice( $ops, 0, 500 ); + echo "Too many to show! Show them anyway.\n"; + } + echo "

$group commands

"; + echo "
\n";
+				$lines = array();
+				foreach ( $ops as $op ) {
+					$lines[] = $this->colorize_debug_line($op); 
+				}
+				print_r($lines);
+				echo "
\n"; } - print_r($lines); - echo "
\n"; - } - if ( $this->debug ) - var_dump($this->memcache_debug); - } + if ( $this->debug ) + var_dump($this->memcache_debug); + } - function &get_mc($group) { - if ( isset($this->mc[$group]) ) - return $this->mc[$group]; - return $this->mc['default']; - } + function &get_mc($group) { + if ( isset($this->mc[$group]) ) + return $this->mc[$group]; + return $this->mc['default']; + } - function failure_callback($host, $port) { - //error_log("Connection failure for $host:$port\n", 3, '/tmp/memcached.txt'); - } + function failure_callback($host, $port) { + //error_log("Connection failure for $host:$port\n", 3, '/tmp/memcached.txt'); + } - function WP_Object_Cache() { + function WP_Object_Cache() { + + include_once('plugins/memcachier/MemcacheSASL.php'); - include_once('plugins/memcachier/MemcacheSASL.php'); - - global $memcachier_servers; + global $memcachier_servers; - if ( isset($memcachier_servers) ) - $buckets = $memcachier_servers; - else - $buckets = array( - 'default' => array( - getenv("MEMCACHIER_SERVERS") - ) - ); - - reset($buckets); - - foreach ( $buckets as $bucket => $servers) { - $this->mc[$bucket] = new MemcacheSASL; - foreach ( $servers as $server ) { - list ( $node, $port ) = explode(':', $server); - - $this->mc[$bucket]->addServer($node, $port); - $this->mc[$bucket]->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD")); + if ( isset($memcachier_servers) ) + $buckets = $memcachier_servers; + else + $buckets = array( + 'default' => array( + getenv("MEMCACHIER_SERVERS") + ) + ); + + reset($buckets); + + foreach ( $buckets as $bucket => $servers) { + $this->mc[$bucket] = new MemcacheSASL; + foreach ( $servers as $server ) { + list ( $node, $port ) = explode(':', $server); + + $this->mc[$bucket]->addServer($node, $port); + $this->mc[$bucket]->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD")); + } + } + + global $blog_id, $table_prefix; + $this->global_prefix = ''; + $this->blog_prefix = ''; + if ( function_exists( 'is_multisite' ) ) { + $this->global_prefix = ( is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ) ? '' : $table_prefix; + $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; } - } - - global $blog_id, $table_prefix; - $this->global_prefix = ''; - $this->blog_prefix = ''; - if ( function_exists( 'is_multisite' ) ) { - $this->global_prefix = ( is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ) ? '' : $table_prefix; - $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; - } - $this->cache_hits =& $this->stats['get']; - $this->cache_misses =& $this->stats['add']; + $this->cache_hits =& $this->stats['get']; + $this->cache_misses =& $this->stats['add']; + } } } ?> diff --git a/public/testDB.php b/public/testDB.php new file mode 100644 index 000000000..5c5bc1893 --- /dev/null +++ b/public/testDB.php @@ -0,0 +1,8 @@ + \ No newline at end of file