简体   繁体   中英

Caching HTML pages server-side to decrease network load

I work for a small school department managing its website code. Most of the code is written in php and connects to a few seperate database servers to generate the HTML page. Since the databases are not updated very often, I was wondering Is there a way to have php create the HTML page and save that page server side? Basically, still have php generate the page, but only have the script invoked when there is a change to the database, not invoked for every request. This way the php generated HTML page is saved on the server and can be served to many clients before it has to be regenerated.

I know a request normally works like this:

request > web server > php invoked > connect to db server > php generate html with db info> html returned to client

I am looking for something more like this:

db updated > web server generates html > html page cached for requests

that way every time there is a request, it goes like this instead:

request > cached html page stored locally is returned to client

Any advice on this topic would be appreciated. It seems like this model would save a lot of processor time and our network from a lot of traffic.

Excluding solutions like caching from your webserver, there are two major ways in which you can approach this, largely depending on what the reason is you want to cache.

  1. The PHP scripts are causing a heavy load on the server and you're unable to solve that: no replacement or upgrade from hardware is an option, nor is optimizing your PHP code. This is the only scenario in which you could generate static (HTML) content and serve it to your users. I sincerely doubt it's the case, as you lose authentication etc.

  2. The (external) database servers experience a heavy load. Consider caching the results in your PHP scripts and use a relatively high expire time for the entries given that there are few changes to the data.

Assuming the cause of the second option is the reason for asking, consider looking into several caching modules for PHP, like Fast Cache . But again, it depends on the cause of looking into caching. Typically, generating static content based on database content is only a viable solution if the generation part takes too long to wait for.

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