经常看到有童鞋抱怨 WordPress 太耗资源了,其实 WordPress 作为世界上最流行的 php+mysql 建站系统,性能很不错的。那么为什么还有一大堆童鞋抱怨 WordPress 太占用服务器资源呢?其实问题就出在 mysql 数据库身上,由于 WordPress 大部分功能强大的插件和主题模板都有很多数据需要储存在数据库中,而且由于一些主题开发者只追求功能强大而没有考虑到网站运行的性能能问题,没有一个良好的数据库缓存方案,所以导致了 WordPress 的臃肿。其实 WordPress 内部有着很好的缓存机制 (Transients_API) 我们在开发主题时只需要调用 WordPress 缓存机制即可。下面我举几个例子。
缓存 WordPress 的菜单(menu):PS 代码加入到 functions.php
<?php function hh_cached_nav_menu( $args = array(), $prime_cache = false ) { global $wp_query; $queried_object_id = empty( $wp_query->queried_object_id ) ? 0 : (int) $wp_query->queried_object_id; $queried_object_id ); $nav_menu_key = md5( serialize( $args ) ); $my_args = wp_parse_args( $args ); $my_args = apply_filters( 'wp_nav_menu_args', $my_args ); $my_args = (object) $my_args; if ( ( isset( $my_args->echo ) && true === $my_args->echo ) || !isset( $my_args->echo ) ) { $echo = true; } else { $echo = false; } $skip_cache = false; $use_cache = ( true === $prime_cache ) ? false : true; if ( is_singular() ) { $skip_cache = true; } else if ( !in_array( $queried_object_id, hh_get_nav_menu_cache_objects( $use_cache ) ) ) { $skip_cache = true; } if ( true === $skip_cache || true === $prime_cache || false === ( $nav_menu = get_transient( $nav_menu_key ) ) ) { if ( false === $echo ) { $nav_menu = wp_nav_menu( $args ); } else { ob_start(); wp_nav_menu( $args ); $nav_menu = ob_get_clean(); } if ( false === $skip_cache ) set_transient( $nav_menu_key, $nav_menu ); } if ( true === $echo ) echo $nav_menu; else return $nav_menu; } function hh_update_nav_menu_objects( $menu_id = null, $menu_data = null ) { hh_cached_nav_menu( array( 'echo' => false ), $prime_cache = true ); } add_action( 'wp_update_nav_menu', 'hh_update_nav_menu_objects' ); function hh_get_nav_menu_cache_objects( $use_cache = true ) { $object_ids = get_transient( 'hh_nav_menu_cache_object_ids' ); if ( true === $use_cache && !empty( $object_ids ) ) { return $object_ids; } $object_ids = $objects = array(); $menus = wp_get_nav_menus(); foreach ( $menus as $menu_maybe ) { if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id ) ) { foreach( $menu_items as $menu_item ) { if ( preg_match( "#.*/category/([^/]+)/?$#", $menu_item->url, $match ) ) $objects['category'][] = $match[1]; if ( preg_match( "#.*/tag/([^/]+)/?$#", $menu_item->url, $match ) ) $objects['post_tag'][] = $match[1]; } } } if ( !empty( $objects ) ) { foreach( $objects as $taxonomy => $term_names ) { foreach( $term_names as $term_name ) { $term = get_term_by( 'slug', $term_name, $taxonomy ); if ( $term ) $object_ids[] = $term->term_id; } } } $object_ids[] = 0; set_transient( 'hh_nav_menu_cache_object_ids', $object_ids ); return $object_ids; }
缓存标签云(Tag cloud):
$tag_cloud = get_transient( 'tag_cloud' ); if ( false === $tag_cloud || '' === $tag_cloud ){ $args = array('echo' => false); $tag_cloud = wp_tag_cloud( $args ); set_transient( 'tag_cloud', $tag_cloud, 60*60*12 ); } echo $tag_cloud;
缓存自定义查询:
<?php if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' ); set_transient( 'special_query_results', $special_query_results ); } ?>