Smarty's cache operation tips _ PHP Tutorial

Source: Internet
Author: User
Smarty's caching skills. 1. to enable the smarty cache when using the cache, set caching to true and specify cache_dir. cache_lefetime is used to specify the cache survival time. the unit is seconds.
To enable the smarty cache, set caching to true and specify cache_dir.
Use cache_lefetime to specify the cache survival time, in seconds
To generate multiple different caches for the same page, add the second parameter cache_id to display or fetch, for example, $ smarty-> display (index. tpl, $ my_cache_id); this feature can be used to cache different $ _ GET

II. clear cache

Clear_all_cache (); // clear all caches
Clear_cache (index. tpl); // clear the cache of index. tpl
Clear_cache (index. tpl, cache_id); // clear the cache with the specified id

III. use custom cache

Set cache_handler_func to process cache using custom functions
For example:
$ Smarty-> cache_handler_func = "myCache ";
Function myCache ($ action, & $ smarty_obj, & $ cache_content, $ tpl_file = null, $ cache_id = null, $ compile_id = null ){
}
This function usually uses the root action $ action to determine the current cache operation:
Switch ($ action ){
Case "read": // read the cached content
Case "write": // write cache
Case "clear": // clear
}
Generally, md5 ($ tpl_file. $ cache_id. $ compile_id) is used as the unique cache_id.
If necessary, use gzcompress and gzuncompress to compress and decompress

IV. disable local cache

There are several ways to invalidate the cache in some regions (only cache is required:
Insert:
Defines the processing function to be used for an insert tag. the function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the insert name, 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.
Register_block:
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:
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:

Function smarty_block_cacheless ($ param, $ content, & $ smarty ){
Return $ content;
}
?>

2) write programs and templates
Example: testCacheLess. php

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

Template used: cache. tpl

Cached: {$ smarty. now}

{Cacheless}
No cache: {$ smarty. now}
{/Cacheless}

To enable the smarty cache, set caching to true and specify cache_dir. use cache_lefetime to specify the cache survival time. the unit is seconds to generate more for the same page...

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.