xcache comments invalidation 1.4.0
Peter Molnar hello@petermolnar.eu
Mon, 12 May 2014 15:48:22 +0100
4 files changed,
152 insertions(+),
9 deletions(-)
M
readme.txt
→
readme.txt
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC
Tags: cache, page cache, full page cache, nginx, memcached, apc, speed Requires at least: 3.0 Tested up to: 3.9 -Stable tag: 1.3.3 +Stable tag: 1.4.0 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html@@ -72,6 +72,7 @@
* PHP APC * PHP Memcached > 0.1.0 * PHP Memcache > 2.1.0 +* Xcache = How logging works in the plugin? = Log levels by default ( if logging enabled ) includes warning and error level standard PHP messages.@@ -93,6 +94,14 @@ 4. memcached servers settings
5. NGiNX example == Changelog == + += 1.4.0 = +*2014-05-12* + +What's changed: + +* Xcache backend added ( alpha version, still under heavy testing ) +* invalidation for comment actions added = 1.3.3 = *2014-04-29*
M
wp-ffpc-backend.php
→
wp-ffpc-backend.php
@@ -181,6 +181,7 @@ * public get function, transparent proxy to internal function based on backend
* * @param string $post_id ID of post to invalidate * @param boolean $force Force flush cache + * @param boolean $comment Clear a single page based on comment trigger * */ public function clear ( $post_id = false, $force = false ) {@@ -196,7 +197,7 @@ return false;
} /* if invalidation method is set to full, flush cache */ - if ( $this->options['invalidation_method'] === 0 || $force === true ) { + if ( ( $this->options['invalidation_method'] === 0 || $force === true ) ) { /* log action */ $this->log ( __translate__('flushing cache', $this->plugin_constant ) );@@ -261,6 +262,25 @@
/* run clear */ $internal = $this->proxy ( 'clear' ); $this->$internal ( $to_clear ); + } + + /** + * clear cache triggered by new comment + * + * @param $comment_id Comment ID + * @param $comment_object The whole comment object ? + */ + public function clear_by_comment ( $comment_id, $comment_object ) { + if ( empty( $comment_id ) ) + return false; + + $comment = get_comment( $comment_id ); + $post_id = $comment->comment_post_ID; + if ( !empty( $post_id ) ) + $this->clear ( $post_id ); + + unset ( $comment ); + unset ( $post_id ); } /**@@ -508,6 +528,98 @@ }
} /*********************** END APC FUNCTIONS ***********************/ + + /*********************** XCACHE FUNCTIONS ***********************/ + /** + * init apc backend: test APC availability and set alive status + */ + private function xcache_init () { + /* verify apc functions exist, apc extension is loaded */ + if ( ! function_exists( 'xcache_info' ) ) { + $this->log ( __translate__('XCACHE extension missing', $this->plugin_constant ) ); + return false; + } + + /* verify apc is working */ + $info = xcache_info(); + if ( !empty( $info )) { + $this->log ( __translate__('backend OK', $this->plugin_constant ) ); + $this->alive = true; + } + } + + /** + * health checker for Xcache + * + * @return boolean Aliveness status + * + */ + private function xcache_status () { + $this->status = true; + return $this->alive; + } + + /** + * get function for APC backend + * + * @param string $key Key to get values for + * + * @return mixed Fetched data based on key + * + */ + private function xcache_get ( &$key ) { + if ( xcache_isset ( $key ) ) + return xcache_get( $key ); + else + return false; + } + + /** + * Set function for APC backend + * + * @param string $key Key to set with + * @param mixed $data Data to set + * + * @return boolean APC store outcome + */ + private function xcache_set ( &$key, &$data ) { + return xcache_set( $key , $data , $this->options['expire'] ); + } + + + /** + * Flushes APC user entry storage + * + * @return boolean APC flush outcome status + * + */ + private function xcache_flush ( ) { + return xcache_clear_cache(XC_TYPE_VAR); + } + + /** + * Removes entry from APC or flushes APC user entry storage + * + * @param mixed $keys Keys to clear, string or array + */ + private function xcache_clear ( &$keys ) { + /* make an array if only one string is present, easier processing */ + if ( !is_array ( $keys ) ) + $keys = array ( $keys => true ); + + foreach ( $keys as $key => $dummy ) { + if ( ! xcache_unset ( $key ) ) { + $this->log ( sprintf( __translate__( 'Failed to delete XCACHE entry: %s', $this->plugin_constant ), $key ), LOG_ERR ); + //throw new Exception ( __translate__('Deleting APC entry failed with key ', $this->plugin_constant ) . $key ); + } + else { + $this->log ( sprintf( __translate__( 'XCACHE entry delete: %s', $this->plugin_constant ), $key ) ); + } + } + } + + /*********************** END XCACHE FUNCTIONS ***********************/ + /*********************** MEMCACHED FUNCTIONS ***********************/ /**
M
wp-ffpc-class.php
→
wp-ffpc-class.php
@@ -115,12 +115,14 @@
/* cache type possible values array */ $this->select_cache_type = array ( 'apc' => __( 'APC' , $this->plugin_constant ), + 'xcache' => __( 'XCache' , $this->plugin_constant ), 'memcache' => __( 'PHP Memcache' , $this->plugin_constant ), 'memcached' => __( 'PHP Memcached' , $this->plugin_constant ), ); /* check for required functions / classes for the cache types */ $this->valid_cache_type = array ( 'apc' => function_exists( 'apc_sma_info' ) ? true : false, + 'xcache' => function_exists( 'xcache_info' ) ? true : false, 'memcache' => class_exists ( 'Memcache') ? true : false, 'memcached' => class_exists ( 'Memcached') ? true : false, );@@ -177,6 +179,17 @@ add_action( 'private_to_publish' .$post_type , array( &$this->backend , 'clear' ), 0 );
add_action( 'publish_' . $post_type , array( &$this->backend , 'clear' ), 0 ); } + /* comments invalidation hooks */ + if ( $this->options['comments_invalidate'] ) { + add_action( 'comment_post', array( &$this->backend , 'clear' ), 0 ); + add_action( 'edit_comment', array( &$this->backend , 'clear' ), 0 ); + add_action( 'trashed_comment', array( &$this->backend , 'clear' ), 0 ); + add_action( 'pingback_post', array( &$this->backend , 'clear' ), 0 ); + add_action( 'trackback_post', array( &$this->backend , 'clear' ), 0 ); + add_action( 'wp_insert_comment', array( &$this->backend , 'clear' ), 0 ); + add_action( '', array( &$this->backend , 'clear' ), 0 ); + } + /* invalidation on some other ocasions as well */ add_action( 'switch_theme', array( &$this->backend , 'clear' ), 0 ); add_action( 'deleted_post', array( &$this->backend , 'clear' ), 0 );@@ -208,11 +221,11 @@
if ( file_exists ( $this->acache ) && ! is_writable ( $this->acache ) ) $this->errors['no_acache_write'] = __("Advanced cache file is not writeable!<br />Please change the permissions on the file: ", $this->plugin_constant) . $this->acache; - if ( $this->options['cache_type'] == 'memcached' && !class_exists('Memcached') ) - $this->errors['no_memcached'] = __('Memcached cache backend activated but no PHP memcached extension was found.<br />Please either use different backend or activate the module!', $this->plugin_constant); - - if ( $this->options['cache_type'] == 'memcache' && !class_exists('Memcache') ) - $this->errors['no_memcached'] = __('Memcache cache backend activated but no PHP memcache extension was found.<br />Please either use different backend or activate the module!', $this->plugin_constant); + foreach ( $this->valid_cache_type as $backend => $status ) { + if ( $this->options['cache_type'] == $backend && ! $status ) { + $this->errors['no_backend'] = sprintf ( __('%s cache backend activated but no PHP %s extension was found.<br />Please either use different backend or activate the module!', $this->plugin_constant), $backend, $backend ); + } + } /* get the current runtime configuration for memcache in PHP because Memcache in binary mode is really problematic */ if ( extension_loaded ( 'memcache' ) ) {@@ -464,6 +477,14 @@ <select name="invalidation_method" id="invalidation_method">
<?php $this->print_select_options ( $this->select_invalidation_method , $this->options['invalidation_method'] ) ?> </select> <span class="description"><?php _e('Select cache invalidation method. <ol><li><em>flush cache</em> - clears everything in storage, <strong>including values set by other applications</strong></li><li><em>only modified post</em> - clear only the modified posts entry, everything else remains in cache</li><li><em>modified post and all taxonomies</em> - removes all taxonomy term cache ( categories, tags, home, etc ) and the modified post as well</li></ol>', $this->plugin_constant); ?></span> + </dd> + + <dt> + <label for="comments_invalidate"><?php _e('Invalidate on comment actions', $this->plugin_constant); ?></label> + </dt> + <dd> + <input type="checkbox" name="comments_invalidate" id="comments_invalidate" value="1" <?php checked($this->options['comments_invalidate'],true); ?> /> + <span class="description"><?php _e('Trigger cache invalidation when a comments is posted, edited, trashed. ', $this->plugin_constant); ?></span> </dd> <dt>
M
wp-ffpc.php
→
wp-ffpc.php
@@ -3,7 +3,7 @@ /*
Plugin Name: WP-FFPC Plugin URI: http://petermolnar.eu/wordpress/wp-ffpc Description: WordPress cache plugin for memcached & nginx - unbeatable speed -Version: 1.3.2 +Version: 1.4.0 Author: Peter Molnar <hello@petermolnar.eu> Author URI: http://petermolnar.eu/ License: GPLv3@@ -48,8 +48,9 @@ 'response_header' => false,
'generate_time' => false, 'precache_schedule' => 'null', 'key' => '$scheme://$host$request_uri', + 'comments_invalidate' => true, ); -$wp_ffpc = new WP_FFPC ( 'wp-ffpc', '1.3.3', 'WP-FFPC', $wp_ffpc_defaults, 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC' ); +$wp_ffpc = new WP_FFPC ( 'wp-ffpc', '1.4.0', 'WP-FFPC', $wp_ffpc_defaults, 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC' ); ?>