Managing the cache

to avoid making the server do unnecessary work.

The cache system: how to create a dynamic site that is not too dynamic.

In the preceding lessons we started to build some templates. Our site is going to be a roaring success. Let’s stop for a moment to consider the neurons of our poor computer. In this lesson, there will be nothing amusing and nothing essential either. The bone idle among you can take the opportunity of a short snooze...

Summary for anyone in a hurry: in the calling file, tutorial.php3 amend $delais=0; to $delais=3600;


At the time when a page is requested from SPIP, it first looks to see if it has already been generated before. If the required URL is, SPIP looks in its CACHE/ sub-directory to see if the file exists and, if so, compares the age of the file with the value of $delais specified in the file tutorial.php3.

In our example we had fixed $delais=0; which causes a systematic regeneration of the page each time it is called. Let’s change this to $delais=3600; (expressed in seconds).

Therefore our web page is only regenerated if, when a visitor requests it, the cached version is more than one hour old (i.e. 3600 seconds). If not, SPIP simply reads the contents of the cached file [1], and returns the result without connecting to the database (except to record a "hit" in the statistics).

How should the delays $delais be set so as to optimize the performance of the server? There is no miracle solution, but do not hesitate to set a delay of one day (i.e. $delais=24*3600;) or more for articles and sections. The most significant navigation pages can have a shorter $delais (for example, twenty minutes or an hour) if your site is supposed to react to the frequent publication of news items and syndicated sites. If you are on a shared server, consider the other users and do not set a short delay for pages which seldom change: it wouldn’t be very sensible as, on the large articles or the synopses, the generation of the pages can take a few seconds which only slows down access to your pages.

How can an update be forced? We have just decided to set an extremely long delay on a page and then we notice a spelling mistake. After making the correction in the private area, how can we immediately remove the error from the public site?

-  From the private area, clicking on the "View online" button starts the regeneration of the page corresponding to #URL_ARTICLE or #URL_RUBRIQUE of the article or the corresponding section. This is the most common method of updating.

-  In the "Site Maintenance" section of the private area, the "empty the cache" button erases all the cached files. This is useful if you make substantial modifications and have a very complex site, but avoid it otherwise.

-  Another simple method is to use the "Refresh this page" button which is available to administrators on the public page. This button will only be visible if you have previously used the private area from the same machine which causes a cookie to be written to your machine and identifying you.

Return to context: We return here to the concept of context. If the template is called with a context of id_article, id_rubrique or id_breve another button is visible to you when SPIP detects the administrative cookie, entitled "Modify this article (or section, or news item)", which takes you directly to the corresponding page in the private area to edit the article.

Last details:
-  For obvious reasons, the search engine does not cause the cache to be updated, and the forum pages are refreshed as soon as a new contribution is submitted.
-  The CACHE directory of the site is divided into 16 numbered sub-directories 0, 1, 2... 9, A, B... F , in which the cached files are rendomly distributed; this is called "the cache hash" and is only mentioned for completeness.
-  The cached files are used even if the database is "down", which guarantees the site remains available during any outage of the MySQL server.


[1For the technically minded, this is in fact a PHP include of the corresponding file, making it possible to execute code from the cache.

Author Paul Knight Published : Updated : 26/10/12

Translations : عربي, català, corsu, Deutsch, English, Español, italiano, Português, русский, slovenčina, Türkçe, українська