-
- $smarty->cache-dir="directory name"; //Create cache directory name
- $smarty->caching=true; //Enable caching. When it is false, the cache will be invalid
- $smarty- >cache_lifetime=60; //Cache time, unit is seconds
Copy code
2. Use and clear Smarty cache
-
- $marty->display("cache.tpl",cache_id); //Create a cache with 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 specified ID in index.php
Copy Code
3. Smarty’s local cache
The first one: The insert_ function does not cache by default, and this attribute cannot be modified.
How to use: example
index.php,
-
- function insert_get_time(){
- return date("Y-m-d H:m:s");
- }
-
Copy the code in
index.html,
Second: smarty_block
Define a block:smarty_block_name($params,$content, &$smarty){return $content;} //name represents the area name
Register block:$smarty->register_block('name', 'smarty_block_name', false); //The third parameter false means that this area is not cached
Template writing: {name}content{/name}
Written as a block plug-in:
1) Define a plug-in function: block.cacheless.php and place it in the smarty plugins directory
The content of block.cacheless.php is as follows:
-
- function smarty_block_cacheless($param, $content, &$smarty) {
- return $content;
- }
- ?>
-
Copy code
2) Write program and template
Sample program: testCacheLess.php
-
- include('Smarty.class.php');
- $smarty = new Smarty;
- $smarty->caching=true;
- $smarty->cache_lifetime = 6;
- $smarty->display('cache.tpl');
- ?>
-
Copy code
Template used: cache.tpl
Already cached: {$smarty.now}
{cacheless}
Not cached:{$smarty.now}
{/cacheless}
4. Custom cache
Set cache_handler_func to use a custom function to handle the cache
like:
-
- $smarty->cache_handler_func = "myCache";
- function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
- }
-
Copy code
This function is generally based on $action to determine the current operation of the cache:
-
- switch($action){
- case "read"://read cache content
- case "write"://write cache
- case "clear"://clear
- }
-
Copy the code
Generally use md5 ($tpl_file.$cache_id.$compile_id) as the only cache_id
If necessary, use gzcompress and gzuncompress to compress and decompress.
|