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