使用 PostMeta 提速 WordPress 插件
June 5th, 2008 Denis
WordPress Related Posts 这个插件已经被下载了2万多次。但是个人对这个插件一直有一个不满意的地方,那就是效率不是很高。我在我的 Bluehost 空间安装这个插件的时候,几次因为这个插件 SLOW SQL 搞的 CPU 超限而被 Suspend 了几分钟,狂晕。
是的,这个插件是使用了一条效率很低的 SQL,因为根据 Tag 来查找相关日至要对所有的 Post 扫一便,看看是不是有相同的 Tag。我想了很久,还是没有想到效率更高的缓存,大学的时候 SQL 还是学得不错,但是现在基本都不懂了,汗!既然没有想到效率更高的 SQL,我想到的第二个方法是缓存,第一个方法是可以使用 WordPress 对象缓存,但是 WordPress 2.5 已经全部采用内存缓存而舍弃了文件缓存,如果主机没有安装内存缓存模块,基本没用,当然也可以是用高级缓存插件,如:WordPress Super Cache。不过我这里使用的是 PostMeta 来缓存。
我们知道,PostMeta 表是用来存储 Post 其他自定义字段,比如你可以在 WordPress 编辑界面自定义字段那里使用它。那么我可以把当前日志的相关日志列表写到 PostMeta 中去,并记录写入的时间。然后再取出,就可以达到加速的效果。具体代码如如下:
<?php global $id; $output_old = get_post_meta($id, "related_posts", $single = true); // 从 postmeta 表中获取缓存的相关日志 if($output_old){ //如果返回结果不为空 $time = time(); if(($time - $output_old["time"])<600){ //并且在缓存的时间以内(600秒) echo $output_old["related_posts"]; } }else{ $output = wp_get_related_posts() ; //获取相关日志 $output_new = array("time"=>time(),"related_posts"=>$output); if($output_old){//如果 postmeta 中已有记录,更新 update_post_meta($id, 'related_posts', $output_new); }else{ //否则,新插入 add_post_meta($id, 'related_posts', $output_new, true); } echo $output; } ?>
当然了,这样缓存也有点不好,就是相关后台配置修改要一段时间才能体现出来。下载:WordPress Related Posts 0.8
作者:Denis
原文链接:使用 PostMeta 提速 WordPress 插件





1. 胖逗逗 | June 5, 2008 at 5:40 pm | #
值得研究
2. 大猫 | June 5, 2008 at 6:22 pm | #
值得翻译
3. kusanagi | June 5, 2008 at 7:14 pm | #
值得适用.
4. Geedr.com | June 5, 2008 at 7:39 pm | #
值得推荐
5. Lucy | June 5, 2008 at 7:50 pm | #
值得批评:PostMeat
6. Denis | June 5, 2008 at 8:59 pm | #
汗,好吃不?
7. wangyh | June 5, 2008 at 7:50 pm | #
值得六楼.
8. Black-Xstar | June 5, 2008 at 9:02 pm | #
值得升级
9. millettam | June 5, 2008 at 11:23 pm | #
值得评论
10. 李海鑫 | June 6, 2008 at 9:07 am | #
专业…值得学习
11. tomdbtel | June 6, 2008 at 9:56 am | #
最近有点忙,一直没时间过来看鱼儿。
呵呵…得闲来踩踩
12. patrick | June 6, 2008 at 11:13 am | #
值得郁闷!后台界面成英文了?!中文mo文件没有作用?
13. Black-Xstar | June 6, 2008 at 12:45 pm | #
我这里一切正常啊。
看来是你是RP问题
14. lifishake | June 6, 2008 at 11:28 am | #
建个视图怎么样?
15. JackyCheung | June 6, 2008 at 11:57 am | #
下来研究一下… 水鱼要支持, 很强大的地球人
16. Denis | June 6, 2008 at 12:48 pm | #
其实我来自火星!
17. Black-Xstar | June 6, 2008 at 12:37 pm | #
刚刚后台提醒我升级了。
不过还是等等吧
18. MY-Hou | June 6, 2008 at 1:56 pm | #
升了···在品
19. 雀巢 | June 6, 2008 at 2:04 pm | #
技术贴。学习了
20. 二手科学家 | June 6, 2008 at 2:20 pm | #
download enjoy it
21. kmnow2004 | June 12, 2008 at 4:14 pm | #
有个问题,装了后数据库狂报错
类似这样的
[12-Jun-2008 02:12:12] WordPress database error Can’t find FULLTEXT index matching the column list for query SELECT ID, post_title, post_content,MATCH (post_name, post_content) AGAINST (’guide includes thickbox loadinganimatio gif’) AS score FROM wp_posts WHERE MATCH (post_name, post_content) AGAINST (’guide includes thickbox loadinganimatio gif’) AND post_date <= ‘2008-06-12 08:12:12′ AND (post_status IN ( ‘publish’, ’static’ )) AND post_password =” ORDER BY score DESC LIMIT 6 made by aa_related_posts_404
[12-Jun-2008 02:12:21] WordPress database error Can’t find FULLTEXT index matching the column list for query SELECT ID, post_title, post_content,MATCH (post_name, post_content) AGAINST (’includes thickbox loadinganimatio gif’) AS score FROM wp_posts WHERE MATCH (post_name, post_content) AGAINST (’includes thickbox loadinganimatio gif’) AND post_date <= ‘2008-06-12 08:12:21′ AND (post_status IN ( ‘publish’, ’static’ )) AND post_password =” ORDER BY score DESC LIMIT 2 made by aa_related_posts_404
[12-Jun-2008 02:12:21] WordPress database error Can’t find FULLTEXT index matching the column list for query SELECT ID, post_title, post_content,MATCH (post_name, post_content) AGAINST (’includes thickbox loadinganimatio gif’) AS score FROM wp_posts WHERE MATCH (post_name, post_content) AGAINST (’includes thickbox loadinganimatio gif’) AND post_date <= ‘2008-06-12 08:12:21′ AND (post_status IN ( ‘publish’, ’static’ )) AND post_password =” ORDER BY score DESC LIMIT 6 made by aa_related_posts_404
why???
22. Denis | June 12, 2008 at 5:47 pm | #
我晕死,你撞得又不是我写的插件。
23. 小哀 | June 15, 2008 at 12:59 pm | #
memcache?
我的服务器上装的有eaccelerator.
不知道是否有用????????
24. Denis | June 18, 2008 at 4:31 pm | #
not memcached
eaccelerator, WordPress 由相关插件可用!
25. 分享博客 | August 23, 2008 at 8:23 pm | #
好东西还真的是不少啊
26. zhang | August 27, 2008 at 8:25 pm | #
如果是根据tag查找的相关文章,应该是很快的,不需要把表扫一遍的。二叉树查找很快(当然实际实现有点不一样)。