简体   繁体   中英

Codeigniter HTML Caching with Dynamic Content

I have an application where I use ajax to update the comments & Like count.

I am using Codeigniter page caching http://www.codeigniter.com/user_guide/general/caching.html

and set following code to recreate caching every 60 minutes

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

Problem is when somebody enter a new comment, DB operation is happening (Because of Ajax call), But the Newly inserted comment disappears after the page refresh, because of the cached HTML page. How to handle caching and also dynamic content to change?

The main purpose of caching is to save server resources on page load (ie so the server doesn't need to fetch all the dynamic data every time its loaded).

It sounds like you are using the AJAX function to both submit the data but also modify the webpage on the client side simultaneously. When the user then refreshes the page, this will fetch the content back from the server, in this case the original cached content that was generated before the comment was made and is therefore operating exactly as it should.

If you have a true requirement for caching this page, you could consider deleting the cache, which would force it to be rebuilt on the next page load whenever a comment is made. Placing the following line of code in your controller (in the function that received the AJAX data) should do it:

$this->output->delete_cache('foo/bar');

I found the solution.

To delete a cache file you need to use the following function

$this->output->delete_cache('CONTROLLER/FUNCTION');

Note: No Slash before or after.

In my Case i was using custom routes in codeigniter. When user type example.com it is routed to example.com/CONTROLLER/FUNCTIONNAME and that too default landing page.

So i created a new function. Please refer here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM