First, use the cache ? To turn on the Smarty cache, simply set caching to True and specify Cache_dir. Use Cache_lefetime to specify the cache lifetime in seconds To generate multiple different caches for the same page, add a second parameter cache_id, such as $smarty->display (' Index.tpl ', $my _cache_id) to the display or fetch, which can be used for different $_ Get for different caches ? Second, clear the cache Clear_all_cache ();//Clear All caches Clear_cache (' Index.tpl ');//Clear INDEX.TPL Cache Clear_cache (' Index.tpl ', cache_id);//clears the cache for the specified ID
Iii. using a custom cache method
Set Cache_handler_func to handle caching 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) { } The function is generally rooted in the $action to determine the current operation of the cache: Switch ($action) { Case "read"://Read Cache contents Case "Write"://write Cache Case "clear"://Empty } General use of MD5 ($TPL _file. $cache _id. $compile _id) as the only cache_id If required, use gzcompress and gzuncompress to compress and decompress
? Four, partial shutdown cache There are several ways to invalidate the cache in some areas (only for the desired cache): Inser: Defines a handler function to be used for a inser tag, with the format of the function name: insert_xx (array $params, Object & $smarty) where xx is the name of the insert, that is, if you define a function as Insert_ ABC, the method used in the template is {insert name= ' abc '} Parameters passed through $params can also be made into insert plug-in, file name named: insert.xx.php, Function named: Smarty_insert_aa ($params,& $smarty), xx definition ibid. Register_block: Defines a block:smarty_block_name ($params, $content, & $smarty) {return $content;}//name represents the domain name of the zone Register BLOCK: $smarty->register_block (' name ', ' Smarty_block_name ', false); The third parameter, False, indicates that the zone is not cached Template notation: {name} content {/name}
Written as Block plugin: 1) Define a plug-in function: block.cacheless.php, placed in the Smarty plugins directory The contents of block.cacheless.php are as follows:
function smarty_block_cacheless ($param, $content, & $smarty) { return $content; } ?> 2) Writing Programs and templates Example program: testcacheless.php
Include (' Smarty.class.php '); $smarty = new Smarty; $smarty->caching=true; $smarty->cache_lifetime = 6; $smarty->display (' Cache.tpl '); ?>
Template used: CACHE.TPL
Already cached: {$smarty. Now}
{cacheless} Not cached: {$smarty. Now} {/cacheless} Run it now and find that it's not working, and both lines are cached. 3) Rewrite Smarty_Compiler.class.php (Note: This file is important, please back up first to restore if necessary) Find $this->_plugins[' block ' [$tag _command] = Array ($plugin _func, NULL, NULL, NULL, TRUE); //My Line in 705 Modified to: if ($tag _command = = ' cacheless ') $this->_plugins[' block ' [$tag _command] = Array ($plugin _func, NULL, NULL, NULL, FALSE ); else $this->_plugins[' block ' [$tag _command] = Array ($plugin _func, NULL, NULL, NULL, TRUE); You can also directly change the last parameter of the original sentence to false, I do not understand the internal mechanism of smarty, so add a judgment, as long as the block is cacheless not cache 4) OK, now clear the Template_c in the compilation file, re-run, it worked? |