Jpgraph cache system usage instructions

Source: Internet
Author: User
In the previous phpjpgraph tutorial, we mentioned that the burden of using jpgraph to generate a large number of charts is very heavy. There are two ways to reduce the load of using jpgraph on WEB servers: provides dedicated jpgraph servers for chart generation, or uses a reasonable Cache mechanism to relieve pressure. In most PHP jpgraph tutorials, we have mentioned that using jpgraph is very heavy when generating a large number of charts, there are two ways to reduce the load on the WEB server by using jpgraph: providing a dedicated jpgraph server for graph generation, or using a reasonable Cache mechanism to relieve pressure. For most PHP developers, using the cache mechanism to reduce the jpgraph load is the most practical method. The jpgraph class library supports two methods to significantly improve performance: using the built-in cache system described in this tutorial and using the PHP accelerator NuSphere PHP accelerator. The following describes the cache system in the jpgraph class library:


Diagram of jpgraph class library cache mechanism



  Enable jpgraph class library caching system

Enabling or disabling the jpgraph cache system is controlled by three definitions in the jpg-config.php

1
2
3
4
5
1. DEFINE ("USE_CACHE", true)

2. DEFINE ("READ_CACHE", true)

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

In the preceding three switches, USE_CACHE is the master switch for enabling the cache system. it must be set to true.

The second switch READ_CACHE rarely needs to be changed. The second switch basically tells JpGraph whether to constantly query the content in the cache. If this parameter is set to false and the master switch is set to true, a new image file is always generated in the cache and the new image is sent back to the browser.

The third definition is not a real switch, but a specified directory to inform the jpgraph class library of the directory used as the cache Directory (that is, the cache image storage location), the cache Directory (CACHE_DIR) you can set it to any directory, but remember that the cache directory must be readable and writable for running PHP programs.

  Jpgraph cache directory prompt: The given directory name must be an absolute directory path rather than the relative root directory of a file.

  Permission restrictions on cached files

Note: This section applies only to Unix derivative systems that have the concept of group and file ownership.

Two additional settings allow you to control the permission settings of the combined file.

1
2
3
DEFINE ('cache _ FILE_GROUP ', 'WWW ')

DEFINE ('cache _ FILE_MOD ', 0664)

CACHE_FILE_GROUP specifies which group should be used to cache image files. If it is null, the group will be the same as the PHP program running. CACHE_FILE_MOD specifies the file permissions for image files. If it is null, the default permissions used by PHP will be set on the file.

When PHP runs on the command line (using the CLI version of PHP) file, permissions and groups are usually set to run one of PHP suer. Remember that common users are not allowed to change the "WWW" directory (the default directory of Apache2 ).

  Use cache in programs

When jpgraph class library is used to enable caching, the following principles apply:

When the chart program is called for the first time, as usual, the script will run to finally return the image to the browser. However, if caching is enabled, JpGraph automatically obtains copies of the images stored in the cache directory.

When the chart script is executed next time, check whether the image corresponding to this chart script has been generated in the cache directory.

If images stored in the cache directory are available, the jpgraph class library checks the new and old degrees of images. If the new or old image size exceeds the limit, the chart script will generate the latest image as usual and store it in the cache directory.

If the image in the cache directory is current, the remaining part of the script is not executed, but the image is read and sent to the browser.

The preceding description shows the parameters that need to be specified. for example, the name of the image to be stored and the image is identified as obsolete when the time exceeds.

The first parameter, file name, can be manually specified or the jpgraph class library can automatically create a file name based on the name of the chart script.

All these parameters are specified by calling Graph () when initializing a chart instance. The following example uses the cache file name automatically defined by the jpgraph class library, and sets that the cached image is valid for only 60 minutes. if the cache file name expires, it must be updated.

1
2
3
4
5
6
7
8
9
//... Des
 
$ Graph = new Graph ($ width, $ height, 'auto', 60 );
 
//... Rest of the graph script
 
$ Graph-> Stroke ();
?>

