php-smarty-Cache 1 (25)

Source: Internet
Author: User
First, the principle of caching:

IE: Saving a resource file to a local

Smarty: Saving the cache to the server

Compile < Cache < static

Second, why the use of caching technology

1. Improve website access speed

2. Reduce Web server pressure

3, reduce the database server pressure

Third, set the cache

1, in Smarty, set the cache mainly through the following lines of code:

L $smarty->setcachedir ($cache _dir)

Set the cache directory

Future generated cache files are automatically placed in this directory

L $smarty->caching=true

Turn on caching mechanism

As long as the caching technique is used, the caching mechanism must be turned on before the display method call (clear cache does not open)

L $smarty->cach_lifetime=3600

Set the cache validity time (life cycle),

The default is 3,600 seconds (one hour) and can be adjusted appropriately

2, the code part:

Program code:

Template code:

After running the above program, we found that in the current directory, automatically generate two folders:

where Templates_c and cache represent compiled and cached directories, respectively, and they are automatically created

However, it is strongly recommended that you do not rely too much on the auto-create feature, because in some operating systems, you do not create permissions by default, so we recommend that we manually create these two directories before running

3. Observe these two file differences:

To compile the file:

Cache files:

By observing, we find that in the compiled file, the value of the template variable is dynamically read by the PHP program every time

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

4, cache refers to the cache for the template

We can try to create other PHP programs, let these programs display the above template again, found that these programs will only run directly from the above program generated cache files

5, Smarty Cache working principle:

6, two related properties

$smarty->compile_check

The default value is true, which means that the template is checked for changes every time it executes, and if it changes, it is read again, and the compiled and cached files are generated. However, after a project is on-line, the template will basically not be changed or do not move frequently, each judgment will affect the performance of the system, so after the project goes live, we recommend that you turn off this option (set to false).

$smarty->force_compile

The default value is False, which means that each execution checks whether the cache is out of date and, if not, uses the contents of the cached file directly. However, when a project is developed, caching can affect the programmer's most direct judgment on the program, which can affect the observation of the latest data. It is recommended that you set this option to true to regenerate the compiled and cached files each time the runtime is run.

Iv. checking the cache

$smarty->iscached ("Tpl.tpl")

Check if a template has a cached file

Practical application:

Five, clear the cache

For example, in a mall system, the price of this product is 1000.00, the mall is now discounted, hit 70 percent. If the cache is used, but the commodity price is still the original price. At this point we need to delete the previous cache files.

L $smarty->clearcache ("Tpl.tpl")

Clear the cache file for a template

L $smarty->clearallcache ()

Clear cache files for all templates

A change in the product information, the need to regenerate the product's cache file

In general, modifying the data requires rebuilding the cache file (to delete the original cache file).

The above describes the php-smarty-cache 1 (25), including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.