"Review" generates a piece of code for the cache, what do you think is the problem? Code style aspects

Source: Internet
Author: User
Tags sprintf
Wrote a function of generating a cache, what do you think is the problem?
The way and style of writing code, evaluation

Cacheidfunction Makethreecache ($ID) {$idArr = array (' diqu ' = = Array (' ID ', ' name ', '/cachediqu ', 3, 1, ' China ', '/1 . php ', "select * from Gk_common_diqu WHERE ' level ' =%d and upid= '%d '"), ' infocat ' = = Array (' id ', ' name ', '/cacheinfoca  T ', 3, 0, ' Big class ', '/0.php ', ' select * from Gk_info_cat WHERE ' level ' =%d and ' able ' =1 and upid= '%d '),);  if (!array_key_exists ($ID, $IDARR)) return false;  List ($fnid, $fnvalue, $cacheDir, $deep, $upid 1, $pos 1, $firstFile, $selectSQL) = $IDARR [$ID];  Global $dsql;  Set_time_limit (0);  $tm 1 = microtime (true);  $dir 1 = dededata. $cacheDir;  Rmrecurse ($dir 1);//delete this dir first! if (!is_dir ($dir 1) &&!mkdir ($dir 1,0777)) exit ('--| directory creation failed!  '. $dir 1);  $path 1 = $dir 1. $firstFile; $data 1 = ' < '?  Php\n ";  $data 1.= "\n/* CACHE: {$ID}: {$pos 1}\t */";  $data 1.= "\n/* time:". Date ("Y-m-d h:i:s"). "* *";  $data 1.= "\n/* path:" $path 1. "*/\n"; $data 1.= "\n\ $CACHE ['". $ID. "]  = Array (";  $dsql->execute (' s1 ', sprintf ($selectSQL, 1, $upid 1)); while ($row 1 = $dSql->getarray (' s1 ')) {$data 1. = "\ n". $row 1[$fnid]. "    \t=>\t ' ". $row 1[$fnvalue]." ', ";    $tm 2 = Microtime (true); $pos 2 = $pos 1. '. $row 1[$fnid] '.    $row 1[$fnvalue]; $dir 2 = $dir 1. "    /". $row 1[$fnid]; if (!is_dir ($dir 2) &&!mkdir ($dir 2,0777)) {echo ("\n--|--| Directory creation failed!      ". $dir 2);    Continue    } $path 2 = sprintf ('%s/%d.php ', $dir 2, $row 1[$fnid]); $data 2 = ' < '?    Php\n ";    $data 2.= "\n/* CACHE: {$ID}: {$pos 2}\t */";    $data 2.= "\n/* time:". Date ("Y-m-d h:i:s"). "* *";    $data 2.= "\n/* path:" $path 2. "*/\n"; $data 2.= "\n\ $CACHE ['". $ID. "] [' ". $row 1[$fnid]." "    = Array (";    $dsql->execute (' S2 ', sprintf ($selectSQL, 2, $row 1[$fnid])); while ($row 2 = $dsql->getarray (' s2 ')) {$data 2. = "\ n". $row 2[$fnid]. "      \t=>\t ' ". $row 2[$fnvalue]." ', ";      $tm 3 = Microtime (true); $pos 3 = $pos 2. '. $row 2[$fnid] '.      $row 2[$fnvalue]; $dir 3 = $dir 2. "      /". $row 2[$fnid]; if (!is_dir ($dir 3) &&!mkdir ($dir 3,0777)) {ECho ("\n--|--|--| Directory creation failed!        ". $dir 3);      Continue      } $path 3 = sprintf ('%s/%d.php ', $dir 3, $row 2[$fnid]); $data 3 = ' < '?      Php\n ";      $data 3.= "\n/* CACHE: {$ID}: {$pos 3}\t */";      $data 3.= "\n/* time:". Date ("Y-m-d h:i:s"). "* *";      $data 3.= "\n/* path:" $path 3. "*/\n"; $data 3.= "\n\ $CACHE ['". $ID. "] [' ". $row 1[$fnid]." " [' ". $row 2[$fnid]." "      = Array (";      $dsql->execute (' S3 ', sprintf ($selectSQL, 3, $row 2[$fnid])); while ($row 3 = $dsql->getarray (' S3 ')) {$data 3. = "\ n". $row 3[$fnid]. "      \t=>\t ' ". $row 3[$fnvalue]." ', "; } $data 3.= "\ n); \n\n/* [Level=3]all Taken". sprintf ('%.6f ', Microtime (True)-$tm 3). " S */\n? '. '      > ';      $FP 3 = fopen ($path 3, ' W ');      Fwrite ($fp 3, $data 3);    Fclose ($fp 3); } $data 2.= "\ n); \n\n/* [Level=2]all Taken". sprintf ('%.6f ', Microtime (True)-$tm 2). " S */\n? '. '    > ';    $FP 2 = fopen ($path 2, ' W ');    Fwrite ($fp 2, $data 2);  Fclose ($fp 2); } $data 1.= "\ n); \n\n/* [Level=1]all Taken". sprintf ('%.6f ', microtime(true)-$TM 1). " S */\n? '. '  > ';  $FP 1 = fopen ($path 1, ' W ');  Fwrite ($fp 1, $data 1);  Fclose ($fp 1); return true;}



generated content 1: (table Diqu)
 
  [6] Guangdong--[76] Guangzhou *//* time:  2012-12-21 12:28:42 *//* path:  d:/website/35***.com/www/data/cachediqu/6/76/76. PHP */$CACHE [' diqu '] [' 6 '] ['] [] = Array (692=> ' Conghua ',693=> ' Tianhe District ',694=> ' Higashiyama ward ',695=> ' Baiyun District ',696=> ' Haizhu District ', 697 = ' Liwan District ',698=> ' Yuexiu District ',699=> ' Huangpu District ',700=> ' Fanyu District ',701=> ' "Huadou District ',702=> ' to add urban ',703=> ' Conghua area ',704=> ' suburbs ',) ;/* [Level=3]all taken 0.002312s  */?>


Generated Content 2: (table Infocat)
 
  ' Lighting industry ',10152=> ' electronic components ',10272=> ' media, radio and television ',10392=> ' Security, protection ',10603=> ' packaging ',10788=> ' paper ',10897=> ' office, Culture and Education ', 11183=> ' digital, computer ',11384=> ' electrician electrical ',11694=> ' textile, leather ',11894=> ' apparel ',12015=> ' apparel ',12069=> ' machinery and industry equipment ', 13100 = > ' Hardware, Tools ',13506=> ' chemical ',14224=> ' fine chemicals ',14643=> ' rubber and plastic ',14932=> ' environmental protection ',15128=> ' instrumentation ',15520=> ' household items ', 15805=> ' Home appliances ',15967=> ' construction, building materials ',16437=> ' transportation ',16597=> ' gifts, crafts, accessories ',16838=> ' energy ',16978=> ' agriculture ', 17300 = > ' Auto and Accessories ',17636=> ' food, beverage ',17906=> ' communication products ',18034=> ' toys ',18092=> ' metallurgical Minerals ',18536=> ' printing ',18712=> ' movement, Leisure ',18940=> ' business Services ',19177=> ' project Cooperation ',19233=> ' second-hand equipment transfer ',19278=> ' processing ',19455=> ' agent ',19512=> ' inventory backlog ',);/* [ Level=1]all taken 5.697496s  */?>







Reply to discussion (solution)

Check if the code is correct:

Detection of three levels of data, diqu,infocat/*1) deep=1,d1=0,d2>0,d3>0 d1=0,d2=0,d3=0 d1>0,d2>0,d3=0 D1&gt ; 0,d2=0,d3=0 d1>0,d2>0,d3>02) deep=2,d1>0,d2>0,d3>03) deep=3,d1>0,d2>0,d3>0*/function Checkthree ($cacheID = ' diqu ', $dataArray =array (), $deep =2) {if (empty ($dataArray)) return array (false, ' missing passed numeric parameter ');// Missing incoming parameter $CACHEARR = array (' diqu ' = = Array (' Diqu ', '/cachediqu ', '/1.php ', ' makediqucache ', ' region '), ' infocat ' = AR  Ray (' Diqu ', '/cacheinfocat ', '/0.php ', ' makeinfocatcache ', ' Information classification '),); if (!array_key_exists ($cacheID, $CACHEARR)) return array (FALSE, ' ID parameter Error!  ');  List ($varName, $cacheDirName, $firstFile, $cacheMakeFuncName, $cnName) = $CACHEARR [$cacheID];  if (!is_dir (dededata. $cacheDirName)) $cacheMakeFuncName ($cacheID);  List ($d 1, $d 2, $d 3) = $dataArray;  $returnData = Array ();    if ($d 1>0) {$path 1 = dededata. $cacheDirName. $firstFile;    if (!file_exists ($path 1)) return array (false, ' syserror-'. $cacheID. '-d1 File not found! '); InclUde $path 1; if (!array_key_exists ($d 1, $CACHE [$cacheID])) return array (False, ' you selected ['. $cnName. '] First-level Property error!    ');    $returnData [] = $CACHE [$cacheID] [$d 1]; prevent situations [deep=1,d1>0,d2>0,d3=0],[deep=1,d1>0,d2=0,d3>0] if ($deep < 2 &&! $d 2 &&! $d 3) RET    Urn Array (true, $returnData); if ($d 2>0) {$path 2 = dededata. $cacheDirName. "      /{$d 1}/{$d 1}.php ";      if (!file_exists ($path 2)) return array (false, ' syserror-'. $cacheID. '-d2 File not found! ');      Include $path 2; if (!array_key_exists ($d 2, $CACHE [$cacheID] [$d 1])) return array (False, ' you selected ['. $cnName. '] Second-level Property error!      ');      $returnData [] = $CACHE [$cacheID] [$d 1][$d 2];      Preventing the situation [deep=2,d1>0,d2>0,d3>0] causes D3 not to check if ($deep <3 &&! $d 3) Return Array (true, $returnData); if ($d 3>0) {$path 3 = dededata. $cacheDirName. "        /{$d 1}/{$d 2}/{$d 2}.php ";        if (!file_exists ($path 3)) return array (false, ' syserro-'. $cacheID. '-d3 File not found! ');        Include $path 3; if (!array_key_existS ($d 3, $CACHE [$cacheID] [$d 1][$d 2])) return array (False, ' you selected ['. $cnName. '] Three-level attribute classification error!        ');        $returnData [] = $CACHE [$cacheID] [$d 1][$d 2][$d 3];      Return Array (True, $returnData); } else {return array (false, ' you have not selected ['. $cnName. ']      Level three attribute '); }} else {return array (false, ' you have not selected ['. $cnName. ']    Level two attribute '); }} else {return array (false, ' you have not selected ['. $cnName. ']  The first-level attribute '); }//Returns the data return array (True, $returnData) by default;}



For comments, code styles and ways ...

while ($row 2 = $dsql->getarray (' s2 '))
{
$data 2. = "\ n". $row 2[$fnid]. " \t=>\t ' ". $row 2[$fnvalue]." ', ";

$tm 3 = Microtime (true);
$pos 3 = $pos 2. '. $row 2[$fnid] '. $row 2[$fnvalue];
$dir 3 = $dir 2. " /". $row 2[$fnid];
if (!is_dir ($dir 3) &&!mkdir ($dir 3,0777))
{
Echo ("\n--|--|--| Directory creation failed! ". $dir 3);
Continue
}
It means that if you can't create a directory, you skip it, right?
Which means you're not going to be able to cache anything? Is that what it means?

Not bad

1 array_key_exists is intuitive, but not as efficient as Isset
2 Can not use global

1 array_key_exists is intuitive, but not as efficient as Isset
2 Do not use global.
3 mkdir 777, you are the cache, and 600 is enough.

while ($row 2 = $dsql->getarray (' s2 '))
{
$data 2. = "\ n". $row 2[$fnid]. " \t=>\t ' ". $row 2[$fnvalue]." ', ";

$tm 3 = Microtime (true);
$pos 3 = $pos 2. '. $row 2[$fn ...

Logically, that's the way to think. Because I don't want to stop half. It's not quite good, but it's a hint.

My own view is that now look at this big paragraph, feel good bloated, but there is no more convenient. Ha haha ~

Self-Comments: Write very messy ~ ~ But there seems to be no other better way?

  • Related Article

    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.