简体   繁体   English

大选择减慢页面加载 - 缓存PHP

[英]large select slowing down page load - caching php

I'm building a web app, the way I started off the app for testing purposes is to load lots of data in to session arrays from my database so I can use the values easily throughout the pages. 我正在构建一个Web应用程序,我从应用程序开始进行测试的方式是从我的数据库中将大量数据加载到会话数组中,这样我就可以在整个页面中轻松使用这些值。 I have one page the has numerous selects on it, and each time the php page loops through all the variables, chooses the selected one, and outputs the dropdown. 我有一个页面上有很多选项,每次php页面遍历所有变量,选择所选变量,并输出下拉列表。 One of my arrays though has just under 3000 values and loading this dropdown slows the page down from about 300ms to 1-1.2s. 我的一个阵列虽然有不到3000个值,但加载此下拉列表会使页面从大约300毫秒减慢到1-1.2秒。 Not terrible but easy to tell that it is less responsive. 不是很糟糕,但很容易分辨它反应迟钝。 So I'd like to know if there is anyway for me to improve the load speed, or any thoughts on a substitute for the dropdown. 所以我想知道我是否还有提高加载速度,或者对替代下拉列表的任何想法。

What I have tried so far: 到目前为止我尝试过的:

  1. Session arrays hold all the values, when the page is loaded through jquery ajax method the php page loops through these values and echos the dropdowns. 会话数组包含所有值,当页面通过jquery ajax方法加载时,php页面循环遍历这些值并回显下拉列表。

  2. Php include - create php or html pages of all the values pre written as selects, this creates a ~100kb page for the problem dropdown and this is then included with include . PHP包括 - 创建预先写为选择的所有值的php或html页面,这将为问题下拉创建一个~100kb的页面,然后将其包含在include Takes roughly the same amount plus I'd have to then use javascript to set the value, but I'd do this if it could be improved. 大概相同的金额加上我必须使用javascript来设置值,但如果可以改进我会这样做。 I thought perhaps some caching could provide improvements here. 我想也许一些缓存可以在这里提供改进。 There seemed to be no significant difference between html and php pages for include but I'd assume html would be better. html和php页面之间似乎没有显着差异,但我认为html会更好。 I'm also assuming that I cannot use regular caching because I am using a php function to include these pages. 我也假设我不能使用常规缓存,因为我使用php函数来包含这些页面。

  3. I have tried just loading in the html page and it takes about 1 sec on first load, after browser caching it is back down to 100-350ms so I imagine caching could provide a huge boost in performance. 我已经尝试在html页面加载,第一次加载大约需要1秒,浏览器缓存后它恢复到100-350ms,所以我想缓存可以提供巨大的性能提升。

I have considered: 我考虑过:

  1. Creating a cached version of the whole page but this will be quite the pain to implement so I'd only do it if people thought it is the right way to go with this. 创建整个页面的缓存版本,但这将是非常难以实现的,所以我只会在人们认为这是正确的方法时才这样做。 I would have to use ajax to retrieve some data for the inputs which I was originally doing with php echos. 我将不得不使用ajax来检索我最初用php echos做的输入的一些数据。

  2. Just removing the problem dropdown. 只需删除问题下拉列表。

Just to clarify something I've never had clarified, am I correct in thinking php pages can never be cached by the browser, and so by extension any php included files can't be either. 只是为了澄清一些我从未澄清过的内容,我认为php页面永远不会被浏览器缓存,因此通过扩展,任何包含php的文件都不能。 But then how come a javascript file linked to in a php file can be cached, because it is using an html method? 但是,如何在php文件中链接的javascript文件可以缓存,因为它使用的是html方法?

I suspect the problem is the raw size of the data you're transmitting, based on the results of number 2 in "What I have tried so far." 我怀疑问题是你传输的数据的原始大小,根据“我到目前为止尝试过的数字2”的结果。 I don't think you can rely on browser caching, and server-side caching won't change the size of the data transmitted. 我认为你不能依赖浏览器缓存,而服务器端缓存也不会改变传输数据的大小。

Here are a couple of ideas to reduce the amount of data transmitted during page load: 以下是一些减少页面加载期间传输的数据量的想法:

  1. Load the select box separately, after the main page has been delivered, using an asynchronous javascript call. 在主页面交付后,使用异步javascript调用单独加载选择框。
  2. Break the choice into a hierarchical series of choices. 将选择分解为一系列分层选择。 User chooses the top-level category, then another select box is populated with matching sub-categories. 用户选择顶级类别,然后使用匹配的子类别填充另一个选择框。 When they choose a sub-category, the third box fills with the actual options in that sub-category. 当他们选择子类别时,第三个框填充该子类别中的实际选项。 Something like this . 这样的东西。 Of course, this only works if those 2nd and 3rd controls are filled-in using an async javascript call. 当然,这仅适用于使用异步javascript调用填充第2和第3个控件的情况。

Either way, make sure gzip compression is enabled on your server. 无论哪种方式,请确保在您的服务器上启用了gzip压缩。

Edit: More on browser caching 编辑:有关浏览器缓存的更多信息
The browser caches individual files, and you typically don't ask it to cache PHP pages because they may be different next time. 浏览器缓存单个文件,您通常不要求它缓存PHP页面,因为下次它们可能不同。 (Individual php includes are invisible to the browser, because PHP combines their contents into the HTML stream.) If you use a browser's developer console (hit f12 on Chrome and go to Network, for example), you can see that most pages cause multiple requests from the browser to the server, and you may even see that some of those files (js, css, images) are coming from the cache. (单个php包括浏览器不可见,因为PHP将其内容组合到HTML流中。)如果您使用浏览器的开发者控制台(例如,在Chrome上点击f12并转到网络),您可以看到大多数页面导致多个从浏览器到服务器的请求,您甚至可能会看到其中一些文件(js,css,images)来自缓存。

What the browser caches and for how long is controlled by various HTTP response headers, like Cache-Control and Expires . 浏览器缓存的内容以及由各种HTTP响应头控制的时间长度,如Cache-ControlExpires If you don't override these in php by calling the header function, they are controlled by the web server (Apache) configuration. 如果你不通过调用header函数在php中覆盖它们,它们将由Web服务器(Apache)配置控制。

The data being returned and parsed into a dropdown is probably your bottleneck. 返回并解析为下拉列表的数据可能是您的瓶颈。 However, if the bottleneck is actually the PHP code you could try installing an optcode cache like APC at http://php.net/manual/en/book.apc.php . 但是,如果瓶颈实际上是PHP代码,您可以尝试在http://php.net/manual/en/book.apc.php上安装像APC这样的optcode缓存。 It will speed up your PHP. 它会加速你的PHP。 (Zend Optimizer is also available at: http://www.zend.com/en/products/guard/runtime-decoders ) (Zend Optimizer也可在以下网址获得: http//www.zend.com/en/products/guard/runtime-decoders

If your bottleneck is the database where the items in the dropdown is coming from, you may want to try setting MySQL to cache the results. 如果您的瓶颈是下拉列表中的项目所在的数据库,您可能需要尝试设置MySQL来缓存结果。

You may also want to try an alternative dropdown that uses AJAX to populate the dropdown as the user scrolls down, a few records at a time. 您可能还想尝试使用AJAX在用户向下滚动时填充下拉列表的替代下拉列表,一次只能记录几条记录。 You could also create it as a text field that prompts the user for possible matches as they type. 您还可以将其创建为文本字段,在用户输入时提示用户输入可能的匹配项。 These things may be faster. 这些事情可能会更快。

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

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