吴振吉 發表於 2023-10-17 00:00:00

wordpress自带的缓存功能使用介绍

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
用静态化当然可以解决这些问题,不过对于流量不大的博客就没必要了。 wordpress自带有缓存体系,关键的函数你可以在wp-includes/cache.php或 Codex里查找到所有的函数。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
你可以使用下列函数来实现添加缓存。<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode6" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
wp_cache_add($key, $data, $flag = '', $expire = 0)</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>相反的,要删除缓存数据可以通过下面的方法实现。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
wp_cache_delete($id, $flag = '')</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>提取缓存数据:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
wp_cache_get($id, $flag = '')</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>替换缓存数据:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
wp_cache_replace($key, $data, $flag = '', $expire = 0)</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>举个例子,如果想输出最新评论,可以先从缓存里读取需要输出的HTML,如果没有,请求一次,成功后加入缓存,当然时间可以控制。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>我知道有很多最新评论,相关文章免插件输出的方法,原理都一样,可以自行改改,贴个我用的:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>config.php里面添加</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode10" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
define('WP_CACHE', true);</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>如果你用了其它缓存插件,基本上这个就没效果了。因为这些插件有类似的方法。</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode11" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php<br>
/**<br>
* get recent comments<br>
* @param unknown_type $args<br>
* @return unknown_type<br>
*/<br>
function DeamworkRecentcomments($args='number=5&amp;status=approve'){&lt;/p&gt; &lt;p&gt; $cacheID = md5($args);<br>
//有缓存就直接输出<br>
if($output = wp_cache_get('recentComments_'.$cacheID, 'Winysky')){<br>
echo $output;<br>
return;<br>
}&lt;/p&gt; &lt;p&gt; //$rcms = get_comments($args);<br>
global $wpdb;&lt;/p&gt; &lt;p&gt; $my_email = "'" . get_bloginfo ('admin_email') . "'";<br>
//自动获取博主邮箱<br>
$rcms = $wpdb-&gt;get_results("<br>
SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_content<br>
FROM $wpdb-&gt;comments LEFT OUTER JOIN $wpdb-&gt;posts<br>
ON ($wpdb-&gt;comments.comment_post_ID = $wpdb-&gt;posts.ID)<br>
WHERE comment_approved = '1'<br>
AND comment_type = ''<br>
AND post_password = ''<br>
AND comment_author_email != $my_email&lt;/p&gt; &lt;p&gt;ORDER BY comment_date_gmt<br>
DESC LIMIT 10&lt;/p&gt; &lt;p&gt;");&lt;/p&gt; &lt;p&gt; //print_r($rcms);return;<br>
if(empty($rcms)){<br>
_e('没有数据');<br>
return;<br>
}<br>
//历遍数据<br>
$output = '';<br>
foreach( $rcms as $rcm ){<br>
$author = $rcm-&gt;comment_author;<br>
//if($author =='admin'){continue;}<br>
$content = DeamworkStriptags( $rcm-&gt;comment_content);<br>
$the_title = get_the_title($rcm-&gt;comment_post_ID);<br>
$s_excerpt = convert_smilies( DeamworkSubstr( $content, 200 ) );<br>
$contents = '&lt;span class=\'recentcommentslink_author\'&gt;' . $author . '&lt;/span&gt; &lt;span class=\'recentcommentslink_on\'&gt;on&lt;/span&gt; &lt;span class=\'recentcommentslink_title\'&gt;' . $the_title . '&lt;/span&gt;&lt;div class=\'recentcommentslink_excerpt\'&gt;' . get_avatar($rcm-&gt;comment_author_email, 64). $s_excerpt . '&lt;/div&gt;';&lt;/p&gt; &lt;p&gt; $output .= '&lt;li&gt;&lt;div id="C_' . $rcm-&gt;comment_ID . '_d" style="display:none"&gt;' . $contents . '&lt;/div&gt;' . '&lt;a id="C_' . $rcm-&gt;comment_ID . '" href="' . get_permalink($rcm-&gt;comment_post_ID) . '#comment-' . $rcm-&gt;comment_ID . '" &gt;' . get_avatar($rcm-&gt;comment_author_email, 32) . '&lt;/a&gt;&lt;/li&gt;';&lt;/p&gt; &lt;p&gt; }<br>
//输出后加入缓存<br>
wp_cache_add('recentComments_'.$cacheID,$output, 'Deamwork');<br>
echo $output;<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
最后,试试效果吧~</p>
頁: [1]
查看完整版本: wordpress自带的缓存功能使用介绍