Php-Smarty-cache 1 (25), phpsmarty-cache 25_PHP tutorial

Source: Internet
Author: User
Php-Smarty-cache 1 (25), phpsmarty-cache 25. Php-Smarty-cache 1 (25), phpsmarty-cache 25 I. cache principle: IE: save resource files to the local Smarty: save the cache to the server and compile the cache. static 2. Why php-Smarty-cache 1 (25) and phpsmarty-cache 25?

I. cache principle:

IE: save the resource file to the local device.

Smarty: save the cache to the server

Compile <cache <static

II. why cache technology

1. improve website access speed

2. reduce the pressure on web servers

3. reduce the pressure on database servers

3. set cache

1. in smarty, the following code is used to set the cache:

L $ smarty-> setCacheDir ($ cache_dir)

Set cache Directory

Cache files generated in the future will be automatically stored in this directory.

L $ smarty-> caching = true

Enable caching

If you want to use the caching technology, you must enable the caching mechanism before calling the display method (do not enable clearing the cache)

L $ smarty-> cach_lifetime = 3600

Set the cache validity period (lifecycle ),

The default value is 3600 seconds (one hour). you can adjust the value as needed.

2. Code:

Program code:

Template code:

After running the above program, we found that two folders are automatically generated under the current directory:

Templates_c and cache indicate the compiling and cache directories respectively, and they are automatically created.

However, we strongly recommend that you do not rely too much on the automatic creation function, because in some operating systems, you do not have the permission to create by default. Therefore, we recommend that you manually create these two directories before running them.

3. observe the differences between the two files:

Compile the file:

Cached file:

Through observation, we found that in the compilation file, the value of the template variable is dynamically read through the PHP program each time.

In the cached file, the values of these two variables are directly written to the file.

4. cache is a pointer to the template cache

We can try to create other php programs so that these programs can display the above template again and find that these programs only run the cached files generated by the above programs.

5. working principle of Smarty cache:

6. two related attributes

$ Smarty-> compile_check

The default value is true, indicating that the template is changed during each execution. if the template is changed, the system reads the file again and generates the compilation and cache files. However, after a project is launched, the template is basically unchanged or not dynamic. every time the template is determined, it will affect the system performance. Therefore, after the project is launched, we recommend that you disable this option (set to false ).

$ Smarty-> force_compile

The default value is false, indicating that the cache is expired during each execution. if the cache is not expired, the content of the cached file is used directly. However, when a project is developed, the cache will affect the programmer's most direct judgment on the program and will affect the observation of the latest data. We recommend that you set this option to true to re-generate the compilation and cache files each time you run them.

IV. check cache

$ Smarty-> isCached ("tpl. tpl ")

Check whether a template has cached files

Practical application:

5. clear cache

For example, in a mall system, the price of this product is 1000.00. now, the mall has a discount. If cache is used, the product price is still the original price. In this case, we need to delete the previous cached files.

L $ smarty-> elastic ache ("tpl. tpl ")

Clears the cache file of a template.

L $ smarty-> clearAllCache ()

Clear cache files for all templates

If the information of a product changes, the cache file of the product needs to be regenerated.

Generally, to modify data, you need to regenerate the cache file (delete the original cache file ).

Expires (25), phpsmarty-cache 25 I. cache principle: IE: save the resource file to the local Smarty: save the cache to the server and compile the cache. static 2. why...

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.