Smarty configuration and advanced cache technology. Preface Smarty is an excellent PHP template engine that separates logical code from userinterface. Learning and using Smarty without applying it to the cache technology is a huge loss.
Smarty is an excellent PHP template engine that separates logical code from user interface.
Learning and using Smarty without applying it to the cache technology is a huge loss. it can cache the HMTL file that the user finally sees into a static HTML page, when the cache attribute of Smarty is set to true, users' WEB requests are directly converted to this static HTML file during the cachetime period set by Smarty, this is equivalent to calling a static HTML file, which reduces the burden on the backend server.
Download and configuration
Official Download: Smarty Download
After the download, decompress the package to the file directory of your project.
1 require ('../libs/Smarty. class. php ');
2
3 $ smarty = new Smarty;
4
5 // $ smarty-> force_compile = true; // force compile
6 $ smarty-> debugging = true; // debug
7 $ smarty-> caching = true; // enable cache
8 $ smarty-> cache_lifetime = 120; // cache survival time (seconds)
$ Smarty-> cache_dir = MY_SMARTY_DIR. '/cache/'; // Set the cache storage path
Note: If you find that the cached file changes every time you browse it, please refer to the force_compile of Smarty, which forces Smarty to call (recompile) the template each time. This setting is not restricted by $ compile_check. By default, it is invalid. It is very convenient for development and debugging, but it cannot be used in the product environment. If cache is enabled, cache files will be generated again each time.
$ Smarty-> force_compile = false; // force compile
Smarty cache technology
• Global Cache
• Local cache
1. insert method
2. Dynamic block method
3. plug-in block method
Global cache technology
As the name suggests, the global cache generates a cache file for the entire page, specifying the survival time of the cached file, and browsing the entire page again within the specified time. This will directly read the cached file.
$ Smarty-> caching = true; // enable cache
$ Smarty-> cache_lifetime = 120; // cache survival time (seconds)
Note: A template can only have one cached file. if your template has multiple pages, you must set an ID for the cache. For example, a page has multiple articles:
Http: // website/index. php? P = 1
Http: // website/index. php? P = 2
// $ _ SERVER ['request _ URI '] method
// Add the URL of the current page (including? All parameters ).
$ Url = md5 ($ _ SERVER ['request _ URI ']);
// Set the cache file name
$ Smarty-> display ('index. tpl ', $ url );
Key point: a major reason for using the cache technology is to reduce read/write to the database, so we need to use $ smarty-> isCached ('index. tpl ') to determine whether the cache exists. If yes, do not operate the database again.
If (! $ Smarty-> isCached ('index. tpl ')){
Echo "acache no found! ";
$ SQL = "SELECT * FROM test ";
$ Query = mysql_query ($ SQL );
$ Row = mysql_fetch_row ($ query );
$ Smarty-> assign ("loaddatabase", $ row [1]);
}
There is another problem here. if I have changed some content in the database and want to update the display content, but the cache has not expired yet, should it be swollen?
$ Smarty-> revoke ache ("index. tpl ");
The preceding caching ache can solve this problem. after updating the data, you can call it to clear the cache.
PS: I used the Smarty3 version. the naming of many methods in this version has changed. if it is Smarty2, "Call of unknown method 'iscached' will appear '. ", use $ smarty-> is_cached ().
The Smarty3: registerPlugin (), Smarty2: register_block () that follows is also a version issue.
The following is a comparison between the cache speed and the cache speed:
1. browsing for the first Time, no cache Total Time 0.01421
2. 2nd views with cache Total Time 0.00308
Here, I only have a few lines of code in index. php. if there is a large amount of data, there is a significant difference.
Local cache technology
Partial cache = partial cache, which is the cache of a page. not all of them generate a cache. you can customize a function module to not generate a cache. data will be updated each time you browse;
For example, when a web page displays the user's status, statistics on the web page, ads, and so on, the data is updated very quickly and cannot be cached. in this way, local cache is useful.
There are three methods for local cache:
I. insert method
The content contained in the insert statement is not cached. This function is re-executed every time this template is called.
Usage:
Note that the function name must start with insert, and the name in the template corresponds to it.
Index. php
// Define a time to test the difference between insert and normal assign
$ Date = date ("Y-m-d H: I: s ");
$ Smarty-> assign ("date", $ date );
// Insert
Function insert_get_current_time ($ date ){
Return date ("Y-m-d H: I: s ");
}
Index. tpl
Nocache: {insert name = "get_current_time "}
Cache: {$ date}
Then, let's look at the generated cache file: it is concluded that each time insert calls this template, the function will be re-executed.
Nocache: ), $ _ Smarty_tpl);?>
Cache: 15:46:52
This method is simple, but it is not suitable if the content to be displayed is large.
II. Dynamic block method
Php custom block
Index. php
// Smarty 3
// Function declaration
Function smarty_block_nocache ($ param, $ content, $ smarty)
{
Return $ content;
}
// Register with smarty
$ Smarty-> registerPlugin ("function", "nocache", "smarty_block_nocache ");
As mentioned at the beginning, Smarty3 uses registerPlugin and Smarty2 uses register_block.
Index. tpl
{Nocache} {$ date} {/nocache}
Then, read the cached file and draw a conclusion that $ date will be re-executed every time this template is called.
Tpl_vars ['Date']-> value;?>
III. plug-in block method
This method is similar to the 2nd, just put the custom block in php into the plugins folder in the smarty directory.
In the Smarty/plugins directory, create a file block. nocache. php with the following content:
Function smarty_block_nocache ($ param, $ content, $ smarty)
{
Return $ content;
}
?>
The use of the tpl template is the same as that of the second method.
Summary
The Smarty cache technology can be summarized to greatly improve the speed and quality of the website, and its usage is relatively simple.
The last note is that, although the extension of the cache file generated by Smarty is php, it will not be parsed as php code.
Author: that moment
Secret Smarty is an excellent PHP template engine that separates logical code from user interface. Learning and using Smarty without applying it to the cache technology is a huge loss, it...