Smarty data cache and template caching
What is a template cache?
Smarty will replace the template written in the Smarty syntax with PHP format for PHP parsing, PHP data and interface separation.
When we modify the template file every time, the corresponding template cache will be regenerated once.
But this is just the template cache, the program still need to get data from the database and operation, and directly in PHP written out of the interface without any difference.
How do I implement a true data cache?
Smarty supports true data caching, that is, conditional compliance will return a cached static file directly to the browser and will not be retrieved from the database.
You need to add the following settings:
$smarty->caching = true;//Open Cache $smarty->cache_dir = ' ... /cache/';//cache directory, customizable
When we run the program, we will find the cache directory to generate some HTML files, open can be seen are static HTML page
Local cache
The Insert function is not cached by default. And this property cannot be modified.
Cache1.htm{insert name= "MyTime"}cache1.phpfunction insert_mytime () { return date ("y-m-d h:i:s");}
The Smarty_block function can also implement local caching
{Blockname} current time: {$smarty. Now}{/blockname}
Add an ID number to the cache and a template to implement multiple caches
$smarty->display (' template file ', cache ID); Create a cache with ID $smarty->clear_all_cache (); Clears all cache $smarty->clear_cache (' template files ');//Clears the cache $smarty->clear_cache (' template file ', cache ID) of the specified template file;//clears the cache for the specified ID
For example, you can generate different cache pages based on the ID number:
$id = "id=". $_get[' id '); $smarty->display ("Index.tpl", $id);