PHP Cache file cache

Source: Internet
Author: User
PHP Cache file cache 1. php file cache content storage format
There are three main formats for saving php file cache content:
(1) var_export is formatted as a normal value assignment format in PHP;
(2) the variable serialize is saved after serialization and deserialized when used;
(3) the json_encode variable is formatted and saved. when used, json_decode
On the Internet, the test result is: the file parsing efficiency in serialize format is greater than that in Json format, and the Json parsing efficiency is greater than that in PHP normal assignment.
Therefore, we recommend that you parse the data in serialized form faster if the cached data is used.

2. simple php file cache case

[Php]View plain copy print?

  1. Class Cache_Driver {
  2. // Define the cache path
  3. Protected $ _ cache_path;
  4. // Obtain the path information based on the cache_path value in $ config
  5. Public function Cache_Driver ($ config)
  6. {
  7. If (is_array ($ config) & isset ($ config ['cache _ path'])
  8. {
  9. $ This-> _ cache_path = $ config ['cache _ path'];
  10. }
  11. Else
  12. {
  13. $ This-> _ cache_path = realpath (dirname (_ FILE _). "/"). "/cache /";
  14. }
  15. }
  16. // Determine whether the file corresponding to the key value exists. If yes, read the value and value for serialized storage.
  17. Public function get ($ id)
  18. {
  19. If (! File_exists ($ this-> _ cache_path. $ id ))
  20. {
  21. Return FALSE;
  22. }
  23. $ Data = @ file_get_contents ($ this-> _ cache_path. $ id );
  24. $ Data = unserialize ($ data );
  25. If (! Is_array ($ data) |! Isset ($ data ['Time']) |! Isset ($ data ['ttl '])
  26. {
  27. Return FALSE;
  28. }
  29. If ($ data ['ttl ']> 0 & time ()> $ data ['Time'] + $ data ['ttl'])
  30. {
  31. @ Unlink ($ this-> _ cache_path. $ id );
  32. Return FALSE;
  33. }
  34. Return $ data ['data'];
  35. }
  36. // Set the cache information and generate the corresponding cache file based on the key value
  37. Public function set ($ id, $ data, $ ttl = 60)
  38. {
  39. $ Contents = array (
  40. 'Time' => time (),
  41. 'Ttl '=> $ ttl,
  42. 'Data' => $ data
  43. );
  44. If (@ file_put_contents ($ this-> _ cache_path. $ id, serialize ($ contents )))
  45. {
  46. @ Chmod ($ this-> _ cache_path. $ id, 0777 );
  47. Return TRUE;
  48. }
  49. Return FALSE;
  50. }
  51. // Delete the cached file based on the key value
  52. Public function delete ($ id)
  53. {
  54. Return @ unlink ($ this-> _ cache_path. $ id );
  55. }
  56. Public function clean ()
  57. {
  58. $ Dh = @ opendir ($ this-> _ cache_path );
  59. If (! $ Dh)
  60. Return FALSE;
  61. While ($ file = @ readdir ($ dh ))
  62. {
  63. If ($ file = "." | $ file = "..")
  64. Continue;
  65. $ Path = $ this-> _ cache_path. "/". $ file;
  66. If (is_file ($ path ))
  67. @ Unlink ($ path );
  68. }
  69. @ Closedir ($ dh );
  70. Return TRUE;
  71. }
  72. }

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.