优化功能,用于加速应用程序

时间:2013-02-18 22:10:27

标签: php codeigniter

我使用以下代码检查注释中描述的一些案例。 不幸的是,它降低了页面加载速度。 为了改善功能并正确使用它们,你有什么建议,我该怎么办?

function insert() {
    if ($this - > session - > userdata('logged_in')) { //This is for test whether the user is logged in or not?
        if ($this - > siran - > access_level_active()) { //This is because the user name manager is active or not?
            if ($this - > siran - > access_level('1', '5')) { //This is for whether or not permission to access this page?
if($_POST){
                $this - > load - > view('admin/onepage');
}else{
        $this->session->set_flashdata('message_p', $this->lang->line('nopost'));
        redirect($this->session->userdata('previous_page'));
        }
            } else {
                $this - > session - > set_flashdata('message', $this - > lang - > line('notaccess'));
                redirect($this - > session - > userdata('previous_page'));
            }
        } else {
            $this - > session - > set_flashdata('message', $this - > lang - > line('noactive'));
            redirect('login', 'refresh');
        }
    } else {
        $this - > session - > set_flashdata('message', $this - > lang - > line('plslogin'));
        redirect('login', 'refresh');
    }
}

库代码生成器中的这三个函数:

public function access_level($mp, $access){
    //$mp = '1';
    $CI = &get_instance();
            $result = $CI->db->get_where('access_level', array('mainpage'=>$mp,'id_relation'=>$CI->session->userdata('id_relation')));
    $data = array();
    $out = array();
    foreach($result->result() as $row){
        $dv = json_decode($row->subpage);       
        $flat = array();        
        foreach ( $dv as $item ) {
            $flat = array_merge( $flat, explode( ',', $item ) );
        }       
        $out = array_merge( $out, $flat );
    }
    if(in_array($access, $out) || in_array('110', $out)){
        return  true;
    }else{
        return false;
    }
}
public function access_level_active(){
    //$mp = '1';
    $CI = &get_instance();
            $result = $CI->db->get_where('users', array('id'=>$CI->session->userdata('id')));
    if ($result->num_rows() > 0){
        $row = $result->row();
        return ($row->active == 1 ? true : false );
    }else{
        return false;
    }
}
function previous_page(){
    $CI = &get_instance();
    if ($CI->session->userdata('logged_in')) {
        if (isset($_SERVER['HTTP_REFERER']))
        {
            $CI->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
        }else
        {
            $CI->session->set_userdata('previous_page', base_url());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码看起来并不是特别密集,我唯一要改变的是限制嵌套的次数,如果{} else {}语句本身,并将任何重复的代码移到另一个函数。例如,我会亲自重写您的insert()代码,如下所示(主要是因为它看起来更干净)。

插入功能:

function insert() {

    if (!$this->session->userdata('logged_in')) {
        $this->_flashRedirect('login', $this->lang->line('plslogin'));  
    }

    if (!$this->siran->access_level_active()) {
        $this->_flashRedirect('login', $this->lang->line('noactive'));  
    }

    if (!$this->siran->access_level('1', '5')) {
        $this->_flashRedirect($this->session->userdata('previous_page'), $this->lang->line('notaccess'));       
    }

    if($_POST){
        $this->load->view('admin/onepage');
    } else {
        $this->_flashRedirect($this->session->userdata('previous_page'), $this->lang->line('nopost'), 'message_p'); 
    }

}

在您的控制器中创建一个新功能_flashRedirect()

public function _flashRedirect($page, $message, $message_type = 'message') {
    $this->session->set_flashdata('message', $message);
    redirect($page, 'refresh');
}

至于速度,您的查询(mysql)更可能是导致性能问题的原因。你要返回多少行,你的表有多大?