为WP-Super-Cache添加清除Memcache功能

MrLee2015-11-7 2354

如果你的站点添加了缓存功能,那么当你新发表文章或者有新评论的时候,你就会发现页面根本不会刷新,这是为啥呢?我们再复习下什么是Memcache。

MemCache

MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。
说简单点,是因为它缓存了数据,因为缓存中已经存在所以不会去数据库查询。那么我们要做的就是清理缓存。这里拿wordpress的wp-supser-cache插件举例。
在插件目录找到wp-cache.php文件,然后在大概2691行左右把按钮文字再加一个标识 &Memcache,这样好辨别嘛。
echo '
';

我还是把整个函数贴出来,怕大家改错地方。
function wp_cache_delete_buttons() {
	echo '
'; echo ''; echo '
'; wp_nonce_field('wp-cache'); echo "
\n"; echo '
'; echo ''; echo '
'; wp_nonce_field('wp-cache'); echo "
\n"; if ( ( defined( 'VHOST' ) || ( defined( 'WP_ALLOW_MULTISITE' ) && constant( 'WP_ALLOW_MULTISITE' ) == true ) ) && wpsupercache_site_admin() ) { echo '
'; echo ''; echo '
'; wp_nonce_field('wp-cache'); echo "\n"; } }

然后在处理提交函数添加memcache操作代码,这里有一个前提,是已经安装了object-cache.php,因为这里面定义了全局的memcache操作变量。然后在该wp_cache_files函数中添加以下代码。
global $wp_object_cache;
			$wp_object_cache->flush();

最终效果图:

20151107154522


至此结束!
不过有时候不想删除静态的html文件只想更新缓存,我就把wp-supercache稍微改了下,把测试缓存的改成了删除memcache,这个测试感觉没啥用。至少我没用过。修改过后代码。大约在1162行左右。
				if ( array_key_exists('action', $_POST) && $_POST[ 'action' ] == 'test' && $valid_nonce ) {
					//清空MemCache
					global $wp_object_cache;
					$wp_object_cache->flush();
					echo 'Memcached成功清除';
					/*
					$url = trailingslashit( get_bloginfo( 'url' ) );
					if ( isset( $_POST[ 'httponly' ] ) )
						$url = str_replace( 'https://', 'http://', $url );
					// Prime the cache
					echo "
" . sprintf(  __( 'Fetching %s to prime cache: ', 'wp-super-cache' ), $url );
					$page = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
					echo '' . __( 'OK', 'wp-super-cache' ) . '
';
					sleep( 1 );
					// Get the first copy
					echo "
" . sprintf(  __( 'Fetching first copy of %s: ', 'wp-super-cache' ), $url );
					$page = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
					if ( !is_wp_error( $page ) ) {
						$fp = fopen( $cache_path . "1.html", "w" );
						fwrite( $fp, $page[ 'body' ] );
						fclose( $fp );
						echo '' . __( 'OK', 'wp-super-cache' ) . " (1.html)
";
						sleep( 1 );
					} else {
						echo '' . __( 'FAILED', 'wp-super-cache' ) . "
";
					}
					// Get the second copy
					echo "
" . sprintf(  __( 'Fetching second copy of %s: ', 'wp-super-cache' ), $url );
					$page2 = wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
					if ( !is_wp_error( $page2 ) ) {
						$fp = fopen( $cache_path . "2.html", "w" );
						fwrite( $fp, $page2[ 'body' ] );
						fclose( $fp );
						echo '' . __( 'OK', 'wp-super-cache' ) . " (2.html)
";
					} else {
						echo '' . __( 'FAILED', 'wp-super-cache' ) . "
";
					}
					if ( is_wp_error( $page ) || is_wp_error( $page2 ) || $page[ 'response' ][ 'code' ] != 200 || $page2[ 'response' ][ 'code' ] != 200 ) {
						echo '
' . __( 'One or more page requests failed:', 'wp-super-cache' ) . '
';
						$error = false;
						if ( is_wp_error( $page ) ) {
							$error = $page;
						} elseif ( is_wp_error( $page2 ) ) {
							$error = $page2;
						}
						if ( $error ) {
							$errors = '';
							$messages = '';
							foreach ( $error->get_error_codes() as $code ) {
								$severity = $error->get_error_data($code);
								foreach ( $error->get_error_messages( $code ) as $err ) {
									$errors .= '	' . $err . "
\n";
								}
							}
							if ( !empty($err) )
								echo '
' . $errors . "
\n"; } else { echo '
  • ' . sprintf( __( 'Page %d: %d (%s)', 'wp-super-cache' ), 1, $page[ 'response' ][ 'code' ], $page[ 'response' ][ 'message' ] ) . '
  • '; echo '
  • ' . sprintf( __( 'Page %d: %d (%s)', 'wp-super-cache' ), 2, $page2[ 'response' ][ 'code' ], $page2[ 'response' ][ 'message' ] ) . '
'; } } if ( ( !is_wp_error( $page ) && !is_wp_error( $page2 ) ) && preg_match( '/(Cached page generated by WP-Super-Cache on) ([0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*)/', $page[ 'body' ], $matches1 ) && preg_match( '/(Cached page generated by WP-Super-Cache on) ([0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*)/', $page2[ 'body' ], $matches2 ) && $matches1[2] == $matches2[2] ) { echo ' ' . sprintf( __( 'Page 1: %s', 'wp-super-cache' ), $matches1[ 2 ] ) . ' '; echo ' ' . sprintf( __( 'Page 2: %s', 'wp-super-cache' ), $matches2[ 2 ] ) . ' '; echo ' ' . __( 'The timestamps on both pages match!', 'wp-super-cache' ) . ' '; } else { echo ' ' . __( 'The pages do not match! Timestamps differ or were not found!', 'wp-super-cache' ) . ' '; echo ' ' . __( 'Things you can do:', 'wp-super-cache' ) . ' '; echo '
  1. ' . __( 'Load your homepage in a logged out browser, check the timestamp at the end of the html source. Load the page again and compare the timestamp. Caching is working if the timestamps match.', 'wp-super-cache' ) . '
  2. '; echo '
  3. ' . __( 'Enable logging on the Debug page here. That should help you track down the problem.', 'wp-super-cache' ) . '
  4. '; echo '
  5. ' . __( 'You should check Page 1 and Page 2 above for errors. Your local server configuration may not allow your website to access itself.', 'wp-super-cache' ) . '
  6. '; echo "
"; } */ } echo '
'; echo ''; if ( isset( $_SERVER['HTTPS' ] ) && 'on' == strtolower( $_SERVER['HTTPS' ] ) ) echo " " . __( 'Send non-secure (non https) request for homepage', 'wp-super-cache' ); echo '
'; wp_nonce_field('wp-cache'); echo '
';

 

本文链接:https://www.it72.com/6820.htm

推荐阅读
最新回复 (0)
    返回