Cache application in php smarty template engine

Source: Internet
Author: User

1. Smarty cache Configuration:
$ Smarty-> cache-dir = "directory name"; // create a cache directory name
$ Smarty-> caching = true; // enable cache. If it is false, the cache is invalid.
$ Smarty-> cache_lifetime = 60; // cache time, in seconds
2. Use and clear the Smarty Cache
$ Marty-> display ("cache. tpl", cache_id); // create a cache with an ID
$ Marty-> clear_all_cache (); // clear all caches
$ Marty-> clear_cache ("index. php"); // clear the cache in index. php
$ Marty-> clear_cache ("index. php', cache_id); // clear the cache of the ID specified in index. php.
3. Local cache of Smarty
First, the insert _ function is not cached by default. This attribute cannot be modified.
Usage: Example
Index. php,
Function insert_get_time (){
Return date ("Y-m-d H: m: s ");
}
In index.html,
{Insert name = "get_time "}

Second: smarty_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:
<? Php
Function smarty_block_cacheless ($ param, $ content, & $ smarty ){
Return $ content;
}
?>
2) write programs and templates
Example: testCacheLess. php
Copy codeThe Code is as follows:
<? 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} <br>
{Cacheless}
No cache: {$ smarty. now}
{/Cacheless}
4. 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

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.