Phpsmarty cache application

Source: Internet
Author: User
Phpsmarty cache use 1. use cache? To enable the smarty cache, set caching to true and specify cache_dir. cache_lefetime is used to specify the cache survival time. multiple different caches must be generated for the same page in seconds. it is used in the displ php smarty cache.
I. use cache? To enable the smarty cache, set caching to true and specify cache_dir.
Use cache_lefetime to specify the cache survival time, in seconds
To generate multiple different caches for the same page, add the second parameter cache_id to the display or fetch, for example, $ smarty-> display ('index. tpl ', $ my_cache_id); can this feature be used to cache different $ _ GET statements?II. clear cache
Clear_all_cache (); // clear all caches
Clear_cache ('index. tpl '); // clear the cache of index. tpl
Clear_cache ('index. tpl ', cache_id); // clear the cache with the specified id
III. use custom cache
Set cache_handler_func to process cache using custom functions
For example:
$ Smarty-> cache_handler_func = "myCache ";
Function myCache ($ action, & $ smarty_obj, & $ cache_content, $ tpl_file = null, $ cache_id = null, $ compile_id = null ){
}
This function usually uses the root action $ action to determine the current cache operation:
Switch ($ action ){
Case "read": // read the cached content
Case "write": // write cache
Case "clear": // clear
}
Generally, md5 ($ tpl_file. $ cache_id. $ compile_id) is used as the unique cache_id.
If necessary, can gzcompress and gzuncompress be used for compression and decompression?IV. disable local cache
There are several ways to invalidate the cache in some regions (only cache is required:
Inser:
Define the handler to be used for an inser label. the function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the name of insert, that is, if the function you define is insert_abc, the method used in the template is {insert name = 'abc '}
The parameter is passed in through $ params
You can also create an insert plug-in. the file name is insert. xx. php, and the function name is smarty_insert_aa ($ params, & $ smarty). xx is defined as the same as above.
Register_block:
Define a block: smarty_block_name ($ params, $ content, & $ smarty) {return $ content;} // name indicates the region name
Register block: $ smarty-> register_block ('name', 'smarty _ block_name ', false); // The third parameter false indicates that the region is not cached.
Template syntax: {name} content {/name} is written as the block plug-in:
1) define a plug-in function: block. cacheless. php, which is placed in the smarty plugins directory.
The contents of block. cacheless. php are as follows:

Function smarty_block_cacheless ($ param, $ content, & $ smarty ){
Return $ content;
}
?>
2) write programs and templates
Example: testCacheLess. php

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


Dashboard used: cache. tpl

Cached: {$ smarty. now}

{Cacheless}
No slow storage: {$ smarty. now}
{/Cacheless}
Run it now and find that it does not work, and the two lines of content are cached.
3) rewrite Smarty_Compiler.class.php (note: This file is very important. back up the file first to restore it if necessary)
Search$ This-> _ plugins ['block'] [$ tag_command] = array ($ plugin_func, null, true );// My 705 rows
Modify:
If ($ tag_command = 'cacheless ') $ this-> _ plugins ['block'] [$ tag_command] = array ($ plugin_func, null, false );
Else $ this-> _ plugins ['block'] [$ tag_command] = array ($ plugin_func, null, true );
You can also directly change the last parameter of the original sentence to false. I am not familiar with the internal mechanism of smarty, so I added a judgment that as long as the block is cacheless, it will not be cached. 4) OK. Now clear the compilation file in template_c and run it again. does it work?

?

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.