Implement server cache: codeigniter

Codeigniter example: Use server cache to improve page loading, use static html files to output dynamic content as well as faster loading.

Implementing server side cache in a website can dramatically improve page loading by reducing the server processing time. 

The example explains how we can get the benefit of caching in a particular php based web site using codeigniter engine. 

In the CMS application we generate pages using the information stored on the database. Following workflow involves to deliver the a particular page to the visitor. 

1. Read the url of the requesed page and fetch the page attribute. eg: permalink or id. In this example we use permalink.
$permalink= $this->uri->segment(1);
If the users enters like this the permalink will be abc.html

2. The permalink is used to determine which page user has requested. Query is made to the database to get the page object which consists of the page content. 

3. The other information related to the requested page is also queries on the database. 

4. Use all the information retrieved from the database and generate the view and return to the user. 

If we could have hosted the same static page, the page is instantly delivered to the visitor by Apache itself.

Cach the entire view

By caching the entire view to the file cache, you can dramatically improve performance by eliminating db queries. In codeigniter we can do like this. 

 public function  viewHtml($permalink)
        $this->db->where('permalink', $permalink);
        $pages = $this->db->get('pages');
        if ($pages->num_rows > 0) {
            $pagerecords = $pages->result();
            $data['pagerow'] = $pagerecords[0];
            $pageid = $pagerecords[0]->id;

                $this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));

                $cachedata = $this->cache->get('page_' . $pageid);
                    if ($cachedata) {

                $data['title'] = $pagerecords[0]->title;
                $data['metadesc'] = $pagerecords[0]->summary;
                $data['blocks'] = $this->db->query('select * from pageblocks where pageid=' . $pageid . ' order by orderno asc');

                    $viewdata = $this->load->view('page_view', $data, true);
                    $this->cache->save('page_' . $pageid, $viewdata, 2592000); // cache for 1 month
        redirect("/page/p404", "location", 404);

The above function first checks if the cache is available for the required page id. If exists outpus cache to the visitor. If doesnot exist, fetches all the contents  from the database and creates view, stores the cache to the file cache and outputs the content. 

When somebody edits the page, make sure to refresh the cache or delete it. 

Alternative way

If the html file already exits, Apache directly handles them and returns to visitor. But if there is not such file, in codeingnier we can configure route to let controller handle the request.

Instead of caching the entire output, we write them to the html file directly on the root or specific folder. Hence we can further improve by completely bypassing the php engine.

In my example the user requests the url "". The request is forward to the codeigniter controller if the file mypage.html does not exist on the root directory. 

 $route['([a-z0-9-_]+).html'] = "page/view/$1/";

The controller function then creates the html file on the fly so next time the visitor request the same page, it is directly hosted by Apache.

write file to the main application folder
                    //write_file('././'.$slug. ".html", $viewdata);