Smarty Template Configuration
| The code is as follows |
Copy Code |
$smarty->cache_dir = './cache/'; Set up a folder where cached files are stored $smarty->caching = 1; Turn on Cache 0, false on behalf of off | Not 0 digits, true represents open $smarty->cache_lifetime = 3600; The unit is seconds (if filled-1 is never expired) |
The easiest thing to do is to turn on the cache.
First, use the cache
To turn on the Smarty cache, simply set the caching to True and specify Cache_dir.
Use Cache_lefetime to specify the cache lifetime, in seconds
To generate multiple different caches for the same page, add a second parameter cache_id to display or fetch, such as $smarty->display (' Index.tpl ', $my _cache_id); This attribute can be used for different $_ Get to do a different cache
Second, clear the cache
| The code is as follows |
Copy Code |
Clear_all_cache ()//Clear All caches Clear_cache (' index.tpl ')//clear INDEX.TPL Cache Clear_cache (' Index.tpl ', cache_id);//clear cache of specified IDs |
Third, use the custom caching method
Set Cache_handler_func to process caching using custom functions
Such as:
| The code is as follows |
Copy Code |
$smarty->cache_handler_func = "Mycache"; function Mycache ($action, & $smarty _obj, & $cache _content, $tpl _file=null, $cache _id=null, $compile _id=null) { }
|
This function is generally rooted in the $action to determine the current operation of the cache:
| The code is as follows |
Copy Code |
Switch ($action) { Case "read"://Read cache content Case "Write"://write Cache Case "clear"://Empty }
|
General use of MD5 ($TPL _file. $cache _id. $compile _id) as the only cache_id
If necessary, use gzcompress and gzuncompress to compress and decompress
One of the small details need attention
Smarty cache defaults are invalid, first you need to open the Smarty cache.
| The code is as follows |
Copy Code |
$smarty->caching = 1; |
Second, set the life cycle of the cache. When the page is accessed during the lifecycle, Smarty returns the cached content directly to the user. Rather than some other language like the first compile run, link lookup database, organize data to generate page content, and finally return to the user. Obviously can save a lot of processing overhead.
| The code is as follows |
Copy Code |
| $smarty->cache_lifetime = 86400;// |
Number unit: seconds, free to set
Third, display the page content.
| The code is as follows |
Copy Code |
$smarty->display (' Index.tpl '); |