PHP Cache File Cache

Source: Internet
Author: User

1, PHP file cache content Save format
PHP file cache Content storage format is mainly three kinds:
(1) Variable var_export formatted into PHP normal assignment writing format;
(2) The variable serialize is stored after serialization and is deserialized when used;
(3) The variable json_encode is saved after formatting, when used Json_decode
The test results on the Internet are as follows: The parsing efficiency of serialize format is greater than that of Json,json, which is higher than php normal assignment.
So if we cache the data, we suggest that parsing the data in serialized form is faster.

2. Simple case of PHP file cache

[PHP]View Plaincopyprint?
  1. <?php
  2. class cache_driver{
  3. //Define the path of the cache  
  4. protected $_cache_path;
  5. //Get path information based on Cache_path value in $config  
  6. Public function cache_driver ($config)
  7. {
  8. if (is_array($config) && isset ($config[' Cache_path ']))
  9. {
  10. $this ->_cache_path = $config [' Cache_path '];
  11. }
  12. Else  
  13. {
  14. $this ->_cache_path = Realpath (dirname (__file__). "/" ). "/cache/" ; 
  15. }
  16. }
  17. //Determine if the file corresponding to the key value exists, and if present, read value, value to serialize the storage  
  18. Public function get ($id)
  19. {
  20. if ( ! file_exists ($this->_cache_path. $id )) 
  21. {
  22. return FALSE;
  23. }
  24. $data = @ file_get_contents ($this->_cache_path. $id );
  25. $data = unserialize ($data);
  26. if (! Is_array ($data) | |!isset ($data[' time ']) | |!isset ($ Data[' ttl ']))
  27. {
  28. return FALSE;
  29. }
  30. if ($data[' ttl '] > 0 && time () > $data[ ' Time ' ] +$data[' ttl '])
  31. {
  32. @unlink ($this->_cache_path. $id );
  33. return FALSE;
  34. }
  35. return $data[' data '];
  36. }
  37. //Set the cache information, generate the corresponding cache file according to the key value  
  38. Public function set ($id,$data, $ttl =)
  39. {
  40. $contents = Array ( 
  41. ' Time ' = Time (),
  42. ' TTL ' = $ttl ,          
  43. ' Data ' = $data  
  44. );
  45. if (@file_put_contents($this->_cache_path. $id , serialize ($contents)))
  46. {
  47. @chmod($this->_cache_path. $id , 0777);
  48. return             TRUE;
  49. }
  50. return  FALSE;
  51. }
  52. //Delete the cache file according to the key value  
  53. Public functiondelete($id)
  54. {
  55. return @unlink ($this->_cache_path. $id );
  56. }
  57. Public function clean ()
  58. {
  59. $DH = @opendir ($this->_cache_path);
  60. if (! $DH ) 
  61. return  FALSE;
  62. while ($file = @readdir ($dh))
  63. {
  64. if ($file = ="." || $file == ".." ) 
  65. Continue  ;
  66. $path = $this ->_cache_path. "/" . $file ; 
  67. if (is_file($path))
  68. @unlink ($path);
  69. }
  70. @closedir($dh);
  71. return  TRUE;
  72. }
  73. }

PHP Cache File Cache

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.