What do you know about PHP cache? In-depth exploration of PHP Cache principles and usage

Source: Internet
Author: User

Due to its powerful and scalability, PHP has been greatly developed in recent years. Compared with traditional ASP websites, PHP has an absolute speed advantage, if you want mssql to convert 60 thousand pieces of data to PHP, it will take 40 seconds and ASP will take less than 2 minutes. however, due to the increasing number of website data, we are eager to call data more quickly. We do not need to drop data from the database every time. We can choose from other places, such as a file, or a memory address, which is the Cache Technology of PHP.

In-depth analysis

In general, the purpose of caching is to put data in one place to make access faster. There is no doubt that the memory is the fastest, but can hundreds of MB of data be stored in the memory? This is not realistic. Of course, sometimes it is temporarily stored as a server cache, for example, if the ob_start () cache page is enabled, the page content will be cached in the memory before the file header is sent, knowing that the page output will be clear automatically or wait for the ob_get_contents to return, or be cleared by the ob_end_clean display, which can be used well in the generation of static pages and can be well reflected in the template.

In addition, there is an object application in ASP that can save common parameters. This is also a bit of cache. However, in PHP, I have not seen developers generate such objects yet. It is indeed unnecessary. ASP. the NET page cache technology uses viewstate, while the cache is File Association (not necessarily accurate), the file is modified, and the cache is updated, if the file is not modified and does not Time Out (note 1), read the cache and return the result. This is the idea. Let's look at the source code:

 

 
 
  1. <? PHP
  2. Class cache {
  3. Private $ cache_dir;
  4. Private $ expireTime = 180; // The cache time is 60 seconds.
  5. Function _ construct ($ cache_dirname ){
  6. If (! @ Is_dir ($ cache_dirname )){
  7. If (! @ Mkdir ($ cache _ dirname, 0777 )){
  8. $ This-> warn (the cache file does not exist and cannot be created. You need to create it manually .);
  9. Return false;
  10. }
  11. }
  12. $ This-> cache_dir = $ cache_dirname;
  13. }
  14. Function _ destruct (){
  15. Echo Cache class bye .;
  16. }
  17. Function get_url (){
  18. If (! Isset ($ _ SERVER [REQUEST_URI]) {
  19. $ Url = $ _ SERVER [REQUEST_URI];
  20. } Else {
  21. $ Url = $ _ SERVER [SCRIPT_NAME];
  22. $ Url. = (! Emptyempty ($ _ SERVER [QUERY_STRING])? ? . $ _ SERVER [QUERY_STRING]:;
  23. }
  24. Return $ url;
  25. }
  26. Function warn ($ errorstring ){
  27. Echo"<B> <fontColor = red>Error:<Pre>". $ Errorstring ."</Pre> </font> </B>";
  28. }
  29.  
  30. Function cache_page ($ pageurl, $ pagedata ){
  31. If (! $ Fso = fopen ($ pageurl, w )){
  32. $ This->Warns (the cached file cannot be opened.); // trigger_error
  33. Return false;
  34. }
  35. If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
  36. $ This->Warns (the cache file cannot be locked.); // trigger_error
  37. Return false;
  38. }
  39. If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
  40. $ This->Warns (the cache file cannot be written.); // trigger_error
  41. Return false;
  42. }
  43. Flock ($ fso, LOCK_UN); // release lock
  44. Fclose ($ fso );
  45. Return true;

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.