Smarty static cache, smarty static

Source: Internet
Author: User

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

 

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.