The cache application _php template in the PHP smarty module engine

Source: Internet
Author: User
1,smarty Cache Configuration:
$smarty->cache-dir= "directory name"; Create cache directory Name
$smarty->caching=true; Turn on cache, invalid cache for false
$smarty->cache_lifetime=60; Cache time, Unit is seconds
Use and purge of 2,smarty cache
$marty->display ("Cache.tpl", cache_id); To create a cache with IDs
$marty->clear_all_cache (); Clear all Cache
$marty->clear_cache ("index.php"); Clear the cache in index.php
$marty->clear_cache ("index.php ', cache_id);" Clear cache of specified IDs in index.php
Local cache of 3,smarty
First: The Insert_ function is not cached by default, this property is not modified
How to use: Example
In 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
Define a Block:smarty_block_name ($params, $content, & $smarty) {return $content;}//name the domain name of the zone
Register BLOCK: $smarty->register_block (' name ', ' Smarty_block_name ', false); The third argument false indicates that the range is not cached
Template notation: {name} content {/name}
Write 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:
<?php
function smarty_block_cacheless ($param, $content, & $smarty) {
return $content;
}
?>
2 Writing programs and templates
Sample program: testcacheless.php
Copy Code code as follows:

<?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}<br>
{cacheless}
Not cached: {$smarty. Now}
{/cacheless}
4 Custom Caching
Set Cache_handler_func to process caching using custom functions
Such as:
$smarty->cache_handler_func = "Mycache";
function Mycache ($action, & $smarty _obj, & $cache _content, $tpl _file=null, $cache _id=null, $compile _id=null) {
}
This function is generally rooted in the $action to determine the current operation of the cache:
Switch ($action) {
Case "read"://Read cache content
Case "Write"://write Cache
Case "clear"://Empty
}
General use of MD5 ($TPL _file. $cache _id. $compile _id) as the only cache_id
If necessary, use gzcompress and gzuncompress to compress and decompress

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.