PHP template engine Smarty cache usage-php Tutorial

Source: Internet
Author: User
PHP template engine Smarty cache usage

  1. $ Smarty-> display ('index. tpl ', $ my_cache_id );

This feature can be used to cache different $ _ GET objects.

II. clear the smarty cache there are several methods to clear the smarty cache:

  1. Clear_all_cache (); // clear all caches
  2. Clear_cache ('index. tpl '); // clear the cache of the specified template index. tpl
  3. Clear_cache ('index. tpl ', cache_id); // clear the cache of the specified template id

3. global cache: a cache page is generated for all pages of the entire website. To set the global cache, first we need to operate on the smarty configuration file, enable the cache, specify the cache file directory, and set the cache survival time. the parameters are as follows: $ smarty-> cache_dir = '. /cache/'; // Set the folder for storing cached files $ smarty-> caching = 1; // When cache is enabled, 0 or FALSE indicates that the cache is disabled. | if the value is not 0, TRUE indicates that $ smarty-> cache_lifetime = 3600 is enabled. // The unit is second (if-1 is set, the cache will never expire) next we will go to the specific php page to set the corresponding name of the specific cache file. the php page can be written as follows:

  1. $ Smarty-> display('list2.html ', md5 ($ _ SERVER ['request _ URI']); // add the URL of the current page (including? All subsequent parameters), perform md5 encryption, and then set the cache file name.

Note: $ smarty-> display ('corresponding template filename ', 'supplemental part of cache filename.

The second parameter is not required. if it is not written, the cached file name is the encrypted file of the template file name. But this will encounter a tricky problem, such as: http://bbs.it-home.org/article.php? Http://bbs.it-home.org/article.php? Id = 7 the two URLs correspond to different contents, but the generated cache file names are all results after article encryption. This results in the user wanting to query different contents, but accessing the same cache file. Therefore, we recommend that you add a secondary parameter to access the url (including? All parameters later) md5 encryption is a more reliable method.

4. some cached files are designated to generate cached files, not all files on the website. The essence of partial cache is that some files are not cached. that is to say, instead of specifying which files are generated for the cache, some specific files are not generated for the cache. Assume that there are three files: http://bbs.it-home.org/index.php // You Need to cache the http://bbs.it-home.org/cate.php // You Need to cache the http://bbs.it-home.org/article.php // You do not need to cache the first two files in the PHP file or you need to write $ smarty-> display ('corresponding to the template filename ', 'Add part of the cached filename. However, in the third file, we need to specify that the cache does not need to be generated. the specific method is to write the following code before the template is specified by display: $ smarty-> clear_cache ('corresponding template filename '); // actually written in $ smarty-> display ('corresponding template filename ') you can use $ smarty-> display ('corresponding template filename ') before or after, but you still need to write it. you don't want to generate a cache, and you don't need the second parameter. Note: the parameters of $ smarty-> clear_cache () and $ smarty-> display () must be consistent.

5. local cache is used to generate a cache in some parts of the same page. In fact, it does not specify the local cache generation, but the local cache generation does not (this is similar to the operation idea of some caches ). Let's take a look at the following example: part of the article. php file:

  1. $ Time = time ();
  2. $ Smarty-> assign ('time', $ lanmuarr );
  3. Function insert_timeget (){
  4. Return time ();
  5. }
  6. $ Smarty-> display('article.html ');

Part of the code of the template file article.html:

  1. {$ Time}
  2. {Insert name = 'timeget '}

Here we will explain the above example: in PHP, we only need to define a UDF named insert _ (note the underline) to customize the name of the UDF, in which the returned value, you can directly call the assign () method on the template page using {insert name = 'custom supplemental name'} without being affected by the cache, and real-time refresh.

If you feel that the Smarty cache is not good enough, you can also use the custom cache. how can you use the custom cache?

You need to set cache_handler_func to use a custom function to process the cache, for example:

  1. $ Smarty-> cache_handler_func = "myCache ";
  2. Function myCache ($ action, & $ smarty_obj, & $ cache_content, $ tpl_file = null, $ cache_id = null, $ compile_id = null ){
  3. Switch ($ action ){
  4. Case "read": // read the cached content
  5. Case "write": // write cache
  6. Case "clear": // clear
  7. }
  8. }

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.