(Turn) Smarty caching techniques

First, use the cache 
to open smarty cache, simply caching is set to true, and specify cache_dir can.
Use cache_lefetime specified cache lifetime (in seconds)
to generate a plurality of different cache on the same page, display or fetch in Add the cache_id second parameter, such as $ smarty-> display (index.tpl, $ my_cache_id); this feature can be used differently for different cache $ _GET
 
two, clear the cache

clear_all_cache (); // clear all caches
clear_cache (index.tpl); // clear the cache index.tpl
clear_cache (index.tpl, cache_id); // Clear the cache id

Third, use a custom cache mode

Provided cache_handler_func use a custom cache processing function
such as:
$ smarty-> cache_handler_func = "myCache";
  function myCache ($ Action, & $ smarty_obj, & cache_content $, $ tpl_file = null, null = the cache_id $, $ compile_id = null) {
}


The general function $ action is noted in the current operation to determine if the cache:
Switch ($ action) {
  Case "Read": // read the contents of the cache
  case "write": // Write Cache
  case "clear": // Clear
}


Generally used md5 ($ tpl_file. $ Cache_id. $ Compile_id) as the sole cache_id
if desired, may be used to gzuncompress gzcompress and compression and decompression
 
four local cache off

In certain areas to make the cache miss (cache only need thereof), there are several methods:
insert:
  handler insert defines a label to be used, the function name format: insert_xx (array $ params, object & $ smarty) wherein the xx is insert a name, that is, if you define a function for insert_abc, then use the template to {insert name = abc} $ params parameter passed by.

  Insert plug-in can also be made, the file name is named: insert.xx. PHP , function named: smarty_insert_aa ($ params, & $ smarty), xx defined above,


register_block:
  definition of a block: smarty_block_name ($ params, $ content, & $ smarty) {return $ content;} // name indicates the area name
  registration block: $ smarty-> register_block (name , smarty_block_name, false); // third false parameter indicates that the region is not cached
  templates written: {name} content {/ name}


Written in block plug-ins:

  1) the definition of a plug-in function: block.cacheless.php, on smarty plugins directory
  block.cacheless.php reads as follows:

  <? PHP
    function smarty_block_cacheless ($ param, $ Content, & $ Smarty) {
         return $ Content;
    }
  ?>
 
  2) and programming templates
  sample program: testCacheLess.php

  <?php
    include(Smarty.class.php);
    $smarty = new Smarty;
    $smarty->caching=true;
    $smarty->cache_lifetime = 6;
    $smarty->display(cache.tpl);
  ?>

  Templates used: cache.tpl

  Cached: {$ smarty.now <br>}
  {} cacheless
  no cache: smarty.now} {$
  {/} cacheless

Reproduced in: https: //www.cnblogs.com/caly/archive/2011/12/16/2290536.html

Guess you like

Origin blog.csdn.net/weixin_34204722/article/details/93538073