- $ Smarty-> cache-dir = "directory name"; // create a cache directory name
- $ Smarty-> caching = true; // enable cache. if it is false, the cache is invalid.
- $ Smarty-> cache_lifetime = 60; // cache time, in seconds
II. usage and clearing of Smarty cache
- $ Marty-> display ("cache. tpl", cache_id); // create a cache with an 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 ID specified in index. php.
3. the first part of the Smarty local cache: the insert _ function is not cached by default. this attribute cannot be modified. in the example of index. php,
- Function insert_get_time (){
- Return date ("Y-m-d H: m: s ");
- }
-
In index.html,
- {Insert name = "get_time "}
The second one: smarty_block defines a block: smarty_block_name ($ params, $ content, & $ smarty) {return $ content ;}// name indicates that the region name registers the block: $ smarty-> register_block ('name', 'smarty _ block_name ', false); // The third parameter false indicates that the region is not written as a cache template: {name} content {/name} is written as the block plug-in: 1) define a plug-in function: block. cacheless. php, put it in the plug-ins directory block of smarty. cacheless. php content is as follows:
- Function smarty_block_cacheless ($ param, $ content, & $ smarty ){
- Return $ content;
- }
- ?>
-
2) compile the 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 ');
- ?>
-
Template used: cache. tpl cached: {$ smarty. now} {Cacheless} has no cache: {$ smarty. now} {/cacheless} 4. Custom cache settings cache_handler_func:
- $ 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 the cached 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, gzcompress and gzuncompress can be used for compression and decompression. |