How much do you know about PHP's cache? Explore PHP caching principles and using _php tutorials

Source: Internet
Author: User
Tags flock vars
PHP because of its strong and scalable, in recent years has been rapid development, PHP compared to the traditional ASP site, in the speed of the absolute advantage, want to MSSQL 60,000 data PHP if 40 seconds, ASP less than 2 minutes. However, because of the increasing number of data on the website, We want to be able to call the data more quickly, not every time from the database, we can from other places, such as a file, or a memory address, this is the PHP cache technology, that is, cache technology.

Analysis in depth

In general, the purpose of caching is to put data in one place to make access faster, without a doubt, memory is the fastest, but hundreds of m of data can be stored inside it? This is unrealistic, of course, sometimes temporarily placed as a server cache, such as Ob_start () This cache page is opened before sending the file header page content is cached in memory, know that the output of the page automatically clear or wait for the return of ob_get_contents, or be ob_end_clean display of the cleanup, which in the static page generation can be very good use, In the template can be very good embodiment.

In addition, there is an object in the ASP application, can save the common parameters, which is also a point cache, but in PHP, I have not seen the developers to produce this object, indeed, not necessary. Asp. NET page caching technology is viewstate, and the cache is a file association, (not necessarily accurate), the file is modified, update the cache, the file has not been modified and does not time out (note 1), read the cache, return results, is this idea, see this source:

 
 
  1. class cache{
  2. Private $cache _dir;
  3. Private $expireTime=180; //cache time is 60 seconds
  4. function __construct ($cache _dirname) {
  5. if(!@is_dir($cache _dirname)) {
  6. if (!@mkdir($cache _dirname, 0777)) {
  7. $this ->warn ( The cache file does not exist and cannot be created, it needs to be created manually. ) );
  8. return false;
  9. }
  10. }
  11. $this ->cache_dir = $cache _dirname ;
  12. }
  13. function __destruct () {
  14. echo Cache class bye. ;
  15. }
  16. function Get_url () {
  17. if (!isset ($_server[request_uri])) {
  18. $url = $_server [Request_uri];
  19. }else{
  20. $url = $_server [script_name];
  21. $url .= (! Empty Empty ($_server[query_string])? ? . $_server [query_string]: ;
  22. }
  23. return $url;
  24. }
  25. function warn ($errorstring) {
  26. echo " <b><font color=red> error occurred:<Pre >". $errorstring." pre> font> b > ";
  27. }
  28. function Cache_page ($pageurl, $pagedata) {
  29. if (!$fso=fopen($pageurl, W)) {
  30. $this- > warns (unable to open cache file.); /trigger_error
  31. return false;
  32. }
  33. if (!flock ($fso, lock_ex)) {//LOCK_NB, exclusive type lock
  34. $this- > warns (Unable to lock cache file.); /trigger_error
  35. return false;
  36. }
  37. if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format
  38. $this- > warns (unable to write to cache file.); /trigger_error
  39. return false;
  40. }
  41. Flock ($fso, lock_un);//Release lock
  42. Fclose ($FSO);
  43. return true;
  44. http://www.bkjia.com/PHPjc/486256.html www.bkjia.com true http://www.bkjia.com/PHPjc/486256.html techarticle PHP because of its strong and scalable, in recent years has been a great development, PHP compared to the traditional ASP site, in the speed of the absolute advantage, want to MSSQL 60,000 data PHP if it takes 40 seconds, ASP not ...

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.