一個PHP緩衝類

來源:互聯網
上載者:User
  1. cache.inc.php:
  2. class Cache {
  3. /**
  4. * $dir : 快取檔案存放目錄
  5. * $lifetime : 快取檔案有效期間,單位為秒
  6. * $cacheid : 快取檔案路徑,包含檔案名稱
  7. * $ext : 快取檔案副檔名(可以不用),這裡使用是為了查看檔案方便
  8. */
  9. private $dir;
  10. private $lifetime;
  11. private $cacheid;
  12. private $ext;
  13. /**
  14. * 解構函式,檢查緩衝目錄是否有效,預設賦值
  15. */
  16. function __construct($dir='',$lifetime=1800) {
  17. if ($this->dir_isvalid($dir)) {
  18. $this->dir = $dir;
  19. $this->lifetime = $lifetime;
  20. $this->ext = '.Php';
  21. $this->cacheid = $this->getcacheid();
  22. }
  23. }
  24. /**
  25. * 檢查緩衝是否有效
  26. */
  27. private function isvalid() {
  28. if (!file_exists($this->cacheid)) return false;
  29. if (!(@$mtime = filemtime($this->cacheid))) return false;
  30. if (mktime() - $mtime > $this->lifetime) return false;
  31. return true;
  32. }
  33. /**
  34. * 寫入緩衝
  35. * $mode == 0 , 以瀏覽器緩衝的方式取得頁面內容
  36. * $mode == 1 , 以直接賦值(通過$content參數接收)的方式取得頁面內容
  37. * $mode == 2 , 以本地讀取(fopen ile_get_contents)的方式取得頁面內容(似乎這種方式沒什麼必要)
  38. */
  39. public function write($mode=0,$content='') {
  40. switch ($mode) {
  41. case 0:
  42. $content = ob_get_contents();
  43. break;
  44. default:
  45. break;
  46. }
  47. ob_end_flush();
  48. try {
  49. file_put_contents($this->cacheid,$content);
  50. }
  51. catch (Exception $e) {
  52. $this->error('寫入緩衝失敗!請檢查目錄許可權!');
  53. }
  54. }
  55. /**
  56. * 載入緩衝
  57. * exit() 載入緩衝後終止原頁面程式的執行,快取無效判定則運行原頁面程式產生緩衝
  58. * ob_start() 開啟瀏覽器緩衝用於在頁面結尾處取得頁面內容
  59. */
  60. public function load() {
  61. if ($this->isvalid()) {
  62. echo "This is Cache. ";
  63. //以下兩種方式,哪種方式好?????
  64. require_once($this->cacheid);
  65. //echo file_get_contents($this->cacheid);
  66. exit();
  67. }
  68. else {
  69. ob_start();
  70. }
  71. }
  72. /**
  73. * 清除緩衝
  74. */
  75. public function clean() {
  76. try {
  77. unlink($this->cacheid);
  78. }
  79. catch (Exception $e) {
  80. $this->error('清除快取檔案失敗!請檢查目錄許可權!');
  81. }
  82. }
  83. /**
  84. * 取得快取檔案路徑
  85. */
  86. private function getcacheid() {
  87. return $this->dir.md5($this->geturl()).$this->ext;
  88. }
  89. /**
  90. * 檢查目錄是否存在或是否可建立
  91. */
  92. private function dir_isvalid($dir) {
  93. if (is_dir($dir)) return true;
  94. try {
  95. mkdir($dir,0777);
  96. }
  97. catch (Exception $e) {
  98. $this->error('所設定緩衝目錄不存在並且建立失敗!請檢查目錄許可權!');
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * 取得當前頁面完整url
  105. */
  106. private function geturl() {
  107. $url = '';
  108. if (isset($_SERVER['REQUEST_URI'])) {
  109. $url = $_SERVER['REQUEST_URI'];
  110. }
  111. else {
  112. $url = $_SERVER['Php_SELF'];
  113. $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];
  114. }
  115. return $url;
  116. }
  117. /**
  118. * 輸出錯誤資訊
  119. */
  120. private function error($str) {
  121. echo ''.$str.'';
  122. }
  123. }
  124. ?>
複製代碼
  1. demo.php:
  2. /*
  3. * 可自由轉載使用,請保留著作權資訊,謝謝使用!
  4. * Class Name : Cache (For Php5)
  5. * Version : 1.0
  6. * Description : 動態緩衝類,用於控制頁面自動產生緩衝、調用緩衝、更新緩衝、刪除緩衝.
  7. * Author : jiangjun8528@163.com,Junin
  8. * Author Page : http://blog.csdn.Net/sdts/
  9. * Last Modify : 2007-8-22
  10. * Remark :
  11. 1.此版本為Php5版本,本人暫沒有寫Php4的版本,如需要請自行參考修改(比較容易啦,不要那麼懶嘛,呵呵!).
  12. 2.此版本為utf-8編碼,如果網站採用其它編碼請自行轉換,Windows系統用記事本開啟另存新檔,選擇相應編碼即可(一般ANSI),Linux下請使用相應編輯軟體或iconv命令列.
  13. 3.拷貝粘貼的就不用管上面第2條了.
  14. * 關於緩衝的一點感想:
  15. * 動態緩衝和靜態緩衝的根本差別在於其是自動的,使用者訪問頁面過程就是產生緩衝、瀏覽緩衝、更新緩衝的過程,無需人工操作幹預.
  16. * 靜態緩衝指的就是產生靜態頁面,相關操作一般是在網站後台完成,需人工操作(也就是手動產生).
  17. */
  18. /*
  19. * 使用方法舉例
  20. ------------------------------------Demo1-------------------------------------------
  21. require_once('cache.inc.php');
  22. $cachedir = './Cache/'; //設定緩衝目錄
  23. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  24. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝,以此類推,還可以設定一些其它操作
  25. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  26. //頁面代碼開始
  27. echo date('H:i:s jS F');
  28. //頁面代碼結束
  29. $cache->write(); //首次運行或緩衝到期,產生緩衝
  30. ------------------------------------Demo2-------------------------------------------
  31. require_once('cache.inc.php');
  32. $cachedir = './Cache/'; //設定緩衝目錄
  33. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  34. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝,以此類推,還可以設定一些其它操作
  35. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  36. //頁面代碼開始
  37. $content = date('H:i:s jS F');
  38. echo $content;
  39. //頁面代碼結束
  40. $cache->write(1,$content); //首次運行或緩衝到期,產生緩衝
  41. ------------------------------------Demo3-------------------------------------------
  42. require_once('cache.inc.php');
  43. define('CACHEENABLE',true);
  44. if (CACHEENABLE) {
  45. $cachedir = './Cache/'; //設定緩衝目錄
  46. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  47. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝,以此類推,還可以設定一些其它操作
  48. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  49. }
  50. //頁面代碼開始
  51. $content = date('H:i:s jS F');
  52. echo $content;
  53. //頁面代碼結束
  54. if (CACHEENABLE)
  55. $cache->write(1,$content); //首次運行或緩衝到期,產生緩衝
  56. */
  57. ?>
複製代碼
PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.