Small Ant Learning App Interface Development (7)--app Interface Instance--the way of server timed cache development interface

Source: Internet
Author: User

Basic idea: The server is only responsible for updating the cache periodically. the request comes in just to read the cache.

The server periodically executes the code that generates the cache:

<?php /**********************************   crontab  timing generation of cache files ** * *  * * /usr/bin/php /app/crontab.php*    learn php small ant *    Original blog  http:/ /my.oschina.net/woshixiaomayi/blog****************************///loading the DB class//code content written the day before yesterday please refer to  http://my.oschina.net /woshixiaomayi/blog/518295require_once ('./db.php ');//Load the previously written file static cache class//code contents please refer to  http://my.oschina.net/ Woshixiaomayi/blog/518885require_once ('./file.php '); try{$connect  =db::getinstance ()->connect ();} catch (exception  $e) {/* Because this is crontab timed execution, the error can only be written into the error log, which must be noted. */file_put_contents ('./log/'. Date (' y-m-d '). TXT ',  $e->getmessage ()); return;} Operation Database $sql= "Select * from ecm_member limit 3"; $result =mysql_query ($sql, $connect); $ Index_data=array ();while  ($row =mysql_fetch_assoc ($result))  {$index _data[]= $row;} Instantiate the static file cache class, get the data on the cache, no write to log $file =new file (), if ($index _data) {$file->cachedata (' Index_page ', $indEx_data);} Else{file_put_contents ('./log/'. Date (' y-m-d '). TXT ',  ' does not get Data ');}  ?>

Request to come in and send the crontab generated cache directly to the user:

<?php/************************************** Call crontab timed generated cache data * Learn PHP small ant * Blog http://my.oschina.net/ woshixiaomayi/blog*******************************///the interface response class//code content written before loading please refer to http://my.oschina.net/woshixiaomayi/ Blog/517384require_once ('./response.php ');//Load the previously written interface response class//code contents please refer to http://my.oschina.net/woshixiaomayi/blog/ 518885require_once ('./file.php ');//have access in, read the cache, crontab will periodically update the cache, so the direct use of the line $file = new file (); $data = $file CacheData (' Index_page '), if ($data) {return response::show (200, ' data return succeeded ', $data);} Else{return response::show (400, ' data return failed ');}? >

This idea is also relatively simple, the app interface instance here to learn the end. B ( ̄▽ ̄) d come on!

Small ant Learning App Interface Development (7)--app Interface Instance--the way the server timed cache development interface

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.