Php + Smarty cache operations-PHP source code

Source: Internet
Author: User
Ec (2); 1. To enable the smarty cache by using the cache, set caching to true and specify cache_dir. use cache_lefetime to specify the cache survival time. In seconds, multiple different caches must be generated for the same page. Add the second parameter cache_id to display or fetch, for example, $ smarty-& gt; display (index. tpl, $ my_cache_id); this feature is available in script ec (2) and script

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); this feature can be used to cache different $ _ GET
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, use gzcompress and gzuncompress to compress and decompress
Iv. Disable local cache

There are several ways to invalidate the cache in some regions (only cache is required:
Inser:
Defines 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 a 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}
Write 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:



2) write programs and templates
Example: testCacheLess. php



Template used: cache. tpl

Cached: {$ smarty. now}
{Cacheless}
No cache: {$ smarty. now}
{/Cacheless}


Solution to some non-cached templates:

Smarty provides powerful caching functions. However, sometimes we do not want the entire document to be cached, but choose to cache a part of the content or a part of the content. For example, if you use a template with the ad bar position on the top of the page, the ad bar can contain any HTML, image, FLASH, and other mixed information. therefore, a static link cannot be used here, and we do not want the ad bar to be cached. this requires the insert function to specify and a function to retrieve the content of the advertisement bar. Smarty also provides this Cache control capability.

We can use {insert} to make part of the template not cached.

You can use $ smarty-> register_function ($ params, & $ smarty) to prevent the plug-in from outputting data from the cache,

You can also use $ smarty-> register_block ($ params, & $ smarty) to prevent a part of the entire page from being cached.

The following describes the three methods to control the cache output for a simple requirement.

Requirement: the cached document is not cached at the current time and changes with each refresh.

<

Related Article

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.