How the Smarty cache is used

Source: Internet
Author: User

Smarty Template Configuration

The code is as follows Copy Code

$smarty->cache_dir = './cache/'; Set up a folder where cached files are stored
$smarty->caching = 1; Turn on Cache 0, false on behalf of off | Not 0 digits, true represents open
$smarty->cache_lifetime = 3600; The unit is seconds (if filled-1 is never expired)

The easiest thing to do is to turn on the cache.

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

The code is as follows Copy Code
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:

The code is as follows Copy Code
$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:

The code is as follows Copy Code
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

One of the small details need attention

Smarty cache defaults are invalid, first you need to open the Smarty cache.

The code is as follows Copy Code

$smarty->caching = 1;

Second, set the life cycle of the cache. When the page is accessed during the lifecycle, Smarty returns the cached content directly to the user. Rather than some other language like the first compile run, link lookup database, organize data to generate page content, and finally return to the user. Obviously can save a lot of processing overhead.

The code is as follows Copy Code
$smarty->cache_lifetime = 86400;//

Number unit: seconds, free to set

Third, display the page content.

The code is as follows Copy Code

$smarty->display (' Index.tpl ');

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.