網站適用的PHP緩衝類_PHP教程

來源:互聯網
上載者:User
緩衝在實際使用當中應用很廣泛,可以減輕對伺服器資料庫的訪問,提高運行速度。目前很多CMS內容管理系統中頻繁使用緩衝機制來提高系統啟動並執行效率。下面是一個寫得不錯的緩衝類,可以參考下緩衝的機制與寫法。

cache.php 代碼如下:

[php] view plaincopy
  1. /*
  2. 使用者需要事先定義的常量:
  3. _CachePath_ 模板緩衝路徑
  4. _CacheEnable_ 自動緩衝機制是否開啟,未定義或為空白,表示關閉自動緩衝機制
  5. _ReCacheTime_ 自動重新緩衝間隔時間,單位為秒,未定義或為空白,表示關閉自動重新緩衝
  6. */
  7. class cache
  8. {
  9. var $cachefile;
  10. var $cachefilevar;
  11. function cache()
  12. {
  13. //產生當前頁的Cache組檔案名稱 $this->cachefilevar 及檔案名稱 $this->cachefile
  14. //動態網頁的參數不同對應的Cache檔案也不同,但是每一個動態網頁的所有Cache檔案都有相同的檔案名稱,只是副檔名不同
  15. $s=array(".","/");$r=array("_","");
  16. $this->cachefilevar=str_replace($s,$r,$_SERVER["SCRIPT_NAME"])."_".$_GET[_ActionVar_];
  17. $this->cachefile=$this->cachefilevar.".".md5($_SERVER["REQUEST_URI"]);
  18. }
  19. //刪除當前頁/模組的緩衝
  20. function delete()
  21. {
  22. //刪除當前頁的緩衝
  23. $d = dir(_CachePath_);
  24. $strlen=strlen($this->cachefilevar);
  25. //返回當前頁的所有Cache檔案組
  26. while (false !== ($entry = $d->read()))
  27. {
  28. if (substr($entry,0,$strlen)==$this->cachefilevar)
  29. {
  30. if (!unlink(_CachePath_."/".$entry)) {echo "Cache目錄無法寫入";exit;}
  31. }
  32. }
  33. }
  34. //判斷是否已Cache過,以及是否需要Cache
  35. function check()
  36. {
  37. //如果設定了緩衝更新間隔時間 _ReCacheTime_
  38. if (_ReCacheTime_+0>0)
  39. {
  40. //返回當前頁Cache的最後更新時間
  41. $var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];
  42. //如果更新時間超出更新間隔時間則刪除Cache檔案
  43. if (time()-$var>_ReCacheTime_)
  44. {
  45. $this->delete();$ischage=true;
  46. }
  47. }
  48. //返回當前頁的Cache
  49. $file=_CachePath_."/".$this->cachefile;
  50. //判斷當前頁Cache是否存在 且 Cache功能是否開啟
  51. return (file_exists($file) and _CacheEnable_ and !$ischange);
  52. }
  53. //讀取Cache
  54. function read()
  55. {
  56. //返回當前頁的Cache
  57. $file=_CachePath_."/".$this->cachefile;
  58. //讀取Cache檔案的內容
  59. if (_CacheEnable_) return readfile($file);
  60. else return false;
  61. }
  62. //產生Cache
  63. function write($output)
  64. {
  65. //返回當前頁的Cache
  66. $file=_CachePath_."/".$this->cachefile;
  67. //如果Cache功能開啟
  68. if (_CacheEnable_)
  69. {
  70. //把輸出的內容寫入Cache檔案
  71. $fp=@fopen($file,'w');
  72. if (!@fputs($fp,$output)) {echo "模板Cache寫入失敗";exit;}
  73. @fclose($fp);
  74. //如果設定了緩衝更新間隔時間 _ReCacheTime_
  75. if (_ReCacheTime_+0>0)
  76. {
  77. //更新當前頁Cache的最後更新時間
  78. $file=_CachePath_."/".$this->cachefilevar;
  79. $fp=@fopen($file,'w');
  80. if (!@fwrite($fp,time())) {echo "Cache目錄無法寫入";exit;}
  81. @fclose($fp);
  82. }
  83. }
  84. }
  85. }
  86. ?>


類的使用:

[php] view plaincopy
  1. define("_CachePath_","./cache/");
  2. define("_CacheEnable_","1");
  3. define("_ReCacheTime_","43200");
  4. include('cache.php');
  5. $cache=new cache();
  6. if ($cache->check())
  7. {
  8. $template=$cache->read();
  9. }
  10. else
  11. {
  12. ob_start();
  13. ob_implicit_flush(0);
  14. ?>
  15. 頁面內容。。。。
  16. $template = ob_get_contents();
  17. $cache->write($template);
  18. }
  19. ?>


http://www.bkjia.com/PHPjc/750358.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/750358.htmlTechArticle緩衝在實際使用當中應用很廣泛,可以減輕對伺服器資料庫的訪問,提高運行速度。目前很多CMS內容管理系統中頻繁使用緩衝機制來提高系...

  • 聯繫我們

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