写在block的分页缓存

function og_blocks_hot_topics(){
    $output = '';
    $total = db_query("SELECT count(1) FROM {node} WHERE status=1 AND type='group_content' AND promote = 1")->fetchField();
    $cache_total = cache_get('og_blocks:hot_topics_total', 'cache_block');
    $old_total = 0;
    if (!empty($cache_total) && isset($cache_total->data) && !empty($cache_total->data)) {
        $old_total = $cache_total->data;
    }
    if($total<>$old_total){
        db_delete('cache_block')->condition('cid', 'og_blocks:hot_topics_page_%','LIKE')->execute();
        //cache_clear_all('og_blocks:','cache_block',TRUE);
    }
    $page = isset($_GET['page'])?intval($_GET['page']):0;
    $cache = cache_get('og_blocks:hot_topics_page_'.$page, 'cache_block');
    if (!empty($cache) && isset($cache->data) && !empty($cache->data)) {
            $output = $cache->data;
    }
    else{
        $num_per_page = 5;
        $page = pager_default_initialize($total, $num_per_page, 0);
        $offset = $num_per_page * $page;
        $sql = "SELECT n.uid,n.title,n.nid,om.gid,n.created,SUBSTRING_INDEX(GROUP_CONCAT(fm.uri),',',4) AS img_uri
                    FROM {og_membership} om
                    INNER JOIN {node} n ON om.etid = n.nid AND om.entity_type='node' AND n.promote=1 AND n.status=1 AND n.type='group_content'
                    LEFT JOIN {field_data_field_image} fi ON om.etid=fi.entity_id
                    LEFT JOIN {file_managed} fm ON  fi.field_image_fid=fm.fid
                    GROUP BY om.etid ORDER BY n.created DESC LIMIT $offset ,$num_per_page";
        $result = db_query($sql)->fetchAll();
        $rows =  array();
        if(!empty($result)){
            foreach ($result as $data) {
                $userinfo = user_load($data->uid);
                $group = node_load($data->gid);
                $group_content = node_load($data->nid);
                $row = array();
                $row['title'] =  $data->title;
                $row['nid'] =  $data->nid;
                $row['summary'] = $group_content->body['und'][0]['summary'];
                $row['name'] =  $userinfo->name;
                $row['created'] =  $group_content->created;
                $row['img_uri'] =  $data->img_uri;
                $row['gname'] =  $group->title;
                $rows[] = $row;
            }
        }
        unset($userinfo,$group,$group_content,$result);
        $output .= theme('og_blocks_hot_topics', array('rows' => $rows));
        //$args = array('quantity' => 12,'tags' => array('首页','<','','>','>>'));
        //$output .= theme('pager',$args);
        $output .= theme('pager');
        cache_set('og_blocks:hot_topics_total', $total,'cache_block');
        cache_set('og_blocks:hot_topics_page_'.$page, $output,'cache_block', time() + 1800);
    }
    return $output;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值