+1 954 371 4486

How to Organize Caching of Web Pages

What for Do We Cache Web Pages

Caching is an effective way of optimizing the speed of the site operation. Almost all modern websites have dynamic pages. With each visit to this page the server generates it again. In most cases, if the page content is not updated constantly, this turns to be unnecessary waste of resources.

For sites with a minimalist design and low traffic regeneration of pages is not a problem for the server. But when we speak about popular sites with a large traffic it’s necessary to notice that the server load becomes tangible, and may exceed its resources which are not unlimited. Big load for the server, the PHP interpreter, MySQL database server first of all leads to “clogging” of RAM and CPU of the server, reducing the speed of its response, and the pages load time increases.

In the best cases it has a negative impact on usability and makes people feel uncomfortable, and in worst occasions it leads to hang-up of server processes (PHP, MySQL). In such cases, caching is applied as a way of reducing the load and optimizing the speed of the server responses.

Website Caching. How To.

Having being cached dynamic web pages generated by PHP are saved as static HTML pages or text files, or JSON structures. They are stored on the server in a special folder, for example /cache/, or /tmp/. Each time the user requests the address of the source page its stored static HTML copy opens. This is much faster than if turning to the source (PHP script), as in this case, one and the same page is not generated anew each time.

The server PHP process is not started. If the cache is stored as text or json files, then the PHP process starts and it parses a text or JSON structure to represent it in the form of HTML. To run PHP you need some time. And in our work each fraction of a second is valuable :).

In fact, the cache is a high-speed buffer of exchange between the website user and the server. When the user addresses a web page, the cache is the first location where the system searches for it. And only if the system does not find it there, server processes and PHP scripts are started, database queries for generating dynamic pages are made.

Some Problem with Pages Caching

Caching significantly optimizes the speed of the site operation, reduces the server load, but relates to the problem of content obsolescence. Sooner or later, the content is updated, and the cached copy of the page, which is issued to a user by request, may be out of date. To solve this problem it is necessary to provide a “reset” of the cache. This can be an additional PHP script that will be run by the cron (cron is a scheduler in UNIX-like operating systems). This script should be run by appropriate intervals in accordance with your site content updating rate. It can be once a day, once a week. If you content is updated once a month, then, obviously, your site traffic is low and it’s no use contacting the cache.

An Example of Caching Using PHP

if($user) { 

    $myusers = $user; 

} else { 

    $myusers = Users::getUsersCache($where, 5, '', $order_by, $limit); 

    $cacheData->write($my_cache_dir, $myusers); 

}

ASK US A QUESTION: