最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

wordpress自帶的緩存功能使用介紹

  發(fā)布時(shí)間:2014-03-31 09:47:25   作者:佚名   我要評(píng)論
這里不是說(shuō)的的使用緩存插件之類的東西,是關(guān)于如何利用wordpress自帶的緩存體系。很多時(shí)候,比如輸出最新評(píng)論、相關(guān)文章、讀者墻等地方, 基本是不需要實(shí)時(shí)更新的,然而每次打開(kāi)頁(yè)面就會(huì)去數(shù)據(jù)庫(kù)里查詢一次

用靜態(tài)化當(dāng)然可以解決這些問(wèn)題,不過(guò)對(duì)于流量不大的博客就沒(méi)必要了。 wordpress自帶有緩存體系,關(guān)鍵的函數(shù)你可以在wp-includes/cache.php或 Codex里查找到所有的函數(shù)。


你可以使用下列函數(shù)來(lái)實(shí)現(xiàn)添加緩存。

復(fù)制代碼
代碼如下:

wp_cache_add($key, $data, $flag = '', $expire = 0)

相反的,要?jiǎng)h除緩存數(shù)據(jù)可以通過(guò)下面的方法實(shí)現(xiàn)。

復(fù)制代碼
代碼如下:

wp_cache_delete($id, $flag = '')

提取緩存數(shù)據(jù):

復(fù)制代碼
代碼如下:

wp_cache_get($id, $flag = '')

替換緩存數(shù)據(jù):

復(fù)制代碼
代碼如下:

wp_cache_replace($key, $data, $flag = '', $expire = 0)

 
舉個(gè)例子,如果想輸出最新評(píng)論,可以先從緩存里讀取需要輸出的HTML,如果沒(méi)有,請(qǐng)求一次,成功后加入緩存,當(dāng)然時(shí)間可以控制。
我知道有很多最新評(píng)論,相關(guān)文章免插件輸出的方法,原理都一樣,可以自行改改,貼個(gè)我用的:
config.php里面添加

復(fù)制代碼
代碼如下:

define('WP_CACHE', true);

如果你用了其它緩存插件,基本上這個(gè)就沒(méi)效果了。因?yàn)檫@些插件有類似的方法。


復(fù)制代碼
代碼如下:

<?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') . "'";
//自動(dòng)獲取博主郵箱
$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('沒(méi)有數(shù)據(jù)');
return;
}
//歷遍數(shù)據(jù)
$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="' . 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;
}

最后,試試效果吧~

相關(guān)文章

最新評(píng)論

奎屯市| 虞城县| 靖远县| 石景山区| 永福县| 柘城县| 五指山市| 仁怀市| 从江县| 资溪县| 林州市| 榆社县| 宝坻区| 珠海市| 偏关县| 吉木萨尔县| 浦北县| 上林县| 高阳县| 琼结县| 阿荣旗| 富民县| 枣强县| 城固县| 宿松县| 和平区| 海门市| 昌黎县| 南雄市| 万源市| 太谷县| 东台市| 沾益县| 新宾| 清镇市| 平泉县| 衡水市| 闵行区| 濉溪县| 广东省| 灵台县|