Enabling the smarty Cache mechanism indicates that the $ caching attribute of the smarty object is true or 1 indicates that the cache is enabled, and false or 0 indicates that the cache is disabled. $ smarty-& gt; caching = true
Smarty cache
A. basic smarty cache
1) enabling the smarty Cache mechanism the $ caching attribute of the smarty object is true or 1 indicates that the cache is enabled, and false or 0 indicates that the cache is disabled.
$ Smarty-> caching = true
2) set the cache time. the cache_lifetime attribute value of the smarty object is an integer, in seconds.
$ Smarty-> cache_lifetime (unit: Seconds]
3) Check whether the cache needs to update the $ compile_check attribute of the smarty object before setting the smarty compilation. "true" indicates that the check is false, indicating that the smarty object is not checked.
$ Smarty-> compile_check = true
Note: for maximum performance, set $ compile_check to "false ". note: If it is set to "false", although the template file is modified, you will not see the modification result because the template is not re-compiled.
4) boolean is_cached (string $ template, [string $ cache_id]) method for determining whether the template samrty object has been cached
$ Smarty-> is_cached ('index. tpl '[, $ cache_id])
Note: if the cache of the specified template exists, the returned result is true.
$ Template specifies the template file name to be checked
$ Cache_id if the template has multiple caches, you can use it to specify the cache number.
5) clear the void clear_all_cache (int $ expire_time) method of the cached smarty object
$ Smarty-> clear_all_cache (), where $ expire_time indicates that all the caches that exceed the specified time are cleared.
6) clear void clear_cache (string $ template [, string $ cache_id [, string $ compile_id [, int $ expire_time]) of the specified template cache smarty object.
$ Smarty-> clear_cache ('index. tpl ')
$ Smarty-> clear_cache ('index. tpl', $ caceh_id)
Where: $ cache_id indicates that if this template has multiple caches, you can specify the cache number through which to clear the cache.
$ Compile_id indicates that if this template has multiple caches, you can specify the cache compilation number to clear.
$ Expire_time is used to specify the cache that exceeds a certain time (in seconds) to be cleared.
7) multiple caches can be set on one page to pass the second cache_id parameter to the display method of the smarty object.
$ Smarty-> display ('index. tpl ', $ cache_id)
Note: $ cache_id specifies the cache ID. if the cache has been used to directly obtain the cache content specified by the cache ID, otherwise, a cache file with the corresponding cache ID will be created.
Ensure that the cache_id passed to the is_cache () and clear_cache () methods is the same cache ID
You can set the first parameter of clear_cache () to null and specify the second parameter $ cache_id to clear the cache file with the specified cache_id.
8) set the cache_dir attribute of the smarty object in the cache Directory. The value can be the relative path [relatively running PHP script] or absolute path.
$ Smarty-> cache_dir = '../caccache'
Note: The default cache directory is './cache'. it is not recommended to place the cache directory under the root directory of the WEB server.
B. smarty local cache mechanism
1) use the insert function of smarty to disable local caching.
2) use register_function to prevent the plug-in from outputting data from the cache.
3) use register_block to use part of the entire page without caching
Note: Usage of functions, blocks, and insert plug-ins of smarty
Insert usage
Index. tpl
Function plug-in
Index. tpl
{{insert name='getNow'}}index.phpfunction insert_getNow($params,&$smarty){return time();}
$ Smarty-> register_function ('getnow', 'smarty _ function_getNow ', false );
The third parameter of register_function is set to false to prevent the plug-in from outputting data from the cache.
Block plugin
Index. tpl
{{getNow}}{{/getNow}}index.phpfunction smarty_block_getNow($params,&$smarty){return time();}
$ Smarty-> register_block ('getnow', 'smarty _ block_getNow ', false );
The third parameter of register_block is set to false. the getNow block can be used without caching.