简体   繁体   English

在缓存页面中调用动态内容(当前使用ajax)的正确方法是什么?

[英]What is the right way to call dynamic content (currently using ajax) inside a cached page?

We have a news website where we cache a complete article page. 我们有一个新闻网站,我们缓存一个完整的文章页面。

There are 4 areas that need to continue to be dynamic on that page: 该页面上有4个区域需要继续保持动态:

  1. View Counter: We add +1 to view_counts of that article when page loads. 查看计数器:我们在页面加载时向该文章的view_counts添加+1。
  2. Header: On the header of the website we check if session->id exists or not if it does we display a Welcome [Name], My Profile / Logout and if not we show Register / Login. 标题:在网站的标题上,我们检查session-> id是否存在,如果是,我们显示欢迎[名称],我的个人资料/注销,如果不存在,我们会显示注册/登录。
  3. Comments: We display the comments made for that article. 评论:我们显示为该文章所做的评论。
  4. Track User Behavior: We track every single action made by users on the site 跟踪用户行为:我们跟踪用户在网站上所做的每一个操作

Now the only way we could think of doing this is through AJAX calls: 现在,我们想到这样做的唯一方法是通过AJAX调用:

$('#usercheck').load(<?php echo "'" . base_url() . "ajax/check_header'"; ?>);

And so on. 等等。

This is creating a massive load on CPU, but what would be the right/alternative way of approaching this? 这对CPU产生了巨大的负担,但是接近这个的正确/替代方式是什么?

Please see attached: 请参照附件:

负载在60分钟

网站上的高交易

First of all, you do not have to use AJAX for every possible dynamic content, especially in the case of comments, you may as well load them via an iframe . 首先,您不必为每个可能的动态内容使用AJAX,尤其是在评论的情况下,您也可以通过iframe加载它们。 That way, you are not relying on Javascript to make the request. 这样,您不依赖于Javascript来发出请求。 It may even work for the counter. 它甚至可能适用于柜台。

However, you problem is not Javascript, nor the database server, based on what I can see from your graph. 但是,根据我在图表中看到的内容,您的问题不是Javascript,也不是数据库服务器。 It seems to me you have some heavy PHP controllers, maybe you are loading a heavy framework just to have $session->id checked. 在我看来,你有一些沉重的PHP控制器,也许你正在加载一个繁重的框架只是为了检查$session->id

Further, what do you mean by "we track every single action"? 此外,“我们跟踪每一个动作”是什么意思? How do you track them? 你如何追踪它们? Are you sending an AJAX request from every little thing or are you debouncing them with JS and only sending them one every 30 seconds or so? 你是从每一个小东西发送一个AJAX请求还是你用JS去掉它们,并且每隔30秒左右只发送一个?

My advice is that you consider the size of the PHP code you are calling, and slim it down as much as you can, even to zero if it seems feasible (by leveraging localStorage to keep track of you user session after the first login), and maybe loading the counter and the comments in alternative ways. 我的建议是你考虑你正在调用的PHP代码的大小,并尽可能地减少它,如果看起来可行,甚至为零(通过利用localStorage在第一次登录后跟踪你的用户会话),并且可能以其他方式加载计数器和评论。

For example, I infer you are only checking the counter once per page load, ignoring subsequent loads by other users while the current user is reading the article, so your counter may happen to be out-of-date once ia while, depending on your traffic. 例如,我推断您只是在每页加载时检查一次计数器,忽略当前用户正在阅读文章时其他用户的后续加载,因此您的计数器可能会在一段时间内过时,具体取决于您的交通。

I going to explain it better: your page has n views, so when I load it, you request for n and then display n+1 to me. 我将更好地解释它:你的页面有n个视图,所以当我加载它时,你请求n然后向我显示n + 1 While I'm reading, the same page gets requested and viewed x times by other users. 在我阅读时,其他用户会请求并查看同一页面x次。 Your counter on the server has been surely updated to n+x , but the counter on my page still says "n views" . 您在服务器上的计数器肯定已更新为n + x ,但我页面上的计数器仍然显示“n views” So, what's the point in being picky and showing n+1 to me and the not updating it, thus being off by x ? 那么,挑剔并向我展示n + 1并且没有更新它有什么意义,因此被x关闭?

So, first of all the counter controller should be as slim as possible, and what if you loaded it within an iframe, auto updating without AJAX? 所以,首先计数器控制器应该尽可能的纤薄,如果你在iframe中加载它,如果没有AJAX自动更新怎么办?

How to refresh an iframe not using javascript? 如何使用javascript刷新iframe?

That would keep the counter up-to-date, you may render it with PHP just once per page view, and then just statically serve the resulting HTML file. 这将使计数器保持最新状态,您可以在每个页面视图中使用PHP渲染一次,然后静态地提供生成的HTML文件。

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

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