Home > Backend Development > PHP Tutorial > Applications related to smarty caching

Applications related to smarty caching

WBOY
Release: 2016-07-25 09:10:28
Original
1019 people have browsed it
  1. $smarty->cache-dir="directory name"; //Create cache directory name
  2. $smarty->caching=true; //Enable caching. When it is false, the cache will be invalid
  3. $smarty- >cache_lifetime=60; //Cache time, unit is seconds
Copy code

2. Use and clear Smarty cache

  1. $marty->display("cache.tpl",cache_id); //Create a cache with ID
  2. $marty->clear_all_cache(); //Clear all caches
  3. $marty-> clear_cache("index.php"); //Clear the cache in index.php
  4. $marty->clear_cache("index.php',cache_id); //Clear the cache of the specified ID in index.php
Copy Code

3. Smarty’s local cache The first one: The insert_ function does not cache by default, and this attribute cannot be modified. How to use: example index.php,

  1. function insert_get_time(){
  2. return date("Y-m-d H:m:s");
  3. }
Copy the code in

index.html,

  1. {insert name="get_time"}
Copy code

Second: smarty_block Define a block:smarty_block_name($params,$content, &$smarty){return $content;} //name represents the area name Register block:$smarty->register_block('name', 'smarty_block_name', false); //The third parameter false means that this area is not cached Template writing: {name}content{/name} Written as a block plug-in: 1) Define a plug-in function: block.cacheless.php and place it in the smarty plugins directory The content of block.cacheless.php is as follows:

  1. function smarty_block_cacheless($param, $content, &$smarty) {
  2. return $content;
  3. }
  4. ?>
Copy code

2) Write program and template Sample program: testCacheLess.php

  1. include('Smarty.class.php');
  2. $smarty = new Smarty;
  3. $smarty->caching=true;
  4. $smarty->cache_lifetime = 6;
  5. $smarty->display('cache.tpl');
  6. ?>
Copy code

Template used: cache.tpl Already cached: {$smarty.now}
{cacheless} Not cached:{$smarty.now} {/cacheless} 4. Custom cache Set cache_handler_func to use a custom function to handle the cache like:

  1. $smarty->cache_handler_func = "myCache";
  2. function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
  3. }
Copy code

This function is generally based on $action to determine the current operation of the cache:

  1. switch($action){
  2. case "read"://read cache content
  3. case "write"://write cache
  4. case "clear"://clear
  5. }
Copy the code

Generally use md5 ($tpl_file.$cache_id.$compile_id) as the only cache_id If necessary, use gzcompress and gzuncompress to compress and decompress.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template