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