简体   繁体   English

具有动态内容的Codeigniter HTML缓存

[英]Codeigniter HTML Caching with Dynamic Content

I have an application where I use ajax to update the comments & Like count. 我有一个应用程序,使用Ajax更新评论和喜欢计数。

I am using Codeigniter page caching http://www.codeigniter.com/user_guide/general/caching.html 我正在使用Codeigniter页面缓存http://www.codeigniter.com/user_guide/general/caching.html

and set following code to recreate caching every 60 minutes 并设置以下代码以每60分钟重新创建缓存

$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. 问题是当有人输入新注释时,正在发生DB操作(由于Ajax调用),但是由于缓存的HTML页面,新插入的注释在页面刷新后消失了。 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. 听起来您正在使用AJAX功能来提交数据,同时也在客户端修改网页。 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: 将以下代码行放入控制器中(在接收AJAX数据的函数中)即可:

$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. 就我而言,我在codeigniter中使用自定义路由。 When user type example.com it is routed to example.com/CONTROLLER/FUNCTIONNAME and that too default landing page. 当用户键入example.com时,它将被路由到example.com/CONTROLLER/FUNCTIONNAME以及默认登录页面。

So i created a new function. 所以我创建了一个新功能。 Please refer here 参考这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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