PHP file cache smarty template application instance analysis, smarty application instance _ PHP Tutorial

Source: Internet
Author: User
Tags smarty template
Php file cache smarty template application instance analysis, smarty application instance. Php file cache smarty template application instance analysis, smarty application instance This article analyzes the php file cache smarty template application. I. make the php file cache smarty template application instance analysis and smarty application instance for your reference.

This document analyzes the smarty template application of PHP file cache. We will share this with you for your reference. The details are as follows:

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 display or fetch, for example:

$smarty->display('index.tpl',$my_cache_id);

This feature can be used to cache different $ _ GET objects.

II. clear cache

Clear_all_cache (); // clear all cache clear_cache ('index. tpl '); // clear the index. tpl cache clear_cache ('index. tpl ', cache_id); // clear the cache of 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 cache 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:

Insert:

Defines the processing function to be used for an insert tag. the function name format is: insert_xx (array $ params, object & $ smarty). xx indicates the insert name, 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}

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:

<?phpfunction smarty_block_cacheless($param, $content, &$smarty) {   return $content;}?>

2) write programs and templates

Example: testCacheLess. php

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

Template used: cache. tpl

Cached: {$ smarty. now}
{Cacheless} has no cache: {$ smarty. now} {/cacheless}

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.