Smarty template basics-cache data, smarty template-Cache
Cache data. This is not a temporary cache, but a cache written into the memory.
Write in one example: cache data
I. Basic functions for writing php and html pages
Since the smarty template is used, the frontend and backend must be written separately.
(1) php page
| 123456789101112 |
<?php$filename = "../cache/huancun.html"; // This is the page to be cached (it is not the Code but the source information of the page) include("../init.inc.php"); // Import the entry fileinclude("../DBDA.php"); // Introduce the database and use the database content $db = new DBDA(); // Create a new object$sql = "select * from chinastates"; // This is the information in the Chinastates table.$attr = $db->Query($sql); // Execute this statement $smarty->assign("shuzu",$attr); // Register variable information$smarty->display("huancun.html"); // Display template |
(2) html page
You can use tables to display data.
| 1234567891011121314151617 |
<h1> Data list </h1><table width="50%" border="1" cellpadding="0" cellspacing="0"> <tr> <td> Code </td> <td> Name </td> <td> Operation </td> </tr> <{foreach $shuzu as $v}> <! -- Display the corresponding information in the table through traversal --> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td> Operation </td> </tr> <{/foreach}> </table> |
View the effect
2. Compile the "cache" Function
The source code of the page is not written to the above php page but to the cache.
(1)this cachefile contains this huancun.html file which does not exist. How can this problem be solved? Does not exist?
The result is: if the cached file exists, the cache is called directly. If the cached file does not exist, the cache is re-cached.
A. if this file exists (the file_exists () method is used to determine whether the file does not exist) Statement:
| 12345 |
if(file_exists($filename)){ // Directly call the cache include($filename);} |
B. Otherwise (the file does not exist)
It is important to add the following items to read the database content:
1. Enable the memory cache;
2. Disable the memory cache;
They are paired !!!
| 1234567891011121314151617181920212223 |
else{ // Cache again ob_start(); // Enable the memory cache include("../init.inc.php"); include("../DBDA.php"); $db = new DBDA(); $sql = "select * from chinastates"; $attr = $db->Query($sql); $smarty->assign("shuzu",$attr); $smarty->display("huancun.html"); $str = ob_get_contents(); // Obtain the cache content in the memory file_put_contents($filename,$str); // Put the content in the string into the $ filename File ob_flush(); // Disable the memory cache echo "#######################################"; // This is to identify the cache file } |
As shown in the following figure, because the cachefolder does not contain hunacun.html, a long string ######
Click "refresh" again, because the huancun.html file is already in the cachefolder.
Iii. cache Validity Period
If the cache is always there, how can we modify it in the background? The previous modification is not required, so we need to cache the effective time with all our efforts.
The Code is as follows:
| 12 |
$time = 10; // Cache validity period: 10 secondsif(file_exists($filename) && ((filemtime($filename)+$time)>= time()) ) // This is the judgment on the cached File |
Output when the cached file is available and no cached file is available again in 10 seconds ####
4. Paging Cache
The first is paging php page writing, which is modified again on the updated php page above, as shown below:
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
<?php$p = 1;if(!empty($_GET["page"])){ $p = $_GET["page"];} $filename = "../cache/huancun{$p}.html"; // Cache file storage location $time = 10; // Cache validity period: 10 secondsif(file_exists($filename) && ((filemtime($filename)+$time)>= time()) ){ // Directly call the cache include($filename);}else{ // Cache again ob_start(); // Enable the memory cache include("../init.inc.php"); include("../DBDA.php"); $db = new DBDA(); $sqll = "select count(*) from chinastates"; $zts = $db->StrQuery($sqll); // Execution statement of the total number of items include("../page.class.php"); $page = new Page($zts,10); // Number of entries displayed by PAGE $sql = "select * from chinastates ".$page->limit; // Paginated $ page-> limit $attr = $db->Query($sql); $smarty->assign("fpage",$page->fpage()); // Display page information $smarty->assign("shuzu",$attr); $smarty->display("huancun.html"); $str = ob_get_contents(); // Obtain the cache content in the memory file_put_contents($filename,$str); // Write $ str content to the $ filename File ob_flush(); // Disable the memory cache echo "#######################################"; } |
The result is inbound. Each page is available from the cache and becomes invalid 10 seconds later.
This is the second page. No files are cached at the beginning, so there will be ####
Click one page at a time, and the cached file will be available when you return, so no ###
The same is true in the future ~~~