This means that when the program is called again, the image will be directly returned after the initialization function Graph () is called within 60 minutes without executing other code in the script. It is necessary to use the basic cache methods. enable the cache and provide the file name and the supervalue. The rest are processed through the class library.

  Jpgraph cache usage prompt: If you want to "never" time out, you can specify a timeout value of 0 (or leave the parameter blank. To regenerate an image, you will have to manually delete the image file from the cache.

  Manual control of cached images

These functions have been added in 3.0.5 and may be implemented in earlier versions, but more code is required. If you need this function, update it to the latest version.

Manual intervention is required.

1. check whether the cached image exists in the cache and is valid for a period of time.

2. if yes, send the image file to the browser. If the cache file times out, you need to construct a chart as usual and automatically store the image in the cache. The following example illustrates this situation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ Width = ...;
$ Height = ...;
$ Cachefilename = GenImgName ();
$ Graph = new Graph ($ width, $ height );
 
// Check whether the cached file exists and is valid
$ Valid = $ graph-> cache-> IsValid ($ cachefilename );
 
If ($ valid ){
// If it is effective, you can do anything, for example, implement it in the following function, and then return the chart to the browser.
DoSomeProcessingIfNecessary ();
 
$ Graph-> cache-> StreamImgFile ($ graph-> img, $ cachefilename );
} Else {
// If the cached file does not exist or is invalid, you need to build a chart as usual.
 
// Tell jpgraph that we need to cache this file
$ Timeout = ...;
$ Graph-> SetupCache ($ cachefilename, $ timeout );
 
// Remaining part of the chart program
...
// Return the chart as usual and store a copy in the cache Directory
$ Graph-> Stroke ();
}
?>

  Use cache client CSIM, The details will be described later. you only need to know about it here.

You can also use the image cache system (CSIM ). The cache system interface is slightly different in this case because the cache needs to store the cached image and the cached image ING.

Due to the increased complexity, the CSIM script does not send back images, but a pre-formatted HTML page. There are two main differences between cache and non-CSIM:

1. the cached version is not stored in the cache directory defined previously. It uses different cache directories.

2. to use the cache system with CSIM, the script must call an exclusive method. CheckCSIMCache () is used to check the cache.

  Cache client CSIM usage prompt:When using CSIM, the Graph: StrokeCSIM () method must be called at the end of the script instead of Graph: Stroke ()

To use CSIM cache, you need to call Graph: CheckCSIMCache (). Because non-CSIM cache is used, the cache file name and option timeout must be provided. If no timeout value is specified, the default timeout value is 60 minutes.

The reason why CSIM does not send images to the browser is that it sends an HTML page, which includes the coordinates of the image ING and the "" tag, which will reference the image script, however, if there is an image in the cache, the static image will be referenced.

According to the definition of CSIMCACHE_DIR installed in the jpgraph class library. It must be a directory that can be accessed by common HTTP/PHP programs. by default, if it is not specified, a csimcache directory will be created. The disadvantage of the default setting is that the directory must be writable by the PHP program. The best way is to reuse the same directory as the standard cache. However, PHP and HTTP servers are also required to be accessed through the htdocs root.

The CheckCSIMCache () method checks the cache of the existing cache version. if it is found, it returns and stops script execution. Therefore, this call must be called before creating a chart instance $ graph = Graph ($ width, $ height.

The general structure of the program script using CSIM and cache is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Require_once ('jpgraph. php ');
 
//... Any necessary des
 
$ Width =...
$ Height =...
$ Graph = new Graph ($ width, $ height );
 
// Check the cache and set a 10-minute validity period
$ Graph-> CheckCSIMCache ('csim _ image1 ', 10 );
 
// If the cache exists, the script execution will be stopped and the cached HTML file will be sent to the browser
 
// Structure and initialization
 
// Returns the HTML file.
$ Graph-> StrokeCSIM ();
?>

Note that no parameters need to be passed to the final function to call StrokeCSIM. CSIM caches two files in the cache Directory. one file is an image and the other file corresponds to the HTML file mapped to the image. HTML files reference static image files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.