寫在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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章