PHP Cache Class

Source: Internet
Author: User
<无详细内容>
  1. Define (' Cache_root ', DirName (__file__). ' /cache '); Cache Storage Directory
  2. Define (' Cache_time ', 1800);//cache time Unit seconds
  3. Define (' Cache_fix ', '. html ');
  4. $CacheName =md5 ($_server[' Request_uri '). Cache_fix; Cache file Name
  5. $CacheDir =cache_root. ' /'. substr ($CacheName, 0, 1);//cache file storage Directory
  6. $CACHEURL = $CacheDir. ' /'. $CacheName;//full path to the cache file
  7. Get-mode requests are cached and generally expect to see the latest results after post
  8. if ($_server[' Request_method ']== ' GET ') {
  9. If the cache file exists and does not expire, read it out.
  10. if (file_exists ($CacheName) && time ()-filemtime ($CacheName)
  11. $FP =fopen ($CacheName, ' RB ');
  12. Fpassthru ($FP);
  13. Fclose ($FP);
  14. Exit
  15. }
  16. Determines whether the folder exists, does not exist, and creates
  17. ElseIf (!file_exists ($CacheDir)) {
  18. if (!file_exists (Cache_root)) {
  19. mkdir (cache_root,0777);
  20. chmod (cache_root,0777);
  21. }
  22. mkdir ($CacheDir, 0777);
  23. chmod ($CacheDir, 0777);
  24. }
  25. callback function that calls this function automatically when the program ends
  26. function Autocache ($contents) {
  27. Global $CACHEURL;
  28. $FP =fopen ($CACHEURL, ' WB ');
  29. Fwrite ($fp, $contents);
  30. Fclose ($FP);
  31. chmod ($CACHEURL, 0777);
  32. While generating a new cache, all old caches are automatically deleted to save space and can be ignored.
  33. Deloldcache ();
  34. return $contents;
  35. }
  36. function Deloldcache () {
  37. ChDir (Cache_root);
  38. foreach (Glob ("*/*"). Cache_fix) as $file) {
  39. if (Time ()-filemtime ($file) >cache_time) unlink ($file);
  40. }
  41. }
  42. callback function Auto_cache
  43. Ob_start (' Autocache ');
  44. }else{
  45. Delete the cache file instead of the GET request.
  46. if (file_exists ($CACHEURL)) unlink ($CACHEURL);
  47. }
  48. ?>
Copy Code
  • 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.