Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the caching mechanism of Smarty template engine

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "detailed explanation of the caching mechanism of Smarty template engine". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, let's talk about smarty caching and compilation, which are two different concepts. Compilation is started by default, and the cache mechanism needs to be opened artificially. The files compiled by smarty are still php files, so the execution is still compiled. If the database is involved, it is still expensive to access the database, so you need smarty cache to solve it.

1. Turn on global caching

$smarty- > cache_dir = "/ caches/"; / / Cache directory $smarty- > caching = true; / / enable cache, invalid cache for flase $smarty- > cache_lifetime = 3600; / / cache time

two。 Use multiple caches on a page

For example: an article template page will generate multiple article pages, of course, cached into many pages, which is easy to implement, as long as the second parameter is set in the display () method and a unique identifier is specified. The php code is as follows:

$smarty- > display ('index.tpl',$_GET ["article_id"])

As above, an article page is cached through the id of the second parameter article.

3. Reduce overhead for caching

In other words, the pages that have been cached do not need to be processed by database operations, but can be judged by the is_cached () method!

If (! $smarty- > is_cached ('index.tpl')) {/ / call database} $smarty- > display (' index.tpl')

4. Clear cach

Generally speaking, caching is not enabled in the development process, because the output result remains unchanged during the cache time, but enabling caching during application can greatly improve web performance. The methods to clear caching are as follows:

Clear_all_cache (); / / clear all cache clear_cache ('index.tpl'); / / clear index.tpl 's cache clear _ cache (' index.tpl',cache_id); / / clear the cache of the specified id

5. Turn off local caching

If part of a page is cached and the other part does not need caching, you can do so. For example, to display the name of a user's login, you need to turn off caching. Smarty provides the following three solutions:

(1) part of the insert template is not cached.

Define a handler to be used by the inser tag. The function name format is: insert_xx (array $params, object & $smarty), where xx is the name of insert, that is, if the function you define is insert_abc, the method used in the template is {insert name=abc}.

Parameters are passed in via $params

It can also be made into an insert plug-in with the file name: insert.xx.php and the function name: smarty_insert_aa ($params,&$smarty). The xx definition is the same as above.

(2) $smarty- > register_block ($params, & $smarty) so that a piece of the entire page is not cached

Define a block:

Smarty_block_name ($params,$content, & $smarty) {return $content;} / / name represents the zone domain name

Register for block:

$smarty- > register_block (name, smarty_block_name, false); / / the third parameter false indicates that the region is not cached

Template writing:

{name} content {/ name}

Write it as a block plug-in:

Step 1: define a plug-in function: block.cacheless.php, placed in smarty's plugins directory

The block.cacheless.php is as follows:

Step 2: write programs and templates

Sample program: testCacheLess.php

Template used: cache.tpl

Cached: {$smarty.now}

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

Now run it and find that it doesn't work. Both lines are cached.

Step 3: rewrite Smarty_Compiler.class.php (Note: this file is very important, please back it up first to restore it if necessary)

Find:

The copy code is as follows:

$this- > _ plugins [block] [$tag_command] = array ($plugin_func, null, true)

Modified to:

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, that is, turn off the default cache.

(3) use register_function to prevent plug-ins from outputting from the cache

Index.tpl:

{current_time} {/ div} index.php:function smarty_function_current_time ($params, & $smarty) {return date ("Y-m-d H:m:s");} $smarty=new smarty (); $smarty- > caching = true;$smarty- > register_function ('current_time','smarty_function_current_time',false); if (! $smarty- > is_cached ()) {.} $smarty- > display (' index.tpl')

Notes:

Define a function with the function name format: smarty_type_name ($params, & $smarty)

Type is function

Name defines the label name for the user, in this case {current_time}

Two parameters are required, even if they are not used in the function. The functions of the two parameters are the same as above.

This is the end of the content of "detailed explanation of the caching mechanism of Smarty template engine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report