How to Enable Page Cache in Codeigniter?

Share now

In this blog, we will see about how to enable Page Cache in CodeIgniter? by Lia Infraservices – The Leading Web App Development Company in Chennai. In general, caching is the process of storing copies of files in cache memory or temporary storage location like a hard disk, so that they can be accessed more quickly. Cache on the Internet serves a similar purpose. That is they temporarily store the page content, needed for users to make their journey across the web.

Caching a page will improve the page load speed. If the page is cached, then it will be stored in its fully rendered state. Next time, when the server gets a request for the cached page, it will be directly sent to the requested browser.

Cached files are stored in the application or cache folder. Caching can be enabled on a per-page basis. While enabling the cache, we need to set the time, until which it needs to remain in the cached folder and after that period, it will be deleted automatically.

Procedure to Enable Page Cache in codeIgniter:

Before moving into the steps to enable page cache in CodeIgniter, let us see what is CodeIgniter.

What is CodeIgnitor?

As you know JavaScript is the best Frontend development Programming language in Web Application Development In Chennai. The front end uses web languages such as CSS, HTML, and JavaScript. The programming languages used in the back end may include PHP, Java, Python, and Ruby.

JavaScript is a dynamic language. It is mainly used for client-side scripting. Codeigniter is a JavaScript FrameWork, provides a library to help you with certain common functions that you may want to use with Javascript.

Caching can be enabled by executing the following line in any of the controller’s methods.

$this->output->cache(‘$n’);

Where $n is the number of minutes you wish the page to remain cached between refreshes.

How to create a page Cache?

To create a page cache first you should create a folder name called cache in the application folder the image that I have given below for example 

How to create a page Cache      - Lia Infraservices.

And use this keyword call cache $this->output->cache(1);


Sample code to create page cache agro:

public function bob()

{

$data[‘sender’] = ‘alice’;

$data[‘receiver’] = ‘bob’;

$this->output->cache(1);

    $messages =  $this->db->get(‘messages’)->result_array();

foreach ($messages as $key => $message) {

$messages[$key][‘text’] = $this->encryption->decrypt($message[‘text’]);

}

$data[‘messages’] = $messages;

$this->load->view(‘chat’,$data);

}

Use this keyword You can see the cache file is created cache Apliar in the application folder the image given been below:

Use this keyword You can see the cache file is created in the application

How Deleting Caches affects CodeIgniter?

If you no longer wish to cache a file you can remove the caching tag and it will no longer be refreshed when it expires.

If you need to manually delete the cache, you can use the delete_cache() method:

  1. Deletes cache for the currently requested URI

$this->output->delete_cache();

2. Deletes cache for /foo/bar

$this->output->delete_cache(‘bob’);

Sample code :

public function sendMessage(){

$text = $this->input->post(‘message’);

$text = $this->encryption->encrypt($text);

$data[‘text’] = $text;

$data[‘receiver’] = $this->input->post(‘receiver’);

$data[‘sender’] = $this->input->post(‘sender’);

$this->db->insert(‘messages’,$data);

$this->output->delete_cache();

redirect(base_url().’chat/’.$data[‘receiver’]);

}

Result:

You can see the cache file have been deleted in the application folder the screenshot has been given below

Screenshot for cache clear :

Cache file deleted

Conclusion:

Now with Lia infraservices – Top Mobile App Development Company in Chennai and the Best Web App Development Company in Chennai, you can create these above-mentioned files and put them in their respective folder or you can download them from the link given.

Thanks for reading the complete post. Hope you have got the concept. You can share your views in the space provided below and get in touch with us – Contact Lia Infraservices.

Was this article helpful?

Leave a Reply

Your email address will not be published.