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

值得研究
值得翻译
值得适用.
值得推荐
值得批评:PostMeat
汗,好吃不?
值得六楼.
值得升级
值得评论
专业…值得学习
最近有点忙,一直没时间过来看鱼儿。
呵呵…得闲来踩踩
值得郁闷!后台界面成英文了?!中文mo文件没有作用?
我这里一切正常啊。
看来是你是RP问题
建个视图怎么样?
下来研究一下… 水鱼要支持, 很强大的地球人
其实我来自火星!
刚刚后台提醒我升级了。
不过还是等等吧
升了···在品
技术贴。学习了
download enjoy it
有个问题,装了后数据库狂报错
类似这样的
[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???
我晕死,你撞得又不是我写的插件。
memcache?
我的服务器上装的有eaccelerator.
不知道是否有用????????
not memcached
eaccelerator, WordPress 由相关插件可用!
好东西还真的是不少啊
如果是根据tag查找的相关文章,应该是很快的,不需要把表扫一遍的。二叉树查找很快(当然实际实现有点不一样)。