一個php緩衝類與調用樣本

來源:互聯網
上載者:User
  1. class Cache {
  2. /**
  3. * $dir : 快取檔案存放目錄
  4. * $lifetime : 快取檔案有效期間,單位為秒
  5. * $cacheid : 快取檔案路徑,包含檔案名稱
  6. * $ext : 快取檔案副檔名(可以不用),這裡使用是為了查看檔案方便
  7. */
  8. private $dir;
  9. private $lifetime;
  10. private $cacheid;
  11. private $ext;
  12. /**
  13. * 解構函式,檢查緩衝目錄是否有效,預設賦值
  14. */
  15. function __construct($dir='',$lifetime=1800) {
  16. if ($this->dir_isvalid($dir)) {
  17. $this->dir = $dir;
  18. $this->lifetime = $lifetime;
  19. $this->ext = '.Php';
  20. $this->cacheid = $this->getcacheid();
  21. }
  22. }
  23. /**
  24. * 檢查緩衝是否有效
  25. * edit bbs.it-home.org
  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. ?>
複製代碼

2,php緩衝類的示範代碼

  1. /*

  2. * 可自由轉載使用,請保留著作權資訊,謝謝使用!
  3. * Class Name : Cache (For Php5)
  4. * Version : 1.0
  5. * Description : 動態緩衝類,用於控制頁面自動產生緩衝、調用緩衝、更新緩衝、刪除緩衝.
  6. * Remark :
  7. 1.此版本為Php5版本
  8. 2.此版本為utf-8編碼,如果網站採用其它編碼請自行轉換,Windows系統用記事本開啟另存新檔,選擇相應編碼即可(一般ANSI),Linux下請使用相應編輯軟體或iconv命令列.
  9. 3.拷貝粘貼的就不用管上面第2條了.
  10. * 關於緩衝的一點感想:
  11. * 動態緩衝和靜態緩衝的根本差別在於其是自動的,使用者訪問頁面過程就是產生緩衝、瀏覽緩衝、更新緩衝的過程,無需人工操作幹預.
  12. * 靜態緩衝指的就是產生靜態頁面,相關操作一般是在網站後台完成,需人工操作(也就是手動產生).
  13. */
  14. /*
  15. * 使用方法舉例
  16. ----------Demo1--------
  17. require_once('cache.inc.php');
  18. $cachedir = './Cache/'; //設定緩衝目錄
  19. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  20. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝
  21. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  22. //頁面代碼開始
  23. echo date('H:i:s jS F');
  24. //頁面代碼結束
  25. $cache->write(); //首次運行或緩衝到期,產生緩衝

  26. ---------Demo2-------

  27. require_once('cache.inc.php');
  28. $cachedir = './Cache/'; //設定緩衝目錄
  29. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  30. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝
  31. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  32. //頁面代碼開始
  33. $content = date('H:i:s jS F');
  34. echo $content;
  35. //頁面代碼結束
  36. $cache->write(1,$content); //首次運行或緩衝到期,產生緩衝

  37. ------------Demo3----

  38. require_once('cache.inc.php');
  39. define('CACHEENABLE',true);
  40. if (CACHEENABLE) {
  41. $cachedir = './Cache/'; //設定緩衝目錄
  42. $cache = new Cache($cachedir,10); //省略參數即採用預設設定, $cache = new Cache($cachedir);
  43. if ($_GET['cacheact'] != 'rewrite') //此處為一技巧,通過xx.Php?cacheact=rewrite更新緩衝
  44. $cache->load(); //裝載緩衝,緩衝有效則不執行以下頁面代碼
  45. }
  46. //頁面代碼開始
  47. $content = date('H:i:s jS F');
  48. echo $content;
  49. //頁面代碼結束
  50. if (CACHEENABLE)
  51. $cache->write(1,$content); //首次運行或緩衝到期,產生緩衝
  52. */
  53. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.