Php+smarty Caching operations

Source: Internet
Author: User
First, use the cache
To turn on the Smarty cache, simply set the 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 to display or fetch, such as $smarty->display (' Index.tpl ', $my _cache_id); This attribute can be used for different $_ Get to do a different cache
Second, clear the cache

Clear_all_cache ()//Clear All caches
Clear_cache (' Index.tpl ');//Clear INDEX.TPL Cache
Clear_cache (' Index.tpl ', cache_id);//clear cache of specified IDs

Third, use the custom caching method

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
Four, partial shutdown cache

There are several ways to disable caching in certain areas (only the cache that is needed):
Inser:
Defines a handler function to use for a inser tag, with the function name format: insert_xx (array $params, Object & $smarty) where xx is the name of the insert, that is, if you define a function that is Insert_ ABC, the method used in the template is {insert name= ' abc '}
Parameter passed through $params
can also be made into insert plug-ins, filename named: insert.xx.php, Function named: Smarty_insert_aa ($params,& $smarty), XX definition ditto
Register_block:
Define a Block:smarty_block_name ($params, $content, & $smarty) {return $content;}//name the domain name of the zone
Registration 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:



2 Writing programs and templates
Sample program: testcacheless.php



Template used: CACHE.TPL

Already cached: {$smarty. Now}
{cacheless}
Not cached: {$smarty. Now}
{/cacheless}


For some of the solutions that are not cached in the template:

Smarty provides a powerful caching capability. But sometimes we don't want the entire document to be cached, but we have the option to cache a portion of the content or a portion of the content that is not cached. For example, you use a template with a banner position at the top of the page, and the banner can contain any mix of HTML, images, flash, etc. So you can't use a static link here, and we don't want the banner to be cached. This needs to be specified in the Insert function, and a function is required to take the content information of the advertisement bar. Smarty also provides this caching control capability.

We can use {insert} to keep part of the template from being cached

You can use $smarty->register_function ($params,& $smarty) to block Plug-ins from being exported from the cache.

You can also use $smarty->register_block ($params,& $smarty) to keep one piece of the entire page from being cached.

Below we really to a simple requirement, explain these three kinds of methods of controlling the cache output respectively.

Requirements: The current time in the cached document is not cached and varies with each refresh.
<

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.