PHP File Cache smarty template application instance analysis, smarty application instance
This document analyzes the smarty template application of PHP File Cache. We will share this with you for your reference. The details are as follows:
I. Use Cache
To enable the smarty cache, set caching to true and specify cache_dir.
Use cache_lefetime to specify the cache survival time, in seconds
To generate multiple different caches for the same page, add the second parameter cache_id to display or fetch, for example:
$smarty->display('index.tpl',$my_cache_id);
This feature can be used to cache different $ _ GET objects.
Ii. Clear Cache
Clear_all_cache (); // clear all cache clear_cache ('index. tpl '); // clear the index. tpl cache clear_cache ('index. tpl ', cache_id); // clear the cache of the specified id
Iii. Use 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 cache 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
Iv. Disable local cache
There are several ways to invalidate the cache in some regions (only cache is required:
Insert:
Defines the processing function to be used for an insert tag. The function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the insert name, that is, if the function you define is insert_abc, the method used in the template is {insert name = 'abc '}
The parameter is passed in through $ params
You can also create an insert plug-in. The file name is insert. xx. php, and the function name is smarty_insert_aa ($ params, & $ smarty). xx is defined as the same as above.
Register_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:
<?phpfunction smarty_block_cacheless($param, $content, &$smarty) { return $content;}?>
2) write programs and templates
Example: testCacheLess. php
<?phpinclude('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} Not cached: {$ smarty. now} {/cacheless}