Optimization of multi-level distribution docking third party API acquisition Data System

Source: Internet
Author: User
Tags php language
Recently in a multi-level distribution management system based on the praise, all the members of the store are in the likes of the mall, using the likes API to get their performance, but there is like the distribution of only one level, so make the system. Considering the reduction of workload, sorting out the hierarchical relationship, using the OOP design method, the database, table encapsulated as base class, distribution member, store and other inheritance table.

However, there are serious performance issues when listing sales reports and distributors, because reseller performance rewards are linked to their sub-distributors, so when the database is encapsulated, DFS has historically been given a tree of all distributors, however, there is no need for such a relationship when listing sales reports. Dfs spends a great deal of time on the PHP language, resulting in a page opening that takes more than 10s of time, so it is optimized for the first step by setting the DFS switch in the package function of the database:

This is a partial implementation of the database class, where some sensitive data is hidden.

!--? phpnamespace system;require_once (& #39; kdtapiclient.php& #39;); class Database{const db_adr= "" Const db_usr= "", const db_pwd= "", const db_db= "";p ublic $member =array ();p ublic function init ($dfs =true, $store =true) {$ Mysqli=new \mysqli (self::d b_adr,self::d b_usr,self::d b_pwd,self::d b_db); if ($mysqli--->connect_error) throw new Exception (' Connect Error ' ('. $mysqli->connect_errno. ') ' . $mysqli->connect_error); $result = $mysqli->query ("Select ' id ' from member_new"); $row = $result->fetch_array ( ); $i =0;while ($row) {$this->member[$i]=new member ($row [0], $dfs, $store); $row = $result->fetch_array (); $i + +;} $mysqli->close ();} static public Function Doquery ($string) {$mysqli =new \mysqli (self::d b_adr,self::d b_usr,self::d b_pwd,self::d b_db); ($mysqli->connect_error) throw new Exception (' Connect error ('. $mysqli->connect_errno. ') ' . $mysqli->connect_error); return $mysqli->query ($string);} static public Function Querysell ($start, $end) {}} 

The member class is inherited from table:

Class Table{public $data =array ();p rotected $table _name;public function __construct ($id) {$this->data[' id ']= $id; Mysqli=new \mysqli (Database::d b_adr,database::d b_usr,database::d b_pwd,database::d b_db); if ($mysqli->connect_ Error) throw new Exception (' Connect Error ('. $mysqli->connect_errno. ') ' . $mysqli->connect_error); $result = $mysqli->query ("select * from" $this->table_name. "where ' id ' = $id"); $row = $result->fetch_assoc (); $this->data= $row; $mysqli->close ();} Public Function UpdateAll ()//do is not a change ID!!! {Reset ($this->data), while ($data =each ($this->data)) {$querystring = "update". $this->table_name. "Set". $ Data[0]. " ' = ' $data [1] ' where ' id ' = '. $this->data[' id ']. "'";D Atabase::d oquery ($querystring);} Reset ($this->data);} Public Function Update ($key) {$querystring = "update". $this->table_name. "Set ' $key ' = '". $this->data[$key]. "' where ' id ' = ' ". $this->data[' id ']." ' ";D Atabase::d oquery ($querystring);} Public function set ($key, $data)//recommended{$this->data[$key]= $data; $this->update ($key);} Public function Get ($key)//recommended{return $this->data[$key];}}

Encapsulates the basic operation of the table, simplifying subsequent code writing. The following constructor of the member class can complete the DFS function, and after setting the switch parameters, you can improve efficiency by not trying DFS on some occasions:

Class Member extends table{protected $table _name= "member_new";p ublic $infer =array ();p ublic $store =array ();p ublic function __construct ($id, $dfs =true, $store =true) {parent::__construct ($id); $mysqli =new \mysqli (Database::d B_adr, Database::d b_usr,database::d b_pwd,database::d b_db), if ($mysqli->connect_error) throw new Exception (' Connect Error ('. $mysqli->connect_errno. ') ' . $mysqli->connect_error), if ($dfs) {$result = $mysqli->query ("Select ' Id ' from". $this->table_name. "Where" Super ' = '. $this->data[' id ']), if ($result) {$row = $result->fetch_array (); $i =0;while ($row) {$this->infer[$i] =new Member ($row [0]); $row = $result->fetch_array (); $i + +;}}} if ($store) {$result = $mysqli->query ("Select ' id ' from store_new where ' member ' =". $this->data[' id ')); if ($result) {$row = $result->fetch_array (); $i =0;while ($row) {$this->store[$i]=new Store ($row [0]); $row = $result->fetch_ Array (); $i + +;}}} $mysqli->close ();}

(Some sensitive functions are hidden)

In addition, in the front-end of the Ajax response to the PHP script, the support of the cache, due to the good API calls slow, however, this distribution mall data on the real-time requirements are not very high, so the use of server caching to reduce the speed and the part of the likes of communication slowed down. In addition, Localizing objects instead of reading information from a remote database also reduces the expense of querying the database. The cache object is implemented as follows to store the object's serialization code locally on the server:

 
  3600*24) return false;            return $array [' data '];        static public Function Updatecache ($string, $data)        {            $array =array ();            $array [' Time ']=time ();            $array [' Data ']= $data;            $file =fopen ($string. ". Ser "," w ");            Fwrite ($file, serialize ($array));            Fclose ($file);        }    }

The Backend Ajax response page chooses to get local data based on feedback from the cache class, or requests the server to update local data:

 
  Fetch_array (); Echo $row [0];exit (0); case ' get ': if ($array =cache::readcache ("member") {echo json_encode ($array);} else{$db =new Database (); $db->init (false,true); $arr =array (); for ($i =0; $i
 
  
   
  member); $i + +) {$arr [$i]= Array (); $arr [$i]= $db->member[$i]->data; $arr [$i] [' Password ']=null; $arr [$i] [' Name ']=iconv ("GBK", "Utf-8", $ arr[$i [' name ']), $arr [$i] [' Nickname ']=iconv ("GBK", "Utf-8", $arr [$i] [' nickname ']), $arr [$i] [' Sell ']= $db member[$i]->getsell ();} Cache::updatecache ("member", $arr); Echo Json_encode ($arr);} Exit (0);
 
  

After the above optimization, the original response time of about 15s pages, the response time is now about 0.5s.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the multi-level distribution docking third-party API acquisition Data System optimization, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.