![]() Caching of pages in PHP Hello expensive{dear} colleagues and it is simple readers. If you have decided to read this clause{article} means you as well as me the problem of caching of pages and by that excites acceleration of their loading. Many after start to study language of web - programming aspire to create that - nibud` unique and more likely to lay out for general using in the Internet, but the majority does not care of functionality of the applications. Therefore given clause{article} is aimed to the aid in increase in functionality of your programs, even on the part of speed of loading of pages. Because of that that users much{many} the Internet, is especial in Russia leave in the Internet through the usual modem, and speed as you know full g.... Loadings of pages through the modem occurs about speed on the average 4 kB in a second, predstavte if you load page in weight 60 kB or it is even worse 100 kB to expect to you pridjot`sja from 15 till 25 seconds. And if here to add on a plenty sql searches in a script and probably not optimized code that..... Therefore I offer kehshirovat` pages with the help of class WebCachePage_v_1.0. I shall not describe in detail all functions of a class, and I shall tell a principle of job of a class and as them{him;it} to use. With a code I think you and will understand. Initially the class was created as addition to obrabotchiku patterns WebTemplate_v_1.1 (shortly I shall present{introduce} it to you). The principle of job of a class is based on three standard functions php: ob_start () - includes buffering a conclusion of the data. ob_get_contents () - returns contents of the buffer (it is possible to tire out{drive} in a variable). ob_end_clean () - clears the buffer of a conclusion and disconnects buffering a conclusion. C the help of function ob_start () - buferiziruem a content of a file, then with the help of function ob_get_contents () we exhaust a content of a file in a variable and at last we clear the buffer function ob_end_clean (). A code of a class (unfortunately the link that it to download I can not give as there is no server): CLASS CACHE {// Begin class // The name of a class var $name = "WebCachePage"; // The version of a class of caching var $version = " _ v_1.0 "; // KHehsh a code of a file var $hash_code = ""; // The catalogue of caching of files var $dir_cache = "cache"; // A variable containing date of last caching which it is received as a result of calculations var $modifi_file; // Time of last updating // 10 mines are established in seconds, by default var $time_file_cache=600; // Storage of value of a content of a file // At buffering var $CONTENT; /* ---------------------------------------------- The main function kehshiruemykh files ---------------------------------------------- */ function haupt_cache () { $this-> code_file (); // Reception khehsh a code of a file $this-> clean_content (); // Reception and otchistka the buffer if ($this-> file_and_katalog_isset () == FALSE) $this-> create_cache_file (); // if it no Creation of a file of a cache else $this-> cache_file (); // Check of last updating if ($this-> modifi_file> $this-> time_file_cache) $this-> mod_file_cache (); // Modifying of a file of a cache } /* ------------------------------------------------------------------------------- Function of check of existence of a file and the catalogue of caching ------------------------------------------------------------------------------- */ function file_and_katalog_isset () { if (file_exists ($this-> dir_cache. "/". $ this-> hash_code. ".php ")) return TRUE; // Checks existence of a file in the catalogue of caching else return FALSE; } /* ---------------------------------------------- Function of caching of a content of a file ---------------------------------------------- */ function cache_content () { ob_start (); // Kehshiruem a content of a file } /* ------------------------------------------------------- Function of transformation url a file in khehsh a code ------------------------------------------------------- */ function code_file () { $url = $ GLOBALS [' REQUEST_URI ']; // It is received url a file $hashcode_file=md5 ($url); // KHehsh a code of a file $this-> hash_code = $ hashcode_file; } /* ---------------------------------------------------------------------------------- Function of creation of a file of a cache if no it in the catalogue of caching ---------------------------------------------------------------------------------- */ function create_cache_file () { $open=fopen ($this-> dir_cache. "/". $ this-> hash_code. ".php "," at ") or die (trigger_error (" It is impossible to open a file ". $this-> hash_code. ".php [". $this-> name. $ this-> version. "] ", E_USER_ERROR)); // we Create a file with a cache flock ($open, LOCK_EX); // we Block a file rewind ($open); // Installation of a position in the beginning of a file $time_create_file=time (); // Time of creation kehshiruemogo a file $write_file = $ this-> CONTENT. ""; // we Form a content of a file for recording fwrite ($open, $write_file); // It is written down the data fclose ($open); // we Close a file readfile ($this-> dir_cache. "/". $ this-> hash_code. ".php "); } /* ------------------------------------------------------------- Function of check of last caching of a file ------------------------------------------------------------- */ function cache_file () { $fopen=fopen ($this-> dir_cache. "/". $ this-> hash_code. ".php "," rt ") or die (trigger_error (" It is impossible to open a file ". $this-> hash_code. ".php [". $this-> name. $ this-> version. "] ", E_USER_ERROR)); // we Open a file $fread=fread ($fopen, filesize ($this-> dir_cache. "/". $ this-> hash_code. ".php ")); // we Read a content of a file preg_match_all (" / ()/i ", $fread, $time_mod); // Ishem date of last updating of a file fclose ($fopen); // we Close a file with a cache $mod_file=time () - $time_mod [2 [0]; // Time of last caching of a file $this-> modifi_file = $ mod_file; // we Appropriate{Give} updating a file of a variable if ($this-> modifi_file <$this-> time_file_cache) // we Check time of last caching { readfile ($this-> dir_cache. "/". $ this-> hash_code. ".php "); // we Connect a file } } /* ------------------------------------------------------------------------------------- Function of updating of a file if time polsednego cachings has expired ------------------------------------------------------------------------------------- */ function mod_file_cache () { unlink ($this-> dir_cache. "/". $ this-> hash_code. ".php "); // we Delete kehshiruemyj a file as time of last updating has expired $open = fopen ($this-> dir_cache. "/". $ this-> hash_code. ".php "," at ") or die (trigger_error (" It is impossible to open a file ". $this-> hash_code. ".php [". $this-> name. $ this-> version. "] ", E_USER_ERROR)); // we Open a file flock ($open, LOCK_EX); // we Block a file rewind ($open); // Installation of a position in the beginning of a file $time_create_file=time (); // Time of creation kehshiruemogo a file $write_file = $ this-> CONTENT. ""; // we Form a content of a file for recording fwrite ($open, $write_file); // It is written down the data fclose ($open); // we Close a file readfile ($this-> dir_cache. "/". $ this-> hash_code. ".php "); } /* ------------------------------------------- Function otchistki the catalogue with a cache ------------------------------------------- */ function cleaning_cache_katalog ($type, $file = "") { switch ($type) { case "all": $dir=opendir ($this-> dir_cache); // we Open the catalogue with a cache while ($delete_file=readdir ($dir)) // we Read the catalogue with a cache in a cycle { unlink ($this-> dir_cache. "/". $ delete_file); // we Delete all files } closedir ($dir); break; case "file": if (unlink ($this-> dir_cache. "/". $ file)) echo " the File of a cache ". $file. " it is successfully removed from the catalogue "; // we Delete a file from the catalogue with a cache else trigger_error (" It is impossible to remove a file with a cache [". $this-> name. $ this-> version. "]", E_USER_ERROR); // Vyvodmi the message on a mistake break; default: trigger_error (" Such the command is absent [". $this-> name. $ this-> version. "]", E_USER_ERROR); // the Message on a mistake break; } } /* --------------------------------- Function of reception of the buffer --------------------------------- */ function clean_content () { $this-> CONTENT=ob_get_contents (); // we Write down the buffer in a variable ob_end_clean (); // we Clean off the buffer } } // End_class To begin job with a class to you it is necessary to create the catalogue for kehshiruemykh files (by default cache). Then will switch on a class in a file which kehshirovat`sja with the help of functions include, require, but it is the best way require_once. After all done actions in the beginning of the document it is necessary for you to declare a class and to call function of buffering: $new=new CACHE; $new-> cache_content (); Further there is a file which shall be kehshirovat` and at the end of a file the main function of caching is caused: $new-> haupt_cache (); If you suddenly want to clean off the catalogue with kehshiruemymi files for this purpose there is a special function cleaning_cache_katalog ($type, $file = "") Two parameters are necessary for job with function $type for you - sets type otchistki the catalogue (all - the catalogue, file - a separate file). If suddenly you want to remove a separate file with a cache to you it is necessary $type to specify in parameter file, and in parameter $file - a name of a file. Let's result an example index.php: <? php require_once ("cache_class.php"); // we Connect a class $new=new CACHE; // we Declare a class $new-> cache_content (); // Buferiziruem a content of a file ?> <html> <body> <table border=1> <tr> <td> Name </td> <td> </td> <td> The address </td> </tr> <tr> <td> maksim </td> <td> Maksik1@yandex.ru </td> <td> http: // users.oskol.ru / ~ dergach/ </td> </tr> </table> <? php $new-> haupt_cache (); ?> That's all. Success to you in studying PHP. | |