wordpress自带的缓存功能使用介绍
框架(架构)  /  管理员 发布于 7年前   266
用静态化当然可以解决这些问题,不过对于流量不大的博客就没必要了。 wordpress自带有缓存体系,关键的函数你可以在wp-includes/cache.php或 Codex里查找到所有的函数。
你可以使用下列函数来实现添加缓存。
复制代码代码如下:
wp_cache_add($key, $data, $flag = '', $expire = 0)
相反的,要删除缓存数据可以通过下面的方法实现。
复制代码代码如下:
wp_cache_delete($id, $flag = '')
提取缓存数据:
复制代码代码如下:
wp_cache_get($id, $flag = '')
替换缓存数据:
复制代码代码如下:
wp_cache_replace($key, $data, $flag = '', $expire = 0)
 
举个例子,如果想输出最新评论,可以先从缓存里读取需要输出的HTML,如果没有,请求一次,成功后加入缓存,当然时间可以控制。
我知道有很多最新评论,相关文章免插件输出的方法,原理都一样,可以自行改改,贴个我用的:
config.php里面添加
复制代码代码如下:
define('WP_CACHE', true);
如果你用了其它缓存插件,基本上这个就没效果了。因为这些插件有类似的方法。
复制代码代码如下:
<?php
/**
 * get recent comments
 * @param unknown_type $args
 * @return unknown_type
 */
function DeamworkRecentcomments($args='number=5&status=approve'){</p><p> $cacheID = md5($args);
    //有缓存就直接输出
 if($output = wp_cache_get('recentComments_'.$cacheID, 'Winysky')){
  echo $output;
  return;
 }</p><p> //$rcms = get_comments($args);
global $wpdb;</p><p> $my_email = "'" . get_bloginfo ('admin_email') . "'";
//自动获取博主邮箱
$rcms = $wpdb->get_results("
SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_content
FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts
ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1'
AND comment_type = ''
AND post_password = ''
AND comment_author_email != $my_email</p><p>ORDER BY comment_date_gmt
DESC LIMIT 10</p><p>");</p><p> //print_r($rcms);return;
 if(empty($rcms)){
  _e('没有数据');
  return;
 }
 //历遍数据
 $output = '';
 foreach( $rcms as $rcm ){
   $author = $rcm->comment_author;
   //if($author =='admin'){continue;}
        $content = DeamworkStriptags( $rcm->comment_content);
  $the_title = get_the_title($rcm->comment_post_ID);
  $s_excerpt = convert_smilies( DeamworkSubstr( $content, 200 ) );
  $contents = '<span class=\'recentcommentslink_author\'>' . $author . '</span> <span class=\'recentcommentslink_on\'>on</span> <span class=\'recentcommentslink_title\'>' . $the_title . '</span><div class=\'recentcommentslink_excerpt\'>' . get_avatar($rcm->comment_author_email, 64). $s_excerpt . '</div>';</p><p>   $output .=  '<li><div id="C_' . $rcm->comment_ID . '_d" style="display:none">' . $contents . '</div>' . '<a id="C_' . $rcm->comment_ID . '" class="recentcommentslink" href="https:/cms/' . get_permalink($rcm->comment_post_ID) . '#comment-' . $rcm->comment_ID . '" >' . get_avatar($rcm->comment_author_email, 32) . '</a></li>';</p><p> }
 //输出后加入缓存
 wp_cache_add('recentComments_'.$cacheID,$output, 'Deamwork');
 echo $output;
}
最后,试试效果吧~
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
			Copyright·© 2019 侯体宗版权所有·
			粤ICP备20027696号
			
			
