Smarty static cache, smarty static
The cache allows the program to access the database more quickly, and the number of databases to be tuned is reduced, so it cannot be synchronized with the database in real time,
Cache files are generally stored in the cach folder under the smarty file;
Create cached PHP and HTML files;
Edit the PHP file to query and display files in the database;
<? Php // defines a file storage path relative to this page $ filname = ".. /cache/huancun.html "; // query data and display include (".. /init. inc. php "); include (".. /dbda. class. php "); $ db = new dbda (); $ SQL =" select * from car "; $ arr = $ db-> query ($ SQL ); $ smarty-> assign ("shuzu", $ arr); $ smarty-> display ("huancun.html"); // call the display template to display
Create a style for HTML display.
<H1> data list
The result is as follows:
Then add the cached code to the PHP file. The Code will be displayed when you open the page. Once refreshed, the code will be directly found in the Q & A folder.
<? Php // defines a file storage path relative to this page $ filname = ".. /cache/huancun.html "; // cache file storage location // determine whether the cached file exists. If the cached file exists, the cache is called directly. If the cached file does not exist, re-Cache if (file_exists ($ filname) // judge whether the file exists {include ($ filname); // directly display it} else {// re-Cache ob_start (); // enable memory cache // query data and display include (".. /init. inc. php "); include (".. /dbda. class. php "); $ db = new dbda (); $ SQL =" select * from car "; $ arr = $ db-> query ($ SQL ); $ smarty-> assign ("shuzu", $ arr); $ smarty-> display ("huancun.html "); // call the display template display // get content from the cache $ str = ob_get_contents (); file_put_contents ($ filname, $ str); // Save the cache ob_flush (); // disable the memory cache echo "wowkww.mongokjwkwjkww.kww.kjwkjw ";}
The cached files are saved in the following folders;
Cache Time added
$ Time = 10; // cache validity period: 10 s // determine whether the cached file exists. If the cached file exists, the cache is called directly. If the cached file does not exist, cache again if (file_exists ($ filname) & filemtime ($ filname) + $ time> = time ()) // determine whether the file exists. The last modification time plus the conditional modification time is later than the current time.
Pagination of pages
$sall = "select count(*) from car ";$zts = $db->Strquery($sall);include("../page.class.php");$page = new Page($zts,5);$smarty->assign("fpage",$page->fpage());
Select the first page by default. Because it is impossible to access only one page, multiple pages must be implemented;
$ P = 1; if (! Empty ($ _ GET ["page"]) {$ p = $ _ GET ["page"];} // define a File Save path relative to this page $ filname = ".. /cache/huancun#$p;.html "; // cache file storage location