简体   繁体   English

如何加快处理多个大型数组的缓慢加载PHP页面的速度?

[英]How to speed up a slow loading PHP page that is processing several large arrays?

How one would go about loading this sort of site properly. 如何正确加载此类网站。 Currently it is taking a page roughly 10 second to load, and that is obviously far too long. 目前,加载页面大约需要10秒钟的时间,这显然太长了。

Some things to just assume for now: 现在要假设的一些事情:

  1. The server is not causing the slowness 服务器没有造成速度慢
  2. Media like images aren't causing the slowness 图像之类的媒体不会造成速度缓慢
  3. The slowness is due to the poor way I currently have things set up in my current 'pass' at learning HTML/CSS/PHP (i don't know what I'm doing, but im trying to learn as I go and redoing everything via full 'passes' as I gain knowledge to reduce code etc and im due for another pass) 之所以缓慢,是因为我目前在学习HTML / CSS / PHP的“通行证”中设置的方式很糟(我不知道自己在做什么,但是我试图边走边学并重做所有事情通过完整的“通过”,因为我获得了减少代码等的知识,而我又要通过另一遍了

Basically I am making a site that people can goto to get useful information about a game. 基本上,我正在建立一个网站,人们可以去那里获得有关游戏的有用信息。 This site uses the games API to pull data that is mainly in the form of arrays. 该站点使用游戏API提取主要是数组形式的数据。

Here is a sample at the starter array (Career): http://pastebin.com/X33k0NKw 这是入门级数组(职业)的示例: http : //pastebin.com/X33k0NKw

One of the arrays comes from that (One of the heros): http://pastebin.com/fA4tjgxd 数组之一来自那个(英雄之一): http : //pastebin.com/fA4tjgxd

The item array that comes from that (also large): http://pastebin.com/gaqba3i7 来自那个的项目数组(也很大): http : //pastebin.com/gaqba3i7

Now from the item array I further get file that gives me data that I can use in tooltips. 现在,从item数组中进一步获取文件,该文件提供了可以在工具提示中使用的数据。

Now I have all this data coming from the server, and its taking 10~sec to all get finished. 现在,我有所有来自服务器的数据,并且要花10秒钟才能完成所有工作。 Way too long. 太久了 You're thinking I need to set up a database and get the data in there. 您在想我需要建立一个数据库并在那里获取数据。 And I plan to. 我打算。 However I can't possibly be doing this correctly, so I want to be able to get this done right before I start doing the MySQL stuff. 但是我可能无法正确执行此操作,因此我希望能够在开始执行MySQL之前正确完成此操作。

So my question is what should I be searching for to help with this sort of thing? 所以我的问题是我应该寻找什么来帮助这类事情?

My rule of thumb when dealing with any RESTful API data is this: A RESTful API is not a database & should never be used for direct real-time calls for data on request. 在处理任何RESTful API数据时,我的经验法则是:RESTful API不是数据库,并且切勿将其用于对请求数据的直接实时调用。 They are simply not built for speed. 它们根本不是为提高速度而构建的。 Especially for arrays of data this large. 特别是对于如此大的数据数组。 The data array is simply so large the combined weight of fetching the data & acting on the data is slowing down your page. 数据数组是如此之大,以至于取回数据和对数据进行操作的总权重降低了页面的速度。

The solution? 解决方案? If the RESTful API calls are filled with data you need to cache those in your local database—or on the file system—and then act on the data you have stored locally in the database or file system. 如果RESTful API调用中填充了数据,则需要将它们缓存在本地数据库或文件系统中,然后对本地存储在数据库或文件系统中的数据进行操作。 Even if the caching time between accesses of the API is 5 seconds, the speed of accessing them locally will still be great. 即使两次访问API之间的缓存时间为5秒,在本地访问它们的速度仍然会很高。

So that all means your setup is like this: 这样就意味着您的设置是这样的:

[Your Page] -> [Your Code] -> [Your Call to the RESTful API]

You need to add some local storage/caching layer: 您需要添加一些本地存储/缓存层:

So that all means your setup is like this: 这样就意味着您的设置是这样的:

[Your Page] -> [Your Code] -> [Your Local Storage of API Data] -> [Your Call to the RESTful API to Update Local Storage]

Now how long you cache the data locally in a database or on the file system is your call. 现在,您需要在数据库或文件系统中本地缓存数据多长时间。 I have dealt with RESTful APIs where data only needs to be updated once a day as well as systems where the caching adds up to maybe just a minute. 我已经处理过RESTful API,其中每天只需要更新一次数据,以及涉及缓存总计可能只有一分钟的系统。 But even if the caching is a minute, that could be enough breathing room to help your system deal with the size of the data. 但是,即使缓存是一分钟,也可能有足够的喘息空间来帮助您的系统处理数据量。

And past all of that, you should also consider adding a AJAX layer to your front-end functionality that would be encompassed in the [Your Page] areas of those short diagrams I have above. 除此之外,您还应该考虑将AJAX层添加到您的前端功能中,该功能将包含在我上面这些简短图表的[Your Page]区域中。

AJAX stands for Asynchronous JavaScript. AJAX代表异步JavaScript。 And the general concept for data issues like this is the core page HTML is rendered by standard methods in whatever language you are using. 诸如此类的数据问题的一般概念是HTML的核心页面,HTML是通过标准方法以您使用的任何语言呈现的。 Then after the HTML loads, a JavaScript call is made on some basic—time interval, user interaction, a mix of both, something else…—which requests data via JSOP from your core application framework & then the JavaScript updates elements on the page based on that data. 然后,在HTML加载后,会在一些基本的基础上进行JavaScript调用(时间间隔,用户交互,两者的混合……),这些调用会通过JSOP向核心应用程序框架请求数据,然后JavaScript会根据页面上的内容更新元素在那个数据上。

So of you were yo break down the [Your Page] functionality it could be like this: 因此,您可能会破坏[Your Page]功能,就像这样:

[Your Server Side Script Renders a Page] -> [Browser Loads HTML, CSS & JavaScript] -> [JavaScript AJAX Calls Adjust the Content in Your Renered HTML] [您的服务器端脚本呈现页面]-> [浏览器加载HTML,CSS和JavaScript]-> [JavaScript AJAX调用调整已调整HTML的内容]

The beauty of this is your core page is never reloaded again. 这样做的好处是您的核心页面不再需要重新加载。 It stays there like a framework waiting to be filled with content. 它像一个等待内容填充的框架一样呆在那里。 And AJAX calls fill that framework with content. 而AJAX调用则用内容填充了该框架。

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

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