- $smarty->display (' Index.tpl ', $my _cache_id);
Copy CodeThis attribute can be used to make different caches for different $_get. Second, clear the Smarty cache clear Smarty cache is generally the following ways:
- Clear_all_cache ();//Clear All caches
- Clear_cache (' Index.tpl ');//clears the cache for the specified template Index.tpl
- Clear_cache (' Index.tpl ', cache_id);//clear cache for specified template specified ID
Copy CodeGlobal cache Global caching is the generation of cached pages for all pages of the entire site. Setting the global cache first we will operate the Smarty configuration file, open the cache, specify the cache file directory, and set the cache survival time, set the parameters as follows: $smarty->cache_dir= './cache/'; Set the folder where the cache files are stored $smarty->caching=1;//turn on cache 0, false for off | Not 0 digits, true to open $smarty->cache_lifetime=3600//units in seconds ( If you fill in 1 to never expire, then we will go to the specific PHP page to set the corresponding specific cache file name, in the PHP page can be written as follows:
- $smarty->display (' list2.html ', MD5 ($_server[' Request_uri '));//MD5 encrypt the URL of the current page (all parameters included) and set the cache file name
Copy CodeNote: This method is $smarty->display (' corresponding template file name ', ' Supplemental portion of cache file name '). The second parameter is not required, and if it is not, the cached file name is the files that are encrypted after the template file name is processed. But this can come across a tricky issue, such as: http://bbs.it-home.org/article.php?id=5http://bbs.it-home.org/article.php?id= 7 The two URLs correspond to the different content that should be, but the generated cache file name is the result of article encryption. This causes the user to query for different content and access the same cache file. So it's a good idea to add an auxiliary parameter that will access the URL (all parameters that contain the following) MD5 encryption. Part of the cache is to specify some file generation cache files, not all the Web site files. The essence of partial caching is actually partial not caching, that is, not specifying which files generate the cache, but specifying that the specific files do not generate the cache. Now suppose there are 3 files: http://bbs.it-home.org/index.php//Need cache http://bbs.it-home.org/cate.php//need cache http://bbs.it-home.org/ article.php///Do not need to cache the previous two files in the PHP file or need to write on the $smarty->display (' corresponding template filename ', ' Cache file name Supplement section ') this sentence. However, in the third file we would like to specifically indicate that there is no need to generate a cache, the specific method is to write the following code before the display specifies the template: $smarty->clear_cache (' corresponding template filename ');//actually written in $smarty-> Display (' Corresponding template file name ') before or after all can of course $smarty->display (' corresponding template file name ') or to write, originally did not want to generate a cache, the second parameter is not required. Note: The parameters of the $smarty->clear_cache () and $smarty->display () must be written in a consistent Local cache local cache is to specify some local local generation cache under the same page. In fact, it is not specifying which local build cache, but which local does not generate the cache (this is similar to the partial caching operation Idea). Let's take a look at the following example: Article.php file part of the content:
- $time =time ();
- $smarty->assign (' time ', $lanmuarr);
- function Insert_timeget () {
- return time ();
- }
- $smarty->display (' article.html ');
Copy CodeThe corresponding template file article.html part of the code:
- {$time}
- {Insert name= ' timeget '}
Copy CodeHere's an explanation of the above example: in PHP we simply define a custom function named Insert_ (note underscore) to customize the supplemental name, and the value returned in it does not need to be passed by the Assign () method, and can be directly in the template page with {insert Name= ' The custom supplemental Name '} is called in a way that is not affected by the cache and is refreshed in real time. If you feel that using the Smarty cache is not cool enough, you can also use a custom cache, how to use the custom cache? You need to set Cache_handler_func to handle the cache using a custom function, such as:
- $smarty->cache_handler_func= "Mycache";
- function Mycache ($action,& $smarty _obj,& $cache _content, $tpl _file=null, $cache _id=null, $compile _id=null) {
- Switch ($action) {
- Case "read"://Read Cache contents
- Case "Write"://write Cache
- Case "clear"://Empty
- }
- }
Copy Code |