Chapter 9. Using the JpGraph cache system

Table of Contents

9.1. Enabling the library cache system
9.2. Permission settings for the cache files
9.3. Using the cache in your script
9.3.1. Manually controlling the cached image
9.4. Using the cache with Client Side Image Maps (CSIM)

To reduce load on the web server JpGraph implements an advanced caching system which avoids the burden of always having to run the full image script. The library supports two primary ways of significantly increase performance, using the built in cache system described in this section and the use of a PHP accelerator described in Chapter 11

Depending on the complexity of the image script, for example if it is doing several DB lookups, the use of the library cache system (which will avoid running the graph scripts completely if possible) can make a for very drastic performance increase.

Figure 9.1shows an overview of the cache system in the library.

Figure 9.1. Library cache principle

Library cache principle


The rationale behind this is the observation that very few graphs are really real-time, i.e. needs to be updated absolutely every time the graphing script is called. For many graphs in a WEB-environment one can often get good precision by restricting the graphs to only be updated, say, a few times each day. Of course, if truly live data is what is needed then the cache system can not be used since then, by the nature of live data, the graph script must be called at each instance to get the latest available data, most probably from a database.

9.1. Enabling the library cache system

The enabling and disabling of the cache system is controlled by three defines in jpg-config.php

  1. DEFINE("USE_CACHE",true)

  2. DEFINE("READ_CACHE",true)

  3. DEFINE("CACHE_DIR","/tmp/jpgraph_cache/")

The first of these, USE_CACHE, is the master-switch which must be set to true to enable the caching system.

The second switch, READ_CACHE very seldom needs to be changed. This second switch basically tells whether or not JpGraph should ever look in the cache. Setting this to false and the master-switch to true would then always generate a new updated image file in the cache and this new image would be send back to the browser. The main use for this (admittedly) strange setting is if you like to have the side effect of the script that a fresh image is always stored in the cache directory.

The third define is not really a switch but a directory specification that tells the library what directory to use as the cache directory (where the cached images are stored).

The cache directory (CACHE_DIR) can be set to an arbitrary directory but the important thing to keep in mind is that the cache directory must be read and writable for the process running PHP.

Note

The directory name given should be an absolute directory path and not a file path relative to the document root.