The principle of the library cache is as follows when it is enabled.
The first time the graph script is called everything will be as usual, the script will run and in the end the script sends back the image to the browser. However if the caching is enabled JpGraph will automatically have stored a copy of the generated image in the cache directory.
When the graph script is executed the next time it checks to see if an image corresponding to this graph script has already been generated and is available in the cache directory.
If the image is available in the cache directory the library check to see how old the image is. If the images is older than a specified limit than it assumes that the image is out dated and runs the graph script as usual and makes sure the newly generated image is stored in the cache directory. Hence replacing the outdated image.
If the image in the cache directory was current (i.e. not too old) it is read and send back to the clients (e.g. Web-browser) without the rest of the graph script being executed.
From the above description there are a couple of parameters that should be specified, the name to use when the image is stored and the timeout value when the image is considered too old, i.e. how long was it since the image was generated.
The first parameter, the filename, can be either manually specified or the library can create a filename based on the name of the graph script.
Both these parameters are specified in the initial Graph()
call where
a new graph instance is created. A basic example of this is shown in Example 9.1.
Example 9.1. Using an automatic cache filename and a 60min timeout of the cached images.
1 2 3 4 5 6 7 8 9 | // ... includes $graph = new Graph($width, $height, 'auto', 60); // ... rest of the graph script $graph->Stroke(); |
The code in Example 9.1. will use an
automatic filename for the cached image and a make the image valid for 60 minutes.
This means that if the script is called again, within 60minutes, it will return the
image just after the initial Graph()
call and not execute any more
lines of code in the script.
For basic usage this is all that is necessary, enable the cache in the settings and supply a filename and a timeout value. The rest of the logic is handled by the library.
If you want the timeout value to be "forever" then you can specify a
"0
" as the timeout value (or leave the parameter blank). To
regenerate the image you will have to manually remove the image files from
the cache. This removal could for example be handled by a nightly
cron-job.