国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > Discuz > Discuz!中增加memcache缓存的方法

Discuz!中增加memcache缓存的方法

来源:程序员人生   发布时间:2014-01-01 14:54:14 阅读次数:2447次
很多站长都有体会,随着坛子越来越大,链接超时的问题越来越严峻。

google后并尝试用这里说的方法来做优化。但发现不够。被缓存的帖子不会在修改或者删除后自动清空缓存。当存在附件的帖子有权限和无权限的人看到的效果一致:要么都不能看到下载链接要么都能看到下载链接。

其实discuz最大的问题是帖子表cdb_threads。所以一般来说对这张表做了cache就会给论坛减轻很大的压力。

笔者做了尝试,效果还可以。所以把方法给大家看看。

先装memcache:http://blog.csdn.net/luojianlong/archive/2008/05/05/2393271.aspx.这里就不做赘述了。

1.在config.inc.php中增加

$memcachehost = '127.0.0.1';
$memcacheport = 11211;

2.在include/common.inc.php中
$mem = new Memcache;
$mem->connect($memcachehost, $memcacheport);

3.在viewthread.php中

找到

$newpostanchor = $postcount = $ratelogpids = 0;

$onlineauthors = array();

在下面加上

// added by rubeljl for memcache the first page of viewthread 2009.6.8 start
$postlist       = Array();
$bUpdateMem     = false;
if( 1 == $page ){
        $postlist       = $mem->get( 'viewthread_' . $tid . '_1');
        $bUpdateMem     = empty( $postlist ) ? false : true;
}

$bHasAttachment = false;
if( !is_array( $postlist ) || empty( $postlist ) ){
// added by rubeljl for memcache the first page of viewthread 2009.6.8 end

找到:while($post = $db->fetch_array($query)) {

在下面增加:

if( $post['attachment'] ){
        $bHasAttachment = true;
}

找到$postlist[$post['pid']] = viewthread_procpost($post);

在下面增加:

if( 1 == $page && false === $bHasAttachment ){
                $mem->set( 'viewthread_' . $tid . '_1', $postlist, 0, 1000 );
        }
}

4.在topicadmin.php中

找到$db->query("DELETE FROM {$tablepre}posts WHERE pid IN ($pids)");

下面添加:

$mem->delete( 'viewthread_' . $tid . '_1' );

找到:$db->query("DELETE FROM {$tablepre}myposts WHERE tid='$othertid'");

下面添加:

$mem->delete( 'viewthread_' . $tid . '_1' );

5.在include/newreply.inc.php中

找到$pid = $db->insert_id();

下面添加:$mem->delete( 'viewthread_' . $tid . '_1' );
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生