PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/!PHP/???/cache/cache.php

http://mootoolstools.googlecode.com/
PHP | 66 lines | 57 code | 4 blank | 5 comment | 10 complexity | 20bd3d71ca828ec5be1cfa3463a388ef MD5 | raw file
  1. <?php
  2. class cache
  3. {
  4. var $cache_dir = './cache/';//This is the directory where the cache files will be stored;
  5. var $cache_time = 120;//How much time will keep the cache files in seconds.
  6. var $caching = false;
  7. var $file = '';
  8. function cache()
  9. {
  10. //Constructor of the class
  11. $this->file = $this->cache_dir . urlencode( $_SERVER['REQUEST_URI'] );
  12. if(file_exists($this->file)) $expired = $this->check_expire();
  13. else $expired = false;
  14. if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )
  15. {
  16. //Grab the cache:
  17. $handle = fopen( $this->file , "r");
  18. do {
  19. $data = fread($handle, 8192);
  20. if (strlen($data) == 0) {
  21. break;
  22. }
  23. echo $data;
  24. } while (true);
  25. fclose($handle);
  26. exit();
  27. }
  28. else
  29. {
  30. //create cache :
  31. $this->caching = true;
  32. ob_start();
  33. $now = time();
  34. echo "<!--last modified:".$now."-->\n";
  35. }
  36. }
  37. function close()
  38. {
  39. //You should have this at the end of each page
  40. if ( $this->caching )
  41. {
  42. //You were caching the contents so display them, and write the cache file
  43. $data = ob_get_clean();
  44. echo $data;
  45. $fp = fopen( $this->file , 'w' );
  46. fwrite ( $fp , $data );
  47. fclose ( $fp );
  48. }
  49. }
  50. function check_expire(){
  51. $fp = fopen($this->file,"r");
  52. preg_match("/\:([\d]+)\-/",fread($fp,200),$time);
  53. $modify_time = $time[1];
  54. if($modify_time<filemtime($_SERVER['SCRIPT_FILENAME'])){
  55. return true;
  56. }
  57. else{
  58. return false;
  59. }
  60. }
  61. }
  62. ?>