Smarty template engine cache mechanism details, smarty template engine details

Source: Internet
Author: User

Smarty template engine cache mechanism details, smarty template engine details

This article describes the cache mechanism of the Smarty template engine. We will share this with you for your reference. The details are as follows:

First of all, there are two different concepts about smarty caching and compilation. By default, the compilation is started, and the caching mechanism needs to be manually enabled. The files compiled by smarty are still PHP files, therefore, it is compiled during execution. If the database is involved, it is still necessary to access the database, so the cost is not small, so the smarty cache is needed to solve the problem!

1. enable global Cache

$ Smarty-> cache_dir = "/caches/"; // cache directory $ smarty-> caching = true; // enable cache, the cache is invalid when it is flase $ smarty-> cache_lifetime = 3600; // The cache time

2. One page uses multiple caches

For example, an article template page generates multiple article pages. Of course, the page is cached as many pages, which is easy to implement. You only need to set the second parameter in the display () method and specify a unique identifier. The following php code:

$smarty->display('index.tpl',$_GET["article_id"]);

As shown above, an article page is cached by the id of the second parameter article.

3. reduce overhead for Cache

That is to say, the cached page does not need to be processed by database operations. You can use the is_cached () method to determine the operation!

If (! $ Smarty-> is_cached ('index. tpl ') {// call database} $ smarty-> display ('index. tpl ');

4. Clear Cache

Generally, cache is not enabled during the development process, because the output results remain unchanged during the cache time, but enabling the Cache during the application process can greatly improve web performance. The cache clearing method is as follows:

Clear_all_cache (); // clear all cache clear_cache ('index. tpl '); // clear the index. tpl cache clear_cache ('index. tpl ', cache_id); // clear the cache of the specified id

5. Disable local cache

If one part of a page is cached, and the other part does not need to be cached, you can do so. For example, to display the user login name, you need to disable the cache. smarty provides the following three solutions:

(1) using part of the insert template is not cached

Defines the handler to be used for an inser label. The function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the name of insert, that is, if the function you define is insert_abc, the method used in the template is {insert name = abc}

The parameter is passed in through $ params

You can also create an insert plug-in. The file name is insert. xx. php, and the function name is smarty_insert_aa ($ params, & $ smarty). xx is defined as the same as above.

(2) $ smarty-> register_block ($ params, & $ smarty) prevents a part of the entire page from being cached

Define a block:

Smarty_block_name ($ params, $ content, & $ smarty) {return $ content;} // name indicates the region name

Register block:

$ Smarty-> register_block (name, smarty_block_name, false); // The third parameter false indicates that the region is not cached.

Template syntax:

{Name} content {/name}

Write the block plug-in:

Step 1: Define a plug-in function: block. cacheless. php, which is placed in the smarty plugins directory.

The contents of block. cacheless. php are as follows:

<?phpfunction smarty_block_cacheless($param, $content, &$smarty) {return $content;}?>

Step 2: write programs and templates

Example: testCacheLess. php

<?phpinclude(Smarty.class.php);$smarty = new Smarty;$smarty->caching=true;$smarty->cache_lifetime = 6;$smarty->display(cache.tpl);?>

Template used: cache. tpl

Cached: {$ smarty. now} <br> {cacheless} Not cached: {$ smarty. now} {/cacheless}

Run it now and find that it does not work, and the two lines of content are cached.

Step 3: rewrite Smarty_Compiler.class.php(Note: This file is very important. Back up the file first to restore it if necessary)

Search:
Copy codeThe Code is as follows: $ this-> _ plugins [block] [$ tag_command] = array ($ plugin_func, null, true );

Modify:

if($tag_command == cacheless) $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, false);else $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);

You can also change the last parameter of the original sentence to false to disable the default cache.

(3) Use register_function to prevent the plug-in from outputting data from the cache.

Index. tpl:

<div>{current_time}{/div}index.php:function smarty_function_current_time($params, &$smarty){  return date("Y-m-d H:m:s");}$smarty=new smarty();$smarty->caching = true;$smarty->register_function('current_time','smarty_function_current_time',false);if(!$smarty->is_cached()){  .......}$smarty->display('index.tpl');

Note:

Define a function in the format of smarty_type_name ($ params, & $ smarty)
Type is function

Name is the name of the custom tag. Here is {current_time}

The two parameters are required, even if they are not used in the function. The functions of the two parameters are the same as those of the preceding two parameters.

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